-
Notifications
You must be signed in to change notification settings - Fork 1
141 lines (118 loc) · 3.87 KB
/
k8s-deploy.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
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
- name: 'Apply manifests: CRD resources'
run: |
if [ -d ./_/CustomResourceDefinition ]; then
kubectl apply -Rf ./_/CustomResourceDefinition | tee -a /tmp/kube.log
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 \
| xargs -r0 -n 1 kubectl apply -Rf
) | tee -a /tmp/kube.log
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}")"
cat <<EOF | kubectl apply -f - | tee -a /tmp/kube.log
apiVersion: v1
kind: Secret
metadata:
name: regcred
namespace: ${namespace}
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: ${{ secrets.DOCKER_CONFIG_BASE64 }}
EOF
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 \
| xargs -r0 -n 1 kubectl apply -Rf
) | tee -a /tmp/kube.log
- 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##*/}"
if [ "${namespace}" == "_" ]; then
kubectl delete $kind $name | tee -a /tmp/kube.log
else
kubectl -n $namespace delete $kind $name | tee -a /tmp/kube.log
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
)"
## 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 }}