Created
November 30, 2021 14:48
-
-
Save iamcryptoki/471d3544d63182e63507593ef755caf8 to your computer and use it in GitHub Desktop.
Terraform Github Actions CI CD Pipeline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD Terraform | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
jobs: | |
terraform: | |
name: Run Terraform | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v1 | |
- name: Terraform Init | |
id: terraform-init | |
run: terraform init -backend-config="encryption_key=${{ secrets.TF_ENCRYPTION_KEY }}" | |
- name: Terraform Format | |
id: terraform-fmt | |
run: terraform fmt -check | |
- name: Terraform Plan | |
id: terraform-plan | |
if: github.event_name == 'pull_request' | |
run: terraform plan -no-color | |
continue-on-error: true | |
- name: Update Pull Request | |
uses: actions/github-script@v5 | |
if: github.event_name == 'pull_request' | |
env: | |
PLAN: "${{ steps.terraform-plan.outputs.stdout }}" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const output = `#### Terraform Format \`${{ steps.terraform-fmt.outcome }}\` | |
#### Terraform Initialization \`${{ steps.terraform-init.outcome }}\` | |
#### Terraform Plan \`${{ steps.terraform-plan.outcome }}\` | |
<details><summary>Show Plan</summary> | |
${{ env.PLAN }} | |
</details> | |
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
- name: Terraform Plan Status | |
if: steps.plan.outcome == 'failure' | |
run: exit 1 | |
- name: Terraform Apply | |
id: terraform-apply | |
if: github.event_name == 'push' | |
run: terraform apply -auto-approve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment