forked from tjcorr/tf-pipeline-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
115 lines (97 loc) · 3.55 KB
/
tf-drift.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
name: 'Terraform Configuration Drift Check'
on:
schedule:
- cron: '0 4 * * *' # runs nightly at 4 am
#Special permissions required for OIDC authentication
permissions:
id-token: write
contents: read
issues: write
#These environment variables are used by the terraform azure provider to setup OIDD authenticate.
env:
ARM_CLIENT_ID: "${{ secrets.AZURE_CLIENT_ID }}"
ARM_SUBSCRIPTION_ID: "${{ secrets.AZURE_SUBSCRIPTION_ID }}"
ARM_TENANT_ID: "${{ secrets.AZURE_TENANT_ID }}"
jobs:
terraform-plan:
name: 'Terraform Plan'
runs-on: ubuntu-latest
environment: production-readonly
env:
#this is needed since we are running terraform with read-only permissions
ARM_SKIP_PROVIDER_REGISTRATION: true
outputs:
tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }}
steps:
# Checkout the repository to the GitHub Actions runner
- name: Checkout
uses: actions/checkout@v2
# Install the latest version of the Terraform CLI
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_wrapper: false
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
- name: Terraform Init
run: terraform init
# Generates an execution plan for Terraform
# An exit code of 0 indicated no changes, 1 a terraform failure, 2 there are pending changes.
- name: Terraform Plan
id: tf-plan
run: |
export exitcode=0
terraform plan -detailed-exitcode -no-color -out tfplan || export exitcode=$?
echo "::set-output name=exitcode::$exitcode"
if [ $exitcode -eq 1 ]; then
echo Terraform Plan Failed!
exit 1
else
exit 0
fi
# Save plan to artifacts
- name: Publish Terraform Plan
uses: actions/upload-artifact@v2
with:
name: tfplan
path: tfplan
# Create string output of Terraform Plan
- name: Create String Output
id: tf-plan-string
run: |
TERRAFORM_PLAN=$(terraform show -no-color tfplan)
echo "## Terraform Plan Output" >> tf.string
echo "<details><summary>Click to expand</summary>" >> tf.string
echo "" >> tf.string
echo '```terraform' >> tf.string
echo "$TERRAFORM_PLAN" >> tf.string
echo '```' >> tf.string
echo "</details>" >> tf.string
SUMMARY=$(cat tf.string)
SUMMARY="${SUMMARY//'%'/'%25'}"
SUMMARY="${SUMMARY//$'\n'/'%0A'}"
SUMMARY="${SUMMARY//$'\r'/'%0D'}"
echo "::set-output name=summary::$SUMMARY"
# Publish Terraform Plan as task summary
- name: Publish Terraform Plan to Task Summary
run: |
cat tf.string >> $GITHUB_STEP_SUMMARY
# If changes are detected, create a new issue
- name: Publish Drift Report
if: steps.tf-plan.outputs.exitcode == 2
uses: actions/github-script@v2
env:
SUMMARY: "${{ steps.tf-plan-string.outputs.summary }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const body = `${process.env.SUMMARY}`;
github.rest.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Terraform Configuration Drift Detected",
body: body
})
# Mark the workflow as failed if drift detected
- name: Error on Failure
if: steps.tf-plan.outputs.exitcode == 2
run: exit 1