Created
June 30, 2021 07:56
-
-
Save ajinkya101/3dc2e978c17c20bfb7950724c3684daf to your computer and use it in GitHub Desktop.
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
# This workflow installs the latest version of Terraform CLI and configures the Terraform CLI configuration file | |
# with an API token for Terraform Cloud (app.terraform.io). On pull request events, this workflow will run | |
# `terraform init`, `terraform fmt`, and `terraform plan` (speculative plan via Terraform Cloud). On push events | |
# to the main branch, `terraform apply` will be executed. | |
# | |
# Documentation for `hashicorp/setup-terraform` is located here: https://github.com/hashicorp/setup-terraform | |
name: 'Terraform' | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
jobs: | |
terraform: | |
name: 'Terraform' | |
env: | |
# Azure Authentication Credentials are stored in the GitHub Secrets. | |
ARM_CLIENT_ID: ${{ secrets.AZURE_AD_CLIENT_ID }} | |
ARM_CLIENT_SECRET: ${{ secrets.AZURE_AD_CLIENT_SECRET }} | |
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
ARM_TENANT_ID: ${{ secrets.AZURE_AD_TENANT_ID }} | |
runs-on: ubuntu-latest | |
environment: dev | |
# Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest | |
defaults: | |
run: | |
shell: bash | |
steps: | |
# Checkout the repository to the GitHub Actions runner | |
- name: Checkout | |
uses: actions/checkout@v2 | |
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v1 | |
with: | |
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} | |
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. | |
- name: Terraform Init | |
id: init | |
run: terraform init | |
# Terraform fmt used to rewrite Terraform configuration files to a canonical format and style. | |
- name: Terraform Format | |
id: fmt | |
run: terraform fmt | |
# The terraform validate command validates the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs. | |
- name: Terraform Validate | |
id: validate | |
run: terraform validate -no-color | |
# Generates an execution plan for Terraform | |
- name: Terraform Plan | |
id: plan | |
run: terraform plan -no-color -out plan.tfplan | |
continue-on-error: true | |
- uses: actions/[email protected] | |
if: github.event_name == 'pull_request' | |
env: | |
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const output = `#### Terraform Format and Style π\`${{ steps.fmt.outcome }}\` | |
#### Terraform Initialization βοΈ\`${{ steps.init.outcome }}\` | |
#### Terraform Validate π€\`${{ steps.validate.outcome }}\` | |
#### Terraform Plan π\`${{ steps.plan.outcome }}\` | |
<details><summary>Show Plan</summary> | |
\`\`\`\n | |
${process.env.PLAN} | |
\`\`\` | |
</details> | |
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; | |
github.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: output | |
}) | |
# Plan file is used by Infracost for Cost estimates | |
- name: "Terraform show" | |
id: show | |
run: terraform show -json plan.tfplan | |
- name: "Save Plan JSON" | |
run: echo '${{ steps.show.outputs.stdout }}' > plan.json # Do not change | |
- name: Run infracost diff | |
uses: infracost/infracost-gh-action@master # Use a specific version instead of master if locking is preferred | |
env: | |
INFRACOST_API_KEY: ${{ secrets.INFRACOST_API_KEY }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
entrypoint: /scripts/ci/diff.sh # Do not change | |
path: plan.json # Do not change | |
- name: Terraform Plan Status | |
if: steps.plan.outcome == 'failure' | |
run: exit 1 | |
# On push to main, build or change infrastructure according to Terraform configuration files | |
# Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks | |
- name: Terraform Apply | |
if: github.ref == 'refs/heads/main' && 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