-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (140 loc) · 5.91 KB
/
tf-pipeline.yaml
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
name: "Terraform Plan/Apply Pipeline"
run-name: "Terraform Plan/Apply Pipeline for commit ${{ github.sha }}"
on:
pull_request:
paths:
- "infrastructure/**"
- ".github/workflows/tf-pipeline.yaml"
push:
branches:
- main
paths:
- "infrastructure/**"
- ".github/workflows/tf-pipeline.yaml"
permissions:
id-token: write
contents: read
pull-requests: write
jobs:
terraform:
name: "Terraform"
runs-on: ubuntu-latest
strategy:
matrix:
env: ["infrastructure"]
defaults:
run:
shell: bash
working-directory: ${{matrix.env}}
env:
TF_VAR_azure_backend_rg: ${{ secrets.AZURE_TF_RESOURCE_GROUP }}
TF_VAR_azure_backend_sa: ${{ secrets.AZURE_TF_STORAGE_ACCOUNT_NAME }}
TF_VAR_azure_backend_container: ${{ secrets.AZURE_TF_CONTAINER_NAME }}
GITHUB_TOKEN: ${{ secrets.TF_GITHUB_TOKEN }}
GITHUB_OWNER: ${{ github.repository_owner }}
steps:
- name: Checkout
uses: actions/[email protected]
- name: Azure Authentication
id: login
uses: azure/[email protected]
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: JSON Parse
id: parse
env:
AZJSON: ${{ secrets.AZURE_CREDENTIALS }}
run: |
ARM_CLIENT_ID=$(echo $AZJSON | jq -r '.["clientId"]')
ARM_CLIENT_SECRET=$(echo $AZJSON | jq -r '.["clientSecret"]')
ARM_TENANT_ID=$(echo $AZJSON | jq -r '.["tenantId"]')
ARM_SUBSCRIPTION_ID=$(echo $AZJSON | jq -r '.["subscriptionId"]')
echo ARM_CLIENT_ID=$ARM_CLIENT_ID >> $GITHUB_ENV
echo ARM_CLIENT_SECRET=$ARM_CLIENT_SECRET >> $GITHUB_ENV
echo ARM_TENANT_ID=$ARM_TENANT_ID >> $GITHUB_ENV
echo ARM_SUBSCRIPTION_ID=$ARM_SUBSCRIPTION_ID >> $GITHUB_ENV
- name: GitHub Token
id: token
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "machine github.com login x password ${TOKEN}" > ~/.netrc
git config --global url."https://github.com/".insteadOf "git://github.com/"
git config --global advice.detachedHead false
- name: Install Terraform
uses: hashicorp/[email protected]
with:
terraform_wrapper: false
- name: Terraform Init
id: init
run: |
terraform init -backend-config="resource_group_name=${TF_VAR_azure_backend_rg}" -backend-config="storage_account_name=${TF_VAR_azure_backend_sa}" -backend-config="container_name=${TF_VAR_azure_backend_container}"
- name: Install Checkov
id: checkov
if: github.event_name == 'pull_request'
run: |
pip install checkov
- name: Checkov Static Test
id: static
if: github.event_name == 'pull_request'
run: |
checkov -d . --download-external-modules true
- name: Terraform Format
id: fmt
run: terraform fmt -check -recursive
continue-on-error: true
- name: Terraform Validate
id: validate
run: terraform validate -no-color
- name: Terraform Plan
id: tplan
run: |
plan_output=$(terraform plan -no-color)
echo "$plan_output"
echo "plan<<EOF" >> $GITHUB_OUTPUT
echo "$plan_output" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Checkov Plan Test
id: cplan
if: github.event_name == 'pull_request'
run: |
echo Disable until we have a self-hosted runner
# terraform plan --out plan.tfplan
# terraform show -json plan.tfplan > tfplan.json
# ls
# checkov -f tfplan.json --framework terraform_plan
- name: Pull Request Comment
id: comment
uses: actions/[email protected]
if: github.event_name == 'pull_request'
env:
TPLAN: "terraform\n${{ steps.tplan.outputs.plan }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `
### Pull Request Information
Please review this pull request. Merging the PR will run Terraform Apply with the plan detailed below.
#### Terraform Checks
Init: \`${{ steps.init.outcome }}\`
Format: \`${{ steps.fmt.outcome }}\`
Validation: \`${{ steps.validate.outcome }}\`
Plan: \`${{ steps.tplan.outcome }}\`
#### Checkov
Static: \`${{ steps.static.outcome }}\`
Plan: \`${{ steps.cplan.outcome }}\`
<details>
<summary>Plan File</summary>
\`\`\`${process.env.TPLAN}\`\`\`
</details>
`
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
- name: Terraform Apply
id: apply
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
run: terraform apply -auto-approve