Skip to content

Commit 13f485f

Browse files
feat(ci): add k8s deployment
1 parent 77dd3e2 commit 13f485f

File tree

2 files changed

+259
-0
lines changed

2 files changed

+259
-0
lines changed

.github/workflows/k8s-deploy.yml

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
name: Deploy K8s Preview
2+
3+
on:
4+
pull_request:
5+
branches: [ develop ]
6+
types: [ opened, reopened, synchronize ]
7+
push:
8+
branches: [ develop ]
9+
10+
env:
11+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12+
13+
DOCKER_REGISTRY: ghcr.io
14+
DOCKER_PACKAGE: laddr-composite
15+
16+
KUBE_CONFIG_DATA: ${{ secrets.KUBECONFIG_BASE64 }}
17+
KUBE_NAMESPACE: laddr
18+
KUBE_HOSTNAME: laddr.sandbox.k8s.phl.io
19+
20+
DATABASE_NAME: laddr
21+
22+
HAB_LICENSE: accept-no-persist
23+
HAB_ORIGIN: codeforphilly
24+
25+
jobs:
26+
27+
k8s-deploy:
28+
runs-on: ubuntu-latest
29+
steps:
30+
31+
- name: Cancel superseded runs
32+
uses: styfle/[email protected]
33+
with:
34+
access_token: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Configure environment
37+
run: |
38+
if [ "${GITHUB_EVENT_NAME}" == "pull_request" ]; then
39+
RELEASE_NAME="pr-$(jq --raw-output .pull_request.number "${GITHUB_EVENT_PATH}")"
40+
RELEASE_TRANSIENT='true'
41+
else
42+
RELEASE_NAME="latest"
43+
RELEASE_TRANSIENT='false'
44+
fi
45+
46+
echo "Using RELEASE_NAME=${RELEASE_NAME}"
47+
echo "RELEASE_NAME=${RELEASE_NAME}" >> $GITHUB_ENV
48+
49+
echo "Using RELEASE_TRANSIENT=${RELEASE_TRANSIENT}"
50+
echo "RELEASE_TRANSIENT=${RELEASE_TRANSIENT}" >> $GITHUB_ENV
51+
52+
DOCKER_REPOSITORY="${GITHUB_REPOSITORY,,}"
53+
54+
echo "Using DOCKER_REPOSITORY=${DOCKER_REPOSITORY}"
55+
echo "DOCKER_REPOSITORY=${DOCKER_REPOSITORY}" >> $GITHUB_ENV
56+
57+
- name: Create Github Deployment
58+
uses: bobheadxi/[email protected]
59+
id: deployment
60+
with:
61+
step: start
62+
token: ${{ secrets.GITHUB_TOKEN }}
63+
env: '${{ env.RELEASE_NAME }}'
64+
ref: '${{ github.head_ref }}'
65+
transient: ${{ env.RELEASE_TRANSIENT }}
66+
logs: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
67+
no_override: false
68+
69+
- uses: actions/checkout@v2
70+
71+
- name: 'Initialize Chef Habitat environment'
72+
uses: JarvusInnovations/habitat-action@action/v1
73+
with:
74+
deps: |
75+
jarvus/hologit
76+
77+
- id: site-projection
78+
name: 'Project holobranch: emergence-site'
79+
uses: JarvusInnovations/hologit@actions/projector/v1
80+
with:
81+
# use HEAD checked out above by checkout action
82+
ref: HEAD
83+
fetch: false
84+
holobranch: emergence-site
85+
86+
- id: fixtures-projection
87+
name: 'Project holobranch: fixtures'
88+
uses: JarvusInnovations/hologit@actions/projector/v1
89+
with:
90+
# use HEAD checked out above by checkout action
91+
ref: HEAD
92+
fetch: false
93+
holobranch: fixtures
94+
95+
- name: Build & push Docker image
96+
uses: whoan/docker-build-with-cache-action@v5
97+
with:
98+
dockerfile: Dockerfile
99+
username: ${{ github.actor }}
100+
password: ${{ env.GITHUB_TOKEN }}
101+
registry: ${{ env.DOCKER_REGISTRY }}
102+
image_name: ${{ env.DOCKER_REPOSITORY }}/${{ env.DOCKER_PACKAGE }}
103+
image_tag: ${{ env.RELEASE_NAME }}
104+
build_extra_args: |
105+
--build-arg=SITE_TREE=${{ steps.site-projection.outputs.tree }}
106+
--build-arg=SITE_VERSION=0.0.0-${{ env.RELEASE_NAME }}
107+
--build-arg=SOURCE_COMMIT=${{ github.sha }}
108+
--build-arg=SOURCE_TAG=${{ env.RELEASE_NAME }}
109+
--build-arg=HAB_LICENSE=${{ env.HAB_LICENSE }}
110+
111+
- name: Configure kubectl
112+
run: |
113+
set -e
114+
test -e ~/.kube || mkdir ~/.kube
115+
printf '%s' "$KUBE_CONFIG_DATA" | base64 -d > ~/.kube/config
116+
117+
- name: Deploy instance via Helm template
118+
run: |
119+
set -e
120+
121+
docker_image="${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_PACKAGE}:${RELEASE_NAME}"
122+
release_hostname="${RELEASE_NAME}.${KUBE_HOSTNAME}"
123+
124+
kubectl config set-context --current --namespace="${KUBE_NAMESPACE}"
125+
126+
echo "Listing pods existing before deploy"
127+
kubectl get pods \
128+
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
129+
--template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' \
130+
| sort \
131+
| tee ./.pods-before
132+
133+
echo "Using helm upgrade to apply ./helm-chart to release ${RELEASE_NAME}"
134+
helm upgrade "${RELEASE_NAME}" ./helm-chart \
135+
--install \
136+
--set name="${RELEASE_NAME}" \
137+
--set namespace="${KUBE_NAMESPACE}" \
138+
--set image="${docker_image}" \
139+
--set hostname="${release_hostname}" \
140+
--set hab.runtime.error.display=true
141+
142+
echo "Listing pods existing after deploy"
143+
kubectl get pods \
144+
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
145+
--template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' \
146+
| sort \
147+
| tee ./.pods-after
148+
149+
echo "Deleting stale pods to force image refresh"
150+
comm -12 ./.pods-before ./.pods-after \
151+
| xargs --no-run-if-empty kubectl delete pod
152+
153+
- name: Wait for Deployment to be ready
154+
timeout-minutes: 10
155+
run: |
156+
until kubectl rollout status deployment "${RELEASE_NAME}" 2>/dev/null >/dev/null; do
157+
echo -n "."
158+
sleep .5
159+
done
160+
161+
- name: Find new Pod
162+
run: |
163+
POD_NAME=$(
164+
kubectl get pod \
165+
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
166+
-o jsonpath='{.items[0].metadata.name}'
167+
)
168+
169+
echo "Using POD_NAME=${POD_NAME}"
170+
echo "POD_NAME=${POD_NAME}" >> $GITHUB_ENV
171+
172+
- name: Wait For Pod to be ready
173+
timeout-minutes: 5
174+
run: kubectl wait --for condition=ready "pod/${POD_NAME}" --timeout=30s
175+
176+
- name: Wait for MySQL to be Ready
177+
timeout-minutes: 5
178+
run: |
179+
until kubectl exec "${POD_NAME}" -- hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" mysqladmin ping; do
180+
sleep .5
181+
done
182+
183+
- name: Load fixtures into database
184+
run: |
185+
echo "Dropping any existing database..."
186+
kubectl exec "${POD_NAME}" -- \
187+
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
188+
mysqladmin drop "${DATABASE_NAME}" --force \
189+
|| true
190+
191+
echo "Creating an empty database..."
192+
kubectl exec "${POD_NAME}" -- \
193+
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
194+
mysqladmin create "${DATABASE_NAME}"
195+
196+
echo "Loading fixtures..."
197+
(
198+
for fixture_file in $(git ls-tree -r --name-only ${{ steps.fixtures-projection.outputs.tree }}); do
199+
git cat-file -p "${{ steps.fixtures-projection.outputs.tree }}:${fixture_file}"
200+
done
201+
) | kubectl exec -i "${POD_NAME}" -- \
202+
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
203+
mysql "${DATABASE_NAME}"
204+
205+
echo "Running migrations..."
206+
kubectl exec "${POD_NAME}" -- \
207+
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
208+
emergence-console-run migrations:execute --all
209+
210+
- name: Update Github Deployment
211+
uses: bobheadxi/[email protected]
212+
if: ${{ always() }}
213+
with:
214+
step: finish
215+
token: ${{ secrets.GITHUB_TOKEN }}
216+
status: ${{ job.status }}
217+
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
218+
env_url: 'https://${{ env.RELEASE_NAME}}.${{ env.KUBE_HOSTNAME }}/'
219+
logs: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'

.github/workflows/k8s-destroy.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Destroy K8s Preview
2+
3+
on:
4+
pull_request:
5+
branches: [ develop ]
6+
types: [ closed ]
7+
8+
env:
9+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10+
11+
KUBE_CONFIG_DATA: ${{ secrets.KUBECONFIG_BASE64 }}
12+
KUBE_NAMESPACE: laddr
13+
14+
RELEASE_NAME: pr-${{ github.event.number }}
15+
16+
jobs:
17+
18+
k8s-destroy:
19+
runs-on: ubuntu-latest
20+
steps:
21+
22+
- name: Configure kubectl
23+
run: |
24+
set -e
25+
test -e ~/.kube || mkdir ~/.kube
26+
printf '%s' "$KUBE_CONFIG_DATA" | base64 -d > ~/.kube/config
27+
28+
- name: Delete PR Deployment
29+
run: |
30+
set -e
31+
kubectl config set-context --current --namespace="${KUBE_NAMESPACE}"
32+
kubectl delete deployment,replicaset,ingress,all -l "app.kubernetes.io/instance=${RELEASE_NAME}"
33+
kubectl delete secret "${RELEASE_NAME}-tls"
34+
35+
- name: Deactivate Github Deployment
36+
uses: bobheadxi/[email protected]
37+
with:
38+
step: deactivate-env
39+
token: ${{ secrets.GITHUB_TOKEN }}
40+
env: '${{ env.RELEASE_NAME }}'

0 commit comments

Comments
 (0)