Skip to content

Instantly share code, notes, and snippets.

@nicksantamaria
Created July 9, 2020 01:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicksantamaria/67dc8484bce431431b29eba89d1bc1e0 to your computer and use it in GitHub Desktop.
Save nicksantamaria/67dc8484bce431431b29eba89d1bc1e0 to your computer and use it in GitHub Desktop.
Terraform CircleCI Workflow
version: 2
jobs:
plan:
docker:
- image: hashicorp/terraform:latest
working_directory: /code
steps:
- checkout
- run:
name: Init
command: terraform init -input=false
- run:
name: Plan
command: terraform plan -input=false -out=tfplan -no-color
- run:
name: Render plan for PR comment
command: terraform show -no-color tfplan > tfplan.txt
- persist_to_workspace:
root: /code
paths:
- .
apply:
docker:
- image: hashicorp/terraform:latest
working_directory: /code
steps:
- attach_workspace:
at: /code
- run:
name: Apply
command: terraform apply tfplan
plan_comment:
docker:
- image: cloudposse/github-commenter:latest
working_directory: /code
steps:
- checkout
- attach_workspace:
at: /code
- run:
name: Post plan to PR comment
command: |-
export CIRCLE_PR_NUMBER=${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}
if [ -z $CIRCLE_PR_NUMBER ]; then echo "Not a pull request - aborting"; exit 0; fi
cat /code/tfplan.txt | github-commenter \
-owner ${CIRCLE_PROJECT_USERNAME} \
-repo ${CIRCLE_PROJECT_REPONAME} \
-number $CIRCLE_PR_NUMBER \
-delete-comment-regex "Output from" \
-type pr \
-template_file /code/.circleci/plan-comment.tpl
workflows:
version: 2
plan_pr:
jobs:
- plan
- plan_comment:
requires:
- plan
filters:
branches:
ignore: master
- apply:
requires:
- plan
filters:
branches:
only: master
Output from `terraform plan`
```
{{.}}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment