-
Notifications
You must be signed in to change notification settings - Fork 6
234 lines (195 loc) · 7.8 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
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
name: Deploy K8s Preview
on:
pull_request:
branches: [ develop ]
types: [ opened, reopened, synchronize ]
push:
branches: [ develop ]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DOCKER_REGISTRY: ghcr.io
DOCKER_PACKAGE: site-composite
KUBE_CONFIG_DATA: ${{ secrets.KUBECONFIG_BASE64 }}
KUBE_NAMESPACE: gatekeeper
KUBE_HOSTNAME: gatekeeper.sandbox.k8s.jarv.us
DATABASE_NAME: gatekeeper
HAB_LICENSE: accept-no-persist
HAB_ORIGIN: jarvus
jobs:
k8s-deploy:
runs-on: ubuntu-latest
steps:
- name: Cancel superseded runs
uses: styfle/[email protected]
with:
access_token: ${{ secrets.GITHUB_TOKEN }}
- name: Configure environment
run: |
if [ "${GITHUB_EVENT_NAME}" == "pull_request" ]; then
RELEASE_NAME="pr-$(jq --raw-output .pull_request.number "${GITHUB_EVENT_PATH}")"
RELEASE_TRANSIENT='true'
else
RELEASE_NAME="latest"
RELEASE_TRANSIENT='false'
fi
echo "Using RELEASE_NAME=${RELEASE_NAME}"
echo "RELEASE_NAME=${RELEASE_NAME}" >> $GITHUB_ENV
echo "Using RELEASE_TRANSIENT=${RELEASE_TRANSIENT}"
echo "RELEASE_TRANSIENT=${RELEASE_TRANSIENT}" >> $GITHUB_ENV
DOCKER_REPOSITORY="${GITHUB_REPOSITORY,,}"
echo "Using DOCKER_REPOSITORY=${DOCKER_REPOSITORY}"
echo "DOCKER_REPOSITORY=${DOCKER_REPOSITORY}" >> $GITHUB_ENV
- name: Create Github Deployment
uses: bobheadxi/[email protected]
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: '${{ env.RELEASE_NAME }}'
ref: '${{ github.head_ref }}'
transient: ${{ env.RELEASE_TRANSIENT }}
logs: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
no_override: false
- uses: actions/checkout@v2
- name: 'Initialize Chef Habitat environment'
uses: JarvusInnovations/habitat-action@action/v1
with:
deps: |
jarvus/hologit
- id: site-projection
name: 'Project holobranch: emergence-site'
uses: JarvusInnovations/hologit@actions/projector/v1
with:
# use HEAD checked out above by checkout action
ref: HEAD
fetch: false
holobranch: emergence-site
- id: fixtures-projection
name: 'Project holobranch: fixtures'
uses: JarvusInnovations/hologit@actions/projector/v1
with:
# use HEAD checked out above by checkout action
ref: HEAD
fetch: false
holobranch: fixtures
- id: helm-projection
name: 'Project holobranch: helm-chart'
uses: JarvusInnovations/hologit@actions/projector/v1
with:
# use HEAD checked out above by checkout action
ref: HEAD
fetch: false
holobranch: helm-chart
- name: Build & push Docker image
uses: whoan/docker-build-with-cache-action@v5
with:
dockerfile: Dockerfile
username: ${{ github.actor }}
password: ${{ env.GITHUB_TOKEN }}
registry: ${{ env.DOCKER_REGISTRY }}
image_name: ${{ env.DOCKER_REPOSITORY }}/${{ env.DOCKER_PACKAGE }}
image_tag: ${{ env.RELEASE_NAME }}
build_extra_args: |
--build-arg=SITE_TREE=${{ steps.site-projection.outputs.tree }}
--build-arg=SITE_VERSION=0.0.0-${{ env.RELEASE_NAME }}
--build-arg=SOURCE_COMMIT=${{ github.sha }}
--build-arg=SOURCE_TAG=${{ env.RELEASE_NAME }}
--build-arg=HAB_LICENSE=${{ env.HAB_LICENSE }}
- name: Configure kubectl
run: |
set -e
test -e ~/.kube || mkdir ~/.kube
printf '%s' "$KUBE_CONFIG_DATA" | base64 -d > ~/.kube/config
- name: Deploy instance via Helm template
run: |
release_hostname="${RELEASE_NAME}.${KUBE_HOSTNAME}"
echo "Ensuring current context is namespace ${KUBE_NAMESPACE}"
kubectl config set-context --current --namespace="${KUBE_NAMESPACE}"
echo "Listing pods existing before deploy"
kubectl get pods \
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
--template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' \
| sort \
| tee ./.pods-before
echo "Extracting projected helm-chart to temporary directory"
temp_dir=$(mktemp -d)
git archive --format=tar "${{ steps.helm-projection.outputs.tree }}" | ( cd "${temp_dir}" && tar -xf - )
echo "Using helm upgrade to apply helm-chart to release ${RELEASE_NAME}"
helm upgrade "${RELEASE_NAME}" "${temp_dir}" \
--install \
--namespace "${KUBE_NAMESPACE}" \
--set site.name="${RELEASE_NAME}" \
--set site.title="${KUBE_NAMESPACE}/${RELEASE_NAME}" \
--set site.image.repository="${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_PACKAGE}" \
--set site.image.tag="${RELEASE_NAME}" \
--set ingress.enabled=true \
--set site.canonicalHostname="${release_hostname}" \
--set site.displayErrors=true \
--set hab.license=accept-no-persist
echo "Listing pods existing after deploy"
kubectl get pods \
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
--template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' \
| sort \
| tee ./.pods-after
echo "Deleting stale pods to force image refresh"
comm -12 ./.pods-before ./.pods-after \
| xargs --no-run-if-empty kubectl delete pod
- name: Wait for Deployment to be ready
timeout-minutes: 10
run: |
until kubectl rollout status deployment "${RELEASE_NAME}" 2>/dev/null >/dev/null; do
echo -n "."
sleep .5
done
- name: Find new Pod
run: |
POD_NAME=$(
kubectl get pod \
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
-o jsonpath='{.items[0].metadata.name}'
)
echo "Using POD_NAME=${POD_NAME}"
echo "POD_NAME=${POD_NAME}" >> $GITHUB_ENV
- name: Wait For Pod to be ready
timeout-minutes: 5
run: kubectl wait --for condition=ready "pod/${POD_NAME}" --timeout=30s
- name: Wait for MySQL to be Ready
timeout-minutes: 5
run: |
until kubectl exec "${POD_NAME}" -- hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" mysqladmin ping; do
sleep .5
done
- name: Load fixtures into database
run: |
echo "Dropping any existing database..."
kubectl exec "${POD_NAME}" -- \
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
mysqladmin drop "${DATABASE_NAME}" --force \
|| true
echo "Creating an empty database..."
kubectl exec "${POD_NAME}" -- \
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
mysqladmin create "${DATABASE_NAME}"
echo "Loading fixtures..."
(
for fixture_file in $(git ls-tree -r --name-only ${{ steps.fixtures-projection.outputs.tree }}); do
git cat-file -p "${{ steps.fixtures-projection.outputs.tree }}:${fixture_file}"
done
) | kubectl exec -i "${POD_NAME}" -- \
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
mysql "${DATABASE_NAME}"
echo "Running migrations..."
kubectl exec "${POD_NAME}" -- \
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
emergence-console-run migrations:execute --all
- name: Update Github Deployment
uses: bobheadxi/[email protected]
if: ${{ always() }}
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: 'https://${{ env.RELEASE_NAME}}.${{ env.KUBE_HOSTNAME }}/'
logs: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'