Skip to content

Commit 5f6fd1e

Browse files
author
Florencia Comuzzi
committed
add env
1 parent e4ebf38 commit 5f6fd1e

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

.github/workflows/terraform-plan.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
TF_API_TOKEN: "${{ secrets.TF_API_TOKEN }}"
77
TF_WORKSPACE: "production" # template field
88
CONFIG_DIRECTORY: "./"
9-
TF_ENVIRONMENT: ${{ github.base_ref }}
9+
TARGET_BRANCH: ${{ github.base_ref }}
1010
jobs:
1111
tflint:
1212
name: "TFLint"
@@ -20,8 +20,8 @@ jobs:
2020
- uses: actions/checkout@v4
2121
name: Checkout source code
2222

23-
- name: Show ENVIRONMENT
24-
run: echo $TF_ENVIRONMENT
23+
- name: Show TARGET_BRANCH
24+
run: echo $TARGET_BRANCH
2525

2626
- uses: actions/cache@v4
2727
name: Cache plugin dir
@@ -46,7 +46,6 @@ jobs:
4646
- name: Run TFLint
4747
run: tflint -f compact
4848
terraform:
49-
if: github.repository != 'hashicorp-education/learn-terraform-github-actions'
5049
name: "Terraform Plan"
5150
runs-on: ubuntu-latest
5251
permissions:

.github/workflows/terraform.yml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
name: "Terraform Infrastructure Change Management Pipeline with GitHub Actions"
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
paths:
9+
- terraform/**
10+
pull_request:
11+
branches:
12+
- main
13+
- develop
14+
paths:
15+
- terraform/**
16+
17+
env:
18+
# verbosity setting for Terraform logs
19+
TF_LOG: INFO
20+
# # Credentials for deployment to AWS
21+
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
22+
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
23+
# # S3 bucket for the Terraform state
24+
# BUCKET_TF_STATE: ${{ secrets.BUCKET_TF_STATE}}
25+
26+
jobs:
27+
terraform:
28+
name: "Terraform Infrastructure Change Management"
29+
runs-on: ubuntu-latest
30+
defaults:
31+
run:
32+
shell: bash
33+
# We keep Terraform files in the terraform directory.
34+
working-directory: ./terraform
35+
36+
steps:
37+
- name: Checkout the repository to the runner
38+
uses: actions/checkout@v2
39+
40+
- name: Setup Terraform with specified version on the runner
41+
uses: hashicorp/setup-terraform@v2
42+
with:
43+
terraform_version: 1.3.0
44+
45+
- name: Terraform init [pull_request]
46+
id: init-dev
47+
if: github.event_name == 'pull_request'
48+
with:
49+
script: |
50+
if [ ${{ github.event.pull_request.base.ref }} == "develop" ]; then
51+
ENV = "dev";
52+
elif [ ${{ github.event.pull_request.base.ref }} == "main" ]; then
53+
ENV = "prod";
54+
else
55+
echo "unsupported environment";
56+
fi
57+
terraform init -upgrade -backend-config=backend/${ENV}.tfvars --reconfigure || exit 1
58+
59+
- name: Terraform format
60+
id: fmt
61+
run: terraform fmt -check
62+
63+
- name: Terraform validate
64+
id: validate
65+
run: terraform validate
66+
67+
- name: Terraform plan
68+
id: plan
69+
if: github.event_name == 'pull_request'
70+
run: terraform plan -no-color -input=false
71+
continue-on-error: true
72+
73+
- uses: actions/github-script@v6
74+
if: github.event_name == 'pull_request'
75+
env:
76+
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
77+
with:
78+
script: |
79+
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
80+
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
81+
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
82+
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
83+
84+
<details><summary>Show Plan</summary>
85+
86+
\`\`\`\n
87+
${process.env.PLAN}
88+
\`\`\`
89+
90+
</details>
91+
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
92+
93+
github.rest.issues.createComment({
94+
issue_number: context.issue.number,
95+
owner: context.repo.owner,
96+
repo: context.repo.repo,
97+
body: output
98+
})
99+
100+
- name: Terraform Plan Status
101+
if: steps.plan.outcome == 'failure'
102+
run: exit 1
103+
104+
- name: Terraform Apply
105+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
106+
run: terraform apply -auto-approve -input=false

0 commit comments

Comments
 (0)