Commit 69286940 authored by Cédric OLIVIER's avatar Cédric OLIVIER
Browse files

Merge branch 'tf-workspace' into 'master'

Add support for terraform workspaces

Closes #45

See merge request to-be-continuous/terraform!70
parents 7860bfde e7e0d50a
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -296,6 +296,7 @@ The Terraform template uses some global configuration used throughout all jobs.
| `TF_OUTPUT_DIR`          | Terraform output directory (relative to `$TF_PROJECT_DIR`). Everything generated in this directory will be kept as job artifacts. | `tf-output` |
| `TF_EXTRA_OPTS`          | Default Terraform extra options (applies to all Terraform commands) | _none_ |
| `TF_INIT_OPTS`           | Default Terraform extra [init options](https://developer.hashicorp.com/terraform/cli/commands/init) | _none_ |
| `TF_WORKSPACE`           | Default Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) | _none_ (do not use workspaces) |
| `TF_APPLY_OPTS`          | Default Terraform extra [apply options](https://developer.hashicorp.com/terraform/cli/commands/apply) | _none_ |
| `TF_DESTROY_OPTS`        | Default Terraform extra [destroy options](https://developer.hashicorp.com/terraform/cli/commands/destroy) | _none_ |

@@ -312,6 +313,7 @@ Here are variables supported to configure review environments:
| `TF_REVIEW_ENABLED`      | Set to `true` to enable your `review` environments | _none_ (disabled) |
| `TF_REVIEW_EXTRA_OPTS`   | Terraform extra options for `review` env (applies to all Terraform commands) | `$TF_EXTRA_OPTS` |
| `TF_REVIEW_INIT_OPTS`    | Terraform extra [init options](https://developer.hashicorp.com/terraform/cli/commands/init) for `review` env | `$TF_INIT_OPTS` |
| `TF_REVIEW_WORKSPACE`    | Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) for `review` env | `$TF_WORKSPACE` |
| `TF_REVIEW_PLAN_ENABLED` | Set to `true` to enable separate Terraform plan job for `review` env. | _none_ (disabled) |
| `TF_REVIEW_PLAN_OPTS`    | Terraform extra [plan options](https://developer.hashicorp.com/terraform/cli/commands/plan) for `review` env | _none_ |
| `TF_REVIEW_APPLY_OPTS`   | Terraform extra [apply options](https://developer.hashicorp.com/terraform/cli/commands/apply) for `review` env | `$TF_APPLY_OPTS` |
@@ -330,6 +332,7 @@ Here are variables supported to configure the integration environment:
| `TF_INTEG_ENABLED`      | Set to `true` to enable your `integration` env | _none_ (disabled) |
| `TF_INTEG_EXTRA_OPTS`   | Terraform extra options for `integration` env (applies to all Terraform commands) | `$TF_EXTRA_OPTS` |
| `TF_INTEG_INIT_OPTS`    | Terraform extra [init options](https://developer.hashicorp.com/terraform/cli/commands/init) for `integration` env | `$TF_INIT_OPTS` |
| `TF_INTEG_WORKSPACE`    | Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) for `integration` env | `$TF_WORKSPACE` |
| `TF_INTEG_PLAN_ENABLED` | Set to `true` to enable separate Terraform plan job for `integration` env. | _none_ (disabled) |
| `TF_INTEG_PLAN_OPTS`    | Terraform extra [plan options](https://developer.hashicorp.com/terraform/cli/commands/plan) for `integration` env | _none_ |
| `TF_INTEG_APPLY_OPTS`   | Terraform extra [apply options](https://developer.hashicorp.com/terraform/cli/commands/apply) for `integration` env | `$TF_APPLY_OPTS` |
@@ -349,6 +352,7 @@ Here are variables supported to configure the staging environment:
| `TF_STAGING_ENABLED`      | Set to `true` to enable your `staging` env | _none_ (disabled) |
| `TF_STAGING_EXTRA_OPTS`   | Terraform extra options for `staging` env (applies to all Terraform commands) | `$TF_EXTRA_OPTS` |
| `TF_STAGING_INIT_OPTS`    | Terraform extra [init options](https://developer.hashicorp.com/terraform/cli/commands/init) for `staging` env | `$TF_INIT_OPTS` |
| `TF_STAGING_WORKSPACE`    | Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) for `staging` env | `$TF_WORKSPACE` |
| `TF_STAGING_PLAN_ENABLED` | Set to `true` to enable separate Terraform plan job for `staging` env. | _none_ (disabled) |
| `TF_STAGING_PLAN_OPTS`    | Terraform extra [plan options](https://developer.hashicorp.com/terraform/cli/commands/plan) for `staging` env | _none_ |
| `TF_STAGING_APPLY_OPTS`   | Terraform extra [apply options](https://developer.hashicorp.com/terraform/cli/commands/apply) for `staging` env | `$TF_APPLY_OPTS` |
@@ -367,6 +371,7 @@ Here are variables supported to configure the production environment:
| `TF_PROD_ENABLED`      | Set to `true` to enable your `production` env | _none_ (disabled) |
| `TF_PROD_EXTRA_OPTS`   | Terraform extra options for `production` env (applies to all Terraform commands) | `$TF_EXTRA_OPTS` |
| `TF_PROD_INIT_OPTS`    | Terraform extra [init options](https://developer.hashicorp.com/terraform/cli/commands/init) for `production` env | `$TF_INIT_OPTS` |
| `TF_PROD_WORKSPACE`    | Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) for `production` env | `$TF_WORKSPACE` |
| `TF_PROD_PLAN_ENABLED` | Set to `true` to enable separate Terraform plan job for `production` env. | `true` (enabled) |
| `TF_PROD_PLAN_OPTS`    | Terraform extra [plan options](https://developer.hashicorp.com/terraform/cli/commands/plan) for `production` env | _none_ |
| `TF_PROD_APPLY_OPTS`   | Terraform extra [apply options](https://developer.hashicorp.com/terraform/cli/commands/apply) for `production` env | `$TF_APPLY_OPTS` |
+29 −0
Original line number Diff line number Diff line
@@ -43,6 +43,11 @@
      "description": "Default Terraform extra [init options](https://developer.hashicorp.com/terraform/cli/commands/init)",
      "advanced": true
    },
    {
      "name": "TF_WORKSPACE",
      "description": "Default Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces)",
      "advanced": true
    },
    {
      "name": "TF_PLAN_OPTS",
      "description": "Default Terraform extra [plan options](https://developer.hashicorp.com/terraform/cli/commands/plan)",
@@ -177,6 +182,12 @@
          "default": "$TF_INIT_OPTS",
          "advanced": true
        },
        {
          "name": "TF_REVIEW_WORKSPACE",
          "description": "Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) for `review` env",
          "default": "$TF_WORKSPACE",
          "advanced": true
        },
        {
          "name": "TF_REVIEW_PLAN_ENABLED",
          "description": "Set to enable separate Terraform plan job for `review` env.",
@@ -221,6 +232,12 @@
          "default": "$TF_INIT_OPTS",
          "advanced": true
        },
        {
          "name": "TF_INTEG_WORKSPACE",
          "description": "Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) for `integration` env",
          "default": "$TF_WORKSPACE",
          "advanced": true
        },
        {
          "name": "TF_INTEG_PLAN_ENABLED",
          "description": "Set to enable separate Terraform plan job for `integration` env.",
@@ -265,6 +282,12 @@
          "default": "$TF_INIT_OPTS",
          "advanced": true
        },
        {
          "name": "TF_STAGING_WORKSPACE",
          "description": "Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) for `staging` env",
          "default": "$TF_WORKSPACE",
          "advanced": true
        },
        {
          "name": "TF_STAGING_PLAN_ENABLED",
          "description": "Set to enable separate Terraform plan job for `staging` env.",
@@ -309,6 +332,12 @@
          "default": "$TF_INIT_OPTS",
          "advanced": true
        },
        {
          "name": "TF_PROD_WORKSPACE",
          "description": "Terraform project [workspace](https://developer.hashicorp.com/terraform/language/state/workspaces) for `production` env",
          "default": "$TF_WORKSPACE",
          "advanced": true
        },
        {
          "name": "TF_PROD_PLAN_ENABLED",
          "description": "Set to enable separate Terraform plan job for `production` env.",
+28 −0
Original line number Diff line number Diff line
@@ -417,6 +417,18 @@ stages:
    terraform init -reconfigure $tf_backend_opts $(echo "$extra_opts" | envsubst_cli) $(echo "$opts" | envsubst_cli)
  }

  function tf_workspace_select() {
    workspace=$1

    # select workspace if configured
    if [[ -n "$workspace" ]]
    then
      log_info "--- Terraform workspace \\e[33;1m${workspace}\\e[0m: select"
      terraform workspace select "$workspace"
      log_info "--- Terraform workspace \\e[33;1m${workspace}\\e[0m: selected"
    fi
  }

  tf_plan() {
    opts=$1
    extra_opts=$2
@@ -574,11 +586,14 @@ stages:
.tf-commands:
  default:
    init: tf_init "${ENV_INIT_OPTS:-$TF_INIT_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}"
    select_workspace: tf_workspace_select "${ENV_WORKSPACE:-$TF_WORKSPACE}"
    plan: tf_plan "${ENV_PLAN_OPTS:-$TF_PLAN_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}" "${ENV_TYPE}.tfplan" "${ENV_TYPE}-plan.json"
    apply: tf_apply "${ENV_APPLY_OPTS:-$TF_APPLY_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}" "$tfplan"
    destroy: tf_destroy "${ENV_DESTROY_OPTS:-$TF_DESTROY_OPTS}" "${ENV_EXTRA_OPTS:-$TF_EXTRA_OPTS}"
  init:
    - !reference [ .tf-commands, default, init ]
  select_workspace:
    - !reference [ .tf-commands, default, select_workspace ]
  plan:
    - !reference [ .tf-commands, default, plan ]
  apply:
@@ -593,6 +608,7 @@ stages:
# defines default Docker image, tracking probe, cache policy
# @arg ENV_TYPE      : environment type
# @arg ENV_INIT_OPTS : environment specific tf init options
# @arg ENV_WORKSPACE : environment specific tf workspace
# @arg ENV_EXTRA_OPTS: environment specific tf extra options (all commands)
.tf-base:
  image:
@@ -610,6 +626,7 @@ stages:
    - install_ca_certs "${CUSTOM_CA_CERTS:-$DEFAULT_CA_CERTS}"
    - cd "$TF_PROJECT_DIR"
    - !reference [ .tf-commands, init ]
    - !reference [ .tf-commands, select_workspace ]
  cache:
    key: "$CI_COMMIT_REF_SLUG-terraform"
    paths:
@@ -791,6 +808,7 @@ tf-plan-review:
    ENV_TYPE: review
    ENV_EXTRA_OPTS: $TF_REVIEW_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_REVIEW_INIT_OPTS
    ENV_WORKSPACE: $TF_REVIEW_WORKSPACE
    ENV_PLAN_OPTS: $TF_REVIEW_PLAN_OPTS
  environment:
    name: review/$CI_COMMIT_REF_NAME
@@ -816,6 +834,7 @@ tf-review:
    ENV_TYPE: review
    ENV_EXTRA_OPTS: $TF_REVIEW_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_REVIEW_INIT_OPTS
    ENV_WORKSPACE: $TF_REVIEW_WORKSPACE
    ENV_APPLY_OPTS: $TF_REVIEW_APPLY_OPTS
    ENV_PLAN_ENABLED: $TF_REVIEW_PLAN_ENABLED
  environment:
@@ -845,6 +864,7 @@ tf-destroy-review:
    ENV_TYPE: review
    ENV_EXTRA_OPTS: $TF_REVIEW_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_REVIEW_INIT_OPTS
    ENV_WORKSPACE: $TF_REVIEW_WORKSPACE
    ENV_DESTROY_OPTS: $TF_REVIEW_DESTROY_OPTS
  environment:
    name: review/$CI_COMMIT_REF_NAME
@@ -869,6 +889,7 @@ tf-plan-integration:
    ENV_TYPE: integration
    ENV_EXTRA_OPTS: $TF_INTEG_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_INTEG_INIT_OPTS
    ENV_WORKSPACE: $TF_INTEG_WORKSPACE
    ENV_PLAN_OPTS: $TF_INTEG_PLAN_OPTS
  environment:
    name: integration
@@ -891,6 +912,7 @@ tf-integration:
    ENV_TYPE: integration
    ENV_EXTRA_OPTS: $TF_INTEG_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_INTEG_INIT_OPTS
    ENV_WORKSPACE: $TF_INTEG_WORKSPACE
    ENV_APPLY_OPTS: $TF_INTEG_APPLY_OPTS
    ENV_PLAN_ENABLED: $TF_INTEG_PLAN_ENABLED
  environment:
@@ -917,6 +939,7 @@ tf-destroy-integration:
    ENV_TYPE: integration
    ENV_EXTRA_OPTS: $TF_INTEG_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_INTEG_INIT_OPTS
    ENV_WORKSPACE: $TF_INTEG_WORKSPACE
    ENV_DESTROY_OPTS: $TF_INTEG_DESTROY_OPTS
  environment:
    name: integration
@@ -938,6 +961,7 @@ tf-plan-staging:
    ENV_TYPE: staging
    ENV_EXTRA_OPTS: $TF_STAGING_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_STAGING_INIT_OPTS
    ENV_WORKSPACE: $TF_STAGING_WORKSPACE
    ENV_PLAN_OPTS: $TF_STAGING_PLAN_OPTS
  environment:
    name: staging
@@ -960,6 +984,7 @@ tf-staging:
    ENV_TYPE: staging
    ENV_EXTRA_OPTS: $TF_STAGING_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_STAGING_INIT_OPTS
    ENV_WORKSPACE: $TF_STAGING_WORKSPACE
    ENV_APPLY_OPTS: $TF_STAGING_APPLY_OPTS
    ENV_PLAN_ENABLED: $TF_STAGING_PLAN_ENABLED
  environment:
@@ -986,6 +1011,7 @@ tf-destroy-staging:
    ENV_TYPE: staging
    ENV_EXTRA_OPTS: $TF_STAGING_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_STAGING_INIT_OPTS
    ENV_WORKSPACE: $TF_STAGING_WORKSPACE
    ENV_DESTROY_OPTS: $TF_STAGING_DESTROY_OPTS
  environment:
    name: staging
@@ -1007,6 +1033,7 @@ tf-plan-production:
    ENV_TYPE: production
    ENV_EXTRA_OPTS: $TF_PROD_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_PROD_INIT_OPTS
    ENV_WORKSPACE: $TF_PROD_WORKSPACE
    ENV_PLAN_OPTS: $TF_PROD_PLAN_OPTS
  environment:
    name: production
@@ -1036,6 +1063,7 @@ tf-production:
    ENV_TYPE: production
    ENV_EXTRA_OPTS: $TF_PROD_EXTRA_OPTS
    ENV_INIT_OPTS: $TF_PROD_INIT_OPTS
    ENV_WORKSPACE: $TF_PROD_WORKSPACE
    ENV_APPLY_OPTS: $TF_PROD_APPLY_OPTS
    ENV_PLAN_ENABLED: $TF_PROD_PLAN_ENABLED
  environment: