-
Notifications
You must be signed in to change notification settings - Fork 0
221 lines (196 loc) · 6.53 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
name: Main
on:
push:
branches:
- main
env:
ACTION_NAME: kustomize-deploy-action
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]
test:
runs-on: ubuntu-latest
name: Test action
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-test:
needs: [test]
runs-on: ubuntu-latest
steps:
- name: Expect files created by action to no longer exist
run: |
[[ ! -f deploy-output.yaml ]]
release-pull-request:
runs-on: ubuntu-latest
needs: [pre-commit, test, post-test]
steps:
- uses: google-github-actions/release-please-action@v4
with:
skip-github-release: true