|
| 1 | +# This workflow installs the latest version of Terraform CLI and configures the Terraform CLI configuration file |
| 2 | +# with an API token for Terraform Cloud (app.terraform.io). On pull request events, this workflow will run |
| 3 | +# `terraform init`, `terraform fmt`, and `terraform plan` (speculative plan via Terraform Cloud). On push events |
| 4 | +# to the "master" branch, `terraform apply` will be executed. |
| 5 | +# |
| 6 | +# Documentation for `hashicorp/setup-terraform` is located here: https://github.com/hashicorp/setup-terraform |
| 7 | +# |
| 8 | +# To use this workflow, you will need to complete the following setup steps. |
| 9 | +# |
| 10 | +# 1. Create a `main.tf` file in the root of this repository with the `remote` backend and one or more resources defined. |
| 11 | +# Example `main.tf`: |
| 12 | +# # The configuration for the `remote` backend. |
| 13 | +# terraform { |
| 14 | +# backend "remote" { |
| 15 | +# # The name of your Terraform Cloud organization. |
| 16 | +# organization = "example-organization" |
| 17 | +# |
| 18 | +# # The name of the Terraform Cloud workspace to store Terraform state files in. |
| 19 | +# workspaces { |
| 20 | +# name = "example-workspace" |
| 21 | +# } |
| 22 | +# } |
| 23 | +# } |
| 24 | +# |
| 25 | +# # An example resource that does nothing. |
| 26 | +# resource "null_resource" "example" { |
| 27 | +# triggers = { |
| 28 | +# value = "A example resource that does nothing!" |
| 29 | +# } |
| 30 | +# } |
| 31 | +# |
| 32 | +# |
| 33 | +# 2. Generate a Terraform Cloud user API token and store it as a GitHub secret (e.g. TF_API_TOKEN) on this repository. |
| 34 | +# Documentation: |
| 35 | +# - https://www.terraform.io/docs/cloud/users-teams-organizations/api-tokens.html |
| 36 | +# - https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets |
| 37 | +# |
| 38 | +# 3. Reference the GitHub secret in step using the `hashicorp/setup-terraform` GitHub Action. |
| 39 | +# Example: |
| 40 | +# - name: Setup Terraform |
| 41 | +# uses: hashicorp/setup-terraform@v1 |
| 42 | +# with: |
| 43 | +# cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} |
| 44 | + |
| 45 | +name: 'Terraform' |
| 46 | + |
| 47 | +on: |
| 48 | + push: |
| 49 | + branches: [ "master" ] |
| 50 | + pull_request: |
| 51 | + |
| 52 | +permissions: |
| 53 | + contents: read |
| 54 | + |
| 55 | +jobs: |
| 56 | + terraform: |
| 57 | + name: 'Terraform' |
| 58 | + runs-on: ubuntu-latest |
| 59 | + environment: production |
| 60 | + |
| 61 | + # Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest |
| 62 | + defaults: |
| 63 | + run: |
| 64 | + shell: bash |
| 65 | + |
| 66 | + steps: |
| 67 | + # Checkout the repository to the GitHub Actions runner |
| 68 | + - name: Checkout |
| 69 | + uses: actions/checkout@v4 |
| 70 | + |
| 71 | + # Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token |
| 72 | + - name: Setup Terraform |
| 73 | + uses: hashicorp/setup-terraform@v1 |
| 74 | + with: |
| 75 | + cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }} |
| 76 | + |
| 77 | + # Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. |
| 78 | + - name: Terraform Init |
| 79 | + run: terraform init |
| 80 | + |
| 81 | + # Checks that all Terraform configuration files adhere to a canonical format |
| 82 | + - name: Terraform Format |
| 83 | + run: terraform fmt -check |
| 84 | + |
| 85 | + # Generates an execution plan for Terraform |
| 86 | + - name: Terraform Plan |
| 87 | + run: terraform plan -input=false |
| 88 | + |
| 89 | + # On push to "master", build or change infrastructure according to Terraform configuration files |
| 90 | + # 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 |
| 91 | + - name: Terraform Apply |
| 92 | + if: github.ref == 'refs/heads/"master"' && github.event_name == 'push' |
| 93 | + run: terraform apply -auto-approve -input=false |
0 commit comments