FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 81216cc6 authored by Roy Harrington's avatar Roy Harrington
Browse files

Merge branch 'tf-pipeline' into 'master'

fix: use TF_WORKSPACE env var to select workspace

Closes #41

See merge request !43
parents a9d51d83 6cd72f62
No related branches found
Tags v2.1.1
1 merge request!43fix: use TF_WORKSPACE env var to select workspace
......@@ -23,8 +23,9 @@
#
# Tests/linting
#
# This template includes four jobs in the test stage, tf-validate, tf-fmt, tf-lint, and tf-sec. These jobs only run
# if a merge request is open. For more info see the comments alongside each of these job definitions below.
# This template includes four jobs in the test stage, tf-validate, tf-fmt, tf-lint, and tf-sec. These jobs run
# on every commit pushed or if a merge request is open. For more info see the comments alongside each of these
# job definitions below.
#
# Terraform plan
#
......@@ -72,10 +73,11 @@ stages:
.tf-plan:
stage: plan
image: ${TERRAFORM_DEPLOY_IMAGE}:${TERRAFORM_DEPLOY_VERSION}
variables:
TF_WORKSPACE: $DEPLOYMENT_ENVIRONMENT
script: |
unset GOOGLE_APPLICATION_CREDENTIALS
terraform init
terraform workspace select ${DEPLOYMENT_ENVIRONMENT}
terraform plan -out=${TF_DATA_DIR}/${CI_COMMIT_REF_SLUG}-${DEPLOYMENT_ENVIRONMENT}.tfplan -detailed-exitcode || exit_code=$?
terraform show -json ${TF_DATA_DIR}/${CI_COMMIT_REF_SLUG}-${DEPLOYMENT_ENVIRONMENT}.tfplan > ${TF_DATA_DIR}/${CI_COMMIT_REF_SLUG}-${DEPLOYMENT_ENVIRONMENT}.tfplan.json
cat ${TF_DATA_DIR}/${CI_COMMIT_REF_SLUG}-${DEPLOYMENT_ENVIRONMENT}.tfplan.json | jq -r '([.resource_changes[]?.change.actions?]|flatten)|{"create":(map(select(.=="create"))|length),"update":(map(select(.=="update"))|length),"delete":(map(select(.=="delete"))|length)}' > ${TF_DATA_DIR}/${CI_COMMIT_REF_SLUG}-${DEPLOYMENT_ENVIRONMENT}.tfplan.report
......@@ -103,10 +105,11 @@ stages:
stage: apply
environment: ${DEPLOYMENT_ENVIRONMENT}
image: ${TERRAFORM_DEPLOY_IMAGE}:${TERRAFORM_DEPLOY_VERSION}
variables:
TF_WORKSPACE: $DEPLOYMENT_ENVIRONMENT
script: |
unset GOOGLE_APPLICATION_CREDENTIALS
terraform init
terraform workspace select ${DEPLOYMENT_ENVIRONMENT}
terraform apply ${TF_DATA_DIR}/${CI_COMMIT_REF_SLUG}-${DEPLOYMENT_ENVIRONMENT}.tfplan
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $DEPLOYMENT_ENVIRONMENT == "staging"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment