-
Notifications
You must be signed in to change notification settings - Fork 2
287 lines (251 loc) · 9.04 KB
/
ci.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
name: Run AWS Spec Tests
on:
pull_request:
branches:
- main
push:
branches:
- main
workflow_call:
inputs:
test_type:
description: 'Type of test: terraform or cloudformation'
required: true
type: 'string'
code_sha:
description: 'SHA of the terraform or cloudformation code to checkout'
required: true
type: 'string'
secrets:
OBSERVE_CUSTOMER:
required: true
OBSERVE_TOKEN:
required: true
OBSERVE_DOMAIN:
required: false
env:
USER: gha-${{ github.run_id }}
AWS_TEST_KITCHEN_REPO: aws-test-kitchen
jobs:
permission_check:
runs-on: ubuntu-latest
outputs:
can-write: ${{ steps.check.outputs.can-write }}
env:
OBSERVE_CUSTOMER: ${{ secrets.OBSERVE_CUSTOMER }}
steps:
- id: check
run: |
# If the OBSERVE_CUSTOMER secret is MIA we can't run tests
if [[ -z "$OBSERVE_CUSTOMER" ]]; then
echo "can-write=false" >> $GITHUB_OUTPUT
else
echo "can-write=true" >> $GITHUB_OUTPUT
fi
build:
runs-on: ubuntu-latest
outputs:
image_name: ${{ steps.set_output_image_name.outputs.image_name }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
repository: ${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}
submodules: true
- name: Login to GitHub Container Registry
if: ${{ inputs.test_type == '' }}
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
if: ${{ inputs.test_type == '' }}
uses: docker/setup-buildx-action@v2
- name: Metadata for Docker Image
id: docker_metadata
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}
tags: |
type=sha
type=ref,event=branch,priority=1001
type=raw,value=latest,enable={{is_default_branch}}
- name: Set IMAGE_NAME environment variable
id: set_output_image_name
run: |
if [[ "${{ inputs.test_type }}" != "" ]]; then
IMAGE_NAME="ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:main"
else
IMAGE_NAME=$(echo "${DOCKER_METADATA_OUTPUT_TAGS}" | head -n 1)
fi
echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV
echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_OUTPUT
- name: Build docker image
if: ${{ inputs.test_type == '' }}
uses: docker/build-push-action@v4
with:
context: .
load: false
push: false
tags: ${{ steps.docker_metadata.outputs.tags }}
labels: ${{ steps.docker_metadata.outputs.labels }}
cache-from: |
type=gha
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:latest
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:main
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:${{ github.ref_name }}
cache-to: type=gha,mode=max
determine-provider:
runs-on: ubuntu-latest
outputs:
providers: ${{steps.set-providers.outputs.providers}}
steps:
- id: set-providers
run: |
if [[ "${{ github.event_name }}" == "pull_request" || "${{ github.event_name }}" == "push" ]]; then
echo "providers=[\"cloudformation\", \"terraform\"]" >> $GITHUB_ENV
echo "providers=[\"cloudformation\", \"terraform\"]" >> $GITHUB_OUTPUT
else
echo "providers=[\"${{ github.event.inputs.test_type }}\"]" >> $GITHUB_ENV
echo "providers=[\"${{ github.event.inputs.test_type }}\"]" >> $GITHUB_OUTPUT
fi
test:
runs-on: ubuntu-latest
needs: [permission_check, build, determine-provider]
if: needs.permission_check.outputs.can-write == 'true'
name: test ${{ matrix.provider }}
permissions:
contents: write
id-token: write
env:
OBSERVE_CUSTOMER: ${{ secrets.OBSERVE_CUSTOMER }}
OBSERVE_TOKEN: ${{ secrets.OBSERVE_TOKEN }}
OBSERVE_DOMAIN: ${{ secrets.OBSERVE_DOMAIN }}
IMAGE_NAME: ${{ needs.build.outputs.image_name }}
strategy:
matrix:
provider: ${{fromJson(needs.determine-provider.outputs.providers)}}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
repository: ${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}
submodules: true
- name: Checkout Terraform SHA
if: matrix.provider == 'terraform' && github.event.inputs.terraform_sha != ''
run: |
cd terraform-aws-collection
git checkout ${{ github.event.inputs.terraform_sha }}
- name: Checkout CloudFormation SHA
if: matrix.provider == 'cloudformation' && github.event.inputs.cloudformation_sha != ''
run: |
cd cloudformation-aws-collection
git checkout ${{ github.event.inputs.cloudformation_sha }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Metadata for Docker Image
id: docker_metadata
if: ${{ inputs.test_type == '' }}
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}
tags: |
type=sha
type=ref,event=branch,priority=1001
type=raw,value=latest,enable={{is_default_branch}}
- name: Build docker image (from cache)
if: ${{ inputs.test_type == '' }}
uses: docker/build-push-action@v4
with:
context: .
load: true
push: false
tags: ${{ steps.docker_metadata.outputs.tags }}
labels: ${{ steps.docker_metadata.outputs.labels }}
cache-from: |
type=gha
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:latest
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:main
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:${{ github.ref_name }}
cache-to: type=gha,mode=max
- name: Pull Docker image
if: ${{ inputs.test_type != '' }}
run: docker pull $IMAGE_NAME
- name: Check AWS Quota
run: |
attempts=0
until make check_aws_quota; do
if [ $attempts -ge 10 ]; then
echo "Exceeded maximum retries for quota check."
exit 1
fi
echo "Failed to meet quota. Retrying in 2 minutes..."
sleep 120
attempts=$((attempts + 1))
done
- name: Setup AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: ${{ secrets.AWS_ROLE_ARN }}
aws-region: us-west-2
- name: Setup Infra and Run spec tests
run: make docker/test
env:
PROVIDER: ${{ matrix.provider }}
- name: Teardown test infrastructure
if: always()
continue-on-error: true
run: make docker/clean
env:
PROVIDER: ${{ matrix.provider }}
push:
runs-on: ubuntu-latest
needs: [permission_check, build, test]
if: ${{ inputs.test_type == '' }} && needs.permission_check.outputs.can-write == 'true' && github.event_name == 'push' && github.ref == 'refs/heads/main'
permissions:
contents: write
packages: write
steps:
- name: Checkout
uses: actions/checkout@v3
with:
repository: ${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}
submodules: true
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Metadata for Docker Image
id: docker_metadata
uses: docker/metadata-action@v4
with:
images: ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}
tags: |
type=sha
type=ref,event=branch,priority=1001
type=raw,value=latest,enable={{is_default_branch}}
- name: Push Docker images
if: success()
uses: docker/build-push-action@v4
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.docker_metadata.outputs.tags }}
labels: ${{ steps.docker_metadata.outputs.labels }}
cache-from: |
type=gha
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:latest
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:main
ghcr.io/${{ github.repository_owner }}/${{ env.AWS_TEST_KITCHEN_REPO }}:${{ github.ref_name }}
cache-to: type=gha,mode=max