Skip to content

Commit 1966041

Browse files
Create terraform.yml
1 parent afb2882 commit 1966041

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

.github/workflows/terraform.yml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
75+
- name: Configure AWS credentials
76+
uses: aws-actions/configure-aws-credentials@v4
77+
with:
78+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
79+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
80+
aws-region: "eu-east-1"
81+
82+
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
83+
- name: Terraform Init
84+
run: terraform init
85+
86+
# Checks that all Terraform configuration files adhere to a canonical format
87+
- name: Terraform Format
88+
run: terraform fmt -check
89+
90+
# Generates an execution plan for Terraform
91+
- name: Terraform Plan
92+
run: terraform plan -input=false
93+
94+
# On push to "master", build or change infrastructure according to Terraform configuration files
95+
# 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
96+
- name: Terraform Apply
97+
if: github.ref == 'refs/heads/"master"' && github.event_name == 'push'
98+
run: terraform apply -auto-approve -input=false

0 commit comments

Comments
 (0)