Skip to content

Instantly share code, notes, and snippets.

@akmalharith
Created February 17, 2023 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akmalharith/a1d890a8c966880886ebeb08be2d7e8b to your computer and use it in GitHub Desktop.
Save akmalharith/a1d890a8c966880886ebeb08be2d7e8b to your computer and use it in GitHub Desktop.
Bitbucket pipelines to run Terraform, with YAML anchors as DRY practice
options:
docker: true
definitions:
commonItems:
- &getOidcToken export AWS_WEB_IDENTITY_TOKEN_FILE=$(pwd)/web-identity-token && echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/web-identity-token
- &terraformInit set -x && terraform -chdir=$TF_WORKDIR init
-backend-config="bucket=$TFSTATE_BUCKET"
-backend-config="key=$TFSTATE_KEY"
-backend-config="region=$TFSTATE_REGION" &&
set +x
- &terraformValidate terraform -chdir=$TF_WORKDIR validate
- &terraformPlan set -x &&
terraform -chdir=$TF_WORKDIR plan
-compact-warnings &&
set +x
- &terraformApplyAutoApprove set -x &&
terraform -chdir=$TF_WORKDIR apply -auto-approve
-compact-warnings &&
set +x
- &terraformDestroyAutoApprove set -x &&
terraform -chdir=$TF_WORKDIR destroy -auto-approve
-compact-warnings &&
set +x
image: hashicorp/terraform:1.3.7
pipelines:
branches:
'develop':
- step:
oidc: true
script:
- *getOidcToken
- *terraformInit
- *terraformValidate
- *terraformPlan
# - *terraformApplyAutoApprove
custom:
destroy:
- step:
oidc: true
script:
- *getOidcToken
- *terraformInit
- step:
oidc: true
script:
- *getOidcToken
- *terraformInit
- *terraformDestroyAutoApprove
trigger: manual
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment