-
Notifications
You must be signed in to change notification settings - Fork 2
174 lines (143 loc) · 5.43 KB
/
pr.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
## When copying this to a new ORC repository...
##
## 1) Visit https://github.com/nhsconnect/<repo>/settings/environments and create environments:
## - dev
## - test
## - test-apply
## - pre-prod
## - pre-prod-apply
## - prod
## - prod-apply
##
## 2) For each environment, add the following secrets:
## - IAM_ROLE
## - TF_BACKEND_BUCKET
## - TF_BACKEND_DYNAMODB_TABLE
## - TF_BACKEND_KEY
##
## 3) Create the following repository secret (https://github.com/nhsconnect/<repo>/settings/secrets/actions):
## - ECR_REPOSITORY_NAME
##
## 4) Edit the ## REPOSITORY SPECIFIC ## section below.
name: PR Checks
on:
pull_request:
branches:
- main
permissions:
contents: read # Required for actions/checkout
id-token: write # Required for requesting the JWT
pull-requests: write # Required to write comments
jobs:
eslint:
runs-on: ubuntu-latest
steps:
- name: Set up git repo
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 'latest'
- run: npm ci
- name: eslint
run: npm run lint
plan:
strategy:
matrix:
environment: [dev, test, pre-prod, prod]
name: Terraform Plan (${{ matrix.environment }})
runs-on: ubuntu-latest
environment: ${{ matrix.environment }}
defaults:
run:
working-directory: ./terraform
steps:
- name: Set up git repo
uses: actions/checkout@v4
- name: Set up Terraform
uses: hashicorp/setup-terraform@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.IAM_ROLE }}
aws-region: eu-west-2
- name: terraform fmt
id: fmt
working-directory: ./
run: terraform fmt -recursive -check
- name: terraform init
id: init
run: terraform init -no-color -upgrade -backend-config="bucket=${{ secrets.TF_BACKEND_BUCKET }}" -backend-config="key=${{ secrets.TF_BACKEND_KEY }}" -backend-config="dynamodb_table=${{ secrets.TF_BACKEND_DYNAMODB_TABLE }}"
- name: terraform validate
id: validate
run: terraform validate -no-color
## REPOSITORY SPECIFIC ##
- name: Setup Terraform variables
id: vars
run: |-
IMAGE_TAG=$(aws ecr describe-images --repository-name ${{ secrets.ECR_REPOSITORY_NAME }} --query 'sort_by(imageDetails,& imagePushedAt)[-1].imageTags[0]')
cat > pipeline.auto.tfvars <<EOF
task_image_tag = $IMAGE_TAG
EOF
- name: terraform plan
id: plan
run: |
terraform plan -var-file="${{ matrix.environment }}.tfvars" -no-color -out=tfplan
terraform show -no-color tfplan > tfplan.txt
echo "summary=$(grep -E 'Plan: [0-9]+ to add, [0-9]+ to change, [0-9]+ to destroy\.|No changes\. Your infrastructure matches the configuration\.' tfplan.txt | sed 's/.*No changes\. Your infrastructure matches the configuration/Plan: no changes/g' | sed 's/.*Plan: //g' | sed 's/\..*//g')" >> $GITHUB_OUTPUT
continue-on-error: true
- name: Add PR comment
uses: actions/github-script@v7
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// 1. Retrieve existing bot comments for the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Report for environment: ${{ matrix.environment }}')
})
// 2. Prepare format of the comment
const output = `### Report for environment: ${{ matrix.environment }}
#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
<details><summary>Format Output</summary>
\`\`\`\n
${{ steps.fmt.outputs.stdout }}
\`\`\`
</details>
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
<details><summary>Initialization Output</summary>
\`\`\`\n
${{ steps.init.outputs.stdout }}
\`\`\`
</details>
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
<details><summary>Validation Output</summary>
\`\`\`\n
${{ steps.validate.outputs.stdout }}
\`\`\`
</details>
#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan (${{ steps.plan.outputs.summary }})</summary>
\`\`\`\n
${{ steps.plan.outputs.stdout }}
\`\`\`
</details>`;
// 3. If we have a comment, update it, otherwise create a new one
if (botComment) {
github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})