-
Notifications
You must be signed in to change notification settings - Fork 0
289 lines (253 loc) · 8.13 KB
/
main.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
288
289
name: Main
on:
push:
branches:
- main
env:
FAKE_KUBECONFIG: |
apiVersion: v1
clusters:
- name: "my-company"
cluster:
certificate-authority-data: ABC123
server: https://some-cloud-provider.io:6443
contexts:
- name: admin@my-company
context:
cluster: "my-company"
user: my-company-admin
current-context: admin@my-company
kind: Config
preferences: {}
users:
- name: my-company-admin
user:
token: abc123
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3
- uses: pre-commit/[email protected]
advanced-test:
runs-on: ubuntu-latest
env:
KUSTOMIZATION_BASE_DIR: kustomize/base
KUSTOMIZATION_DIR: kustomize/overlays/staging
AGE_TEST_PUBLIC_KEY: age1l8paxjj9hd37wu24prsl24nux9ht30690r4lx3tk6hlrdv9rl4cqnm0th6
AGE_TEST_SECRET_KEY: AGE-SECRET-KEY-1CDL85PSFWFMMTNZMUZWL6Q4DJYFRGPHX6J7TCMZZURHXUTXPQNNSMF9ZW2
DEBUG: "true"
KUBECONFIG_DRY_RUN: "true"
steps:
- uses: actions/checkout@v4
- uses: jacobsvante/setup-age-action@main
- name: Create dummy kustomize base files
shell: bash
run: |
mkdir -p ${{ env.KUSTOMIZATION_BASE_DIR }}
cd ${{ env.KUSTOMIZATION_BASE_DIR }}
echo 'CONFIG_1=MY_VALUE' > config.env
cat <<EOF > kustomization.yaml
resources:
- deployment.yaml
- job.yaml
commonLabels:
app: my-app
app.kubernetes.io/name: my-app
app.kubernetes.io/instance: my-app
configMapGenerator:
- name: my-app
envs:
- "config.env"
images:
- name: APP_IMAGE
newName: my-docker-image # NOTE: Replaced by CI/CD
newTag: latest # NOTE: Replaced by CI/CD
EOF
cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
template:
spec:
imagePullSecrets:
- name: my-docker-registry.io
containers:
- name: app
image: APP_IMAGE # NOTE: Will be replaced
imagePullPolicy: IfNotPresent
envFrom:
- configMapRef:
name: my-app
- secretRef:
name: my-app
EOF
cat <<EOF > job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
labels:
delete-me: please
spec:
template:
spec:
imagePullSecrets:
- name: my-docker-registry.io
restartPolicy: OnFailure
containers:
- name: migrate
args:
- migrate
image: hello-world:latest
imagePullPolicy: IfNotPresent
EOF
cd ../..
- name: Create dummy kustomize overlay files
shell: bash
run: |
mkdir -p $KUSTOMIZATION_DIR
cd $KUSTOMIZATION_DIR
echo 'CONFIG_2=MY_STAGING_SPECIFIC_CONFIG_VALUE' > config.env
echo 'MY_FIRST_SECRET=value1\nMY_SECOND_SECRET=value2' | age --encrypt --recipient $AGE_TEST_PUBLIC_KEY > secrets.env
cat <<EOF > kustomization.yaml
bases:
- ../../base
namespace: my-k8s-namespace
configMapGenerator:
- name: my-app
behavior: merge
envs:
- "config.env"
secretGenerator:
- name: my-app
envs:
- "secrets.env.dec"
EOF
cd ../../..
- name: Create Kubernetes cluster
uses: helm/[email protected]
with:
version: v0.14.0
kubectl_version: v1.24.3
- name: Run action
uses: ./
with:
kubeconfig: ${{ env.FAKE_KUBECONFIG }}
docker-repo: my-docker-namespace/my-docker-image
docker-tag: "0.1.2"
docker-server: my-docker-registry.io
docker-password: my-user
docker-username: abc123
pre-deploy-delete-job-selector: delete-me=please
kustomization-base-dir: ${{ env.KUSTOMIZATION_BASE_DIR }}
kustomization-dir: ${{ env.KUSTOMIZATION_DIR }}
age-secret-key: ${{ env.AGE_TEST_SECRET_KEY }}
- name: Test that namespace was created
run: |
kubectl get namespace my-k8s-namespace -o yaml
- name: Test that image pull secret was created with expected properties
run: |
kubectl get secret -n my-k8s-namespace -o yaml > output.yaml
grep 'name: my-docker-registry.io' output.yaml
grep 'namespace: my-k8s-namespace' output.yaml
- name: Test that deployment was made with expected properties
run: |
kubectl get deploy -n my-k8s-namespace -o yaml > output.yaml
grep 'image: my-docker-registry.io/my-docker-namespace/my-docker-image:0.1.2' output.yaml
grep 'namespace: my-k8s-namespace' output.yaml
- name: Ensure that job exists
run: kubectl get -n my-k8s-namespace job/my-job
- name: Run action again for verification purposes
uses: ./
with:
kubeconfig: ${{ env.FAKE_KUBECONFIG }}
docker-repo: my-docker-namespace/my-docker-image
docker-tag: "0.1.2"
docker-server: my-docker-registry.io
docker-password: my-user
docker-username: abc123
pre-deploy-delete-job-selector: delete-me=please
kustomization-base-dir: ${{ env.KUSTOMIZATION_BASE_DIR }}
kustomization-dir: ${{ env.KUSTOMIZATION_DIR }}
age-secret-key: ${{ env.AGE_TEST_SECRET_KEY }}
- name: Ensure that job was deleted
run: kubectl get -n my-k8s-namespace job/my-job || echo "Job not found, as expected"
post-advanced-test:
needs: [advanced-test]
runs-on: ubuntu-latest
steps:
- name: Expect files created by action to no longer exist
run: |
[[ ! -f deploy-output.yaml ]]
simple-test:
runs-on: ubuntu-latest
env:
KUSTOMIZATION_DIR: overlays/simple
DEBUG: "true"
KUBECONFIG_DRY_RUN: "true"
steps:
- uses: actions/checkout@v4
- name: Create dummy nginx base
shell: bash
run: |
mkdir nginx
cd nginx
cat <<EOF > kustomization.yaml
resources:
- deployment.yaml
EOF
cat <<EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
EOF
cd ../..
- name: Create dummy kustomize overlay files
shell: bash
run: |
mkdir -p $KUSTOMIZATION_DIR
cd $KUSTOMIZATION_DIR
cat <<EOF > kustomization.yaml
bases:
- ../../nginx
EOF
cd ../..
- name: Create Kubernetes cluster
uses: helm/[email protected]
with:
version: v0.14.0
kubectl_version: v1.24.3
- name: Run action
uses: ./
with:
kubeconfig: ${{ env.FAKE_KUBECONFIG }}
kustomization-dir: ${{ env.KUSTOMIZATION_DIR }}
- name: Test that deployment was made with expected properties
run: |
kubectl get deploy -o yaml | grep 'image: nginx:latest'
release-pull-request:
runs-on: ubuntu-latest
needs: [pre-commit, advanced-test, post-advanced-test, simple-test]
steps:
- uses: google-github-actions/release-please-action@v4
with:
skip-github-release: true