-
Notifications
You must be signed in to change notification settings - Fork 5
227 lines (191 loc) · 8.16 KB
/
k8s-deploy.yml
File metadata and controls
227 lines (191 loc) · 8.16 KB
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
name: 'K8s: Deploy k8s-manifests'
on:
push:
branches: [ deploys/k8s-manifests ]
env:
BRANCH_RELEASE: releases/k8s-manifests
BRANCH_DEPLOY: deploys/k8s-manifests
jobs:
k8s-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ env.BRANCH_DEPLOY }}
fetch-depth: 2 # need parent commit to detect deletions
- name: Configure .kube/config
run: |
test -e ~/.kube || mkdir ~/.kube
echo "${{ secrets.KUBECONFIG_BASE64 }}" | base64 -d > ~/.kube/config
# initialize empty log of kube operations
echo -n '' > /tmp/kube.log
echo -n '' > /tmp/kube.err
- name: 'Apply manifests: CRD resources'
# Server-side apply avoids the 256KB metadata.annotations limit hit by
# client-side apply's last-applied-configuration on large CRDs (e.g.
# cnpg's poolers.postgresql.cnpg.io). --force-conflicts lets us reclaim
# ownership from any prior client-side annotation during migration.
run: |
if [ -d ./_/CustomResourceDefinition ]; then
# Capture errors and add context
dir_errors=$(kubectl apply --server-side --force-conflicts -Rf ./_/CustomResourceDefinition 2>&1 1>>/tmp/kube.log || true)
# Filter and append errors with context if meaningful
filtered_errors=$(echo "$dir_errors" | \
grep -v "Warning: Use tokens from the TokenRequest API" | \
grep -v "^Error: exit status [0-9]*$" | \
grep -v "^[[:space:]]*$" || true)
if [ -n "$filtered_errors" ] && echo "$filtered_errors" | grep -q '[^[:space:]]' 2>/dev/null; then
echo "=== CRD Resources ===" >> /tmp/kube.err
echo "$filtered_errors" >> /tmp/kube.err
echo "" >> /tmp/kube.err
fi
fi
- name: 'Apply manifests: non-CRD global resources'
run: |
if [ -d ./_ ]; then
find _ \
-maxdepth 1 \
-mindepth 1 \
-type d \
-not -name 'CustomResourceDefinition' \
-print0 \
| sort -z \
| while IFS= read -r -d '' dir; do
# Capture errors and add context per directory
dir_errors=$(kubectl apply -Rf "$dir" 2>&1 1>>/tmp/kube.log || true)
# Filter and append errors with context if meaningful
filtered_errors=$(echo "$dir_errors" | \
grep -v "Warning: Use tokens from the TokenRequest API" | \
grep -v "^Error: exit status [0-9]*$" | \
grep -v "^[[:space:]]*$" || true)
if [ -n "$filtered_errors" ] && echo "$filtered_errors" | grep -q '[^[:space:]]' 2>/dev/null; then
echo "=== Directory: $dir ===" >> /tmp/kube.err
echo "$filtered_errors" >> /tmp/kube.err
echo "" >> /tmp/kube.err
fi
done
fi
- name: 'Apply manifests: generated regcred secrets'
run: |
# apply a copy of regcred secret for every deployed namespace
while IFS= read -r namespace; do
namespace="$(basename "${namespace}")"
# Capture errors for this namespace's regcred
secret_errors=$(cat <<EOF | kubectl apply -f - 2>&1 1>>/tmp/kube.log || true
apiVersion: v1
kind: Secret
metadata:
name: regcred
namespace: ${namespace}
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: ${{ secrets.DOCKER_CONFIG_BASE64 }}
EOF
)
# Filter and append errors with context if meaningful
filtered_errors=$(echo "$secret_errors" | \
grep -v "Warning: Use tokens from the TokenRequest API" | \
grep -v "^Error: exit status [0-9]*$" | \
grep -v "^[[:space:]]*$" || true)
if [ -n "$filtered_errors" ] && echo "$filtered_errors" | grep -q '[^[:space:]]' 2>/dev/null; then
echo "=== Regcred Secret: $namespace ===" >> /tmp/kube.err
echo "$filtered_errors" >> /tmp/kube.err
echo "" >> /tmp/kube.err
fi
done <<< "$(find . -maxdepth 1 -type d -not -name '_' -not -name '.*')"
- name: 'Apply manifests: namespaced resources'
run: |
find . \
-maxdepth 1 \
-type d \
-not -name '_' \
-not -name '.*' \
-print0 \
| sort -z \
| while IFS= read -r -d '' dir; do
# Capture errors and add context per directory
dir_errors=$(kubectl apply -Rf "$dir" 2>&1 1>>/tmp/kube.log || true)
# Filter and append errors with context if meaningful
filtered_errors=$(echo "$dir_errors" | \
grep -v "Warning: Use tokens from the TokenRequest API" | \
grep -v "^Error: exit status [0-9]*$" | \
grep -v "^[[:space:]]*$" || true)
if [ -n "$filtered_errors" ] && echo "$filtered_errors" | grep -q '[^[:space:]]' 2>/dev/null; then
echo "=== Directory: $dir ===" >> /tmp/kube.err
echo "$filtered_errors" >> /tmp/kube.err
echo "" >> /tmp/kube.err
fi
done
- name: 'Apply manifests: deleted resources'
run: |
for manifest_path in $(git diff-tree --name-only --diff-filter=D -r HEAD^ HEAD); do
manifest_path="${manifest_path%.yaml}"
namespace="${manifest_path%%/*}"
kind_name="${manifest_path#*/}"
kind="${kind_name%%/*}"
name="${kind_name##*/}"
# Capture errors for this deletion
if [ "${namespace}" == "_" ]; then
delete_errors=$(kubectl delete $kind $name 2>&1 1>>/tmp/kube.log || true)
else
delete_errors=$(kubectl -n $namespace delete $kind $name 2>&1 1>>/tmp/kube.log || true)
fi
# Filter and append errors with context if meaningful
filtered_errors=$(echo "$delete_errors" | \
grep -v "Warning: Use tokens from the TokenRequest API" | \
grep -v "^Error: exit status [0-9]*$" | \
grep -v "^[[:space:]]*$" || true)
if [ -n "$filtered_errors" ] && echo "$filtered_errors" | grep -q '[^[:space:]]' 2>/dev/null; then
if [ "${namespace}" == "_" ]; then
echo "=== Deleting: $kind/$name ===" >> /tmp/kube.err
else
echo "=== Deleting: $namespace/$kind/$name ===" >> /tmp/kube.err
fi
echo "$filtered_errors" >> /tmp/kube.err
echo "" >> /tmp/kube.err
fi
done
- name: Add comment to PR
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
## build comment body
echo
echo "Builing coment body content..."
comment_body="$(cat <<EOF
\`kubectl apply\` output (excluding unchanged) for $(git describe --always --tag) was:
\`\`\`
$(cat /tmp/kube.log | grep -v ' unchanged$')
\`\`\`
EOF
)"
# Conditionally append error output if it has meaningful content
if [ -s /tmp/kube.err ]; then
comment_body="${comment_body}
## Errors/Warnings
\`\`\`
$(cat /tmp/kube.err)
\`\`\`"
fi
## get most recent merged PR
echo
echo "Looking for most recent merged PR for branch ${BRANCH_RELEASE}..."
pr_number=$(
gh pr list \
--head "${BRANCH_RELEASE}" \
--base "${BRANCH_DEPLOY}" \
--state merged \
--limit 1 \
--json number \
--jq '.[0].number'
)
## post comment
if [ -n "${pr_number}" ]; then
echo
echo "Adding comment to PR #${pr_number}..."
gh pr comment "${pr_number}" \
--body "${comment_body}"
fi
# - uses: mxschmitt/action-tmate@v3
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}