Skip to content

Commit 9cab091

Browse files
Merge pull request #12 from CodeForPhilly/develop
Release: v1.0.3
2 parents cd7dd12 + 56298f9 commit 9cab091

File tree

6 files changed

+279
-7
lines changed

6 files changed

+279
-7
lines changed

.github/workflows/k8s-deploy.yml

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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: site-composite
15+
16+
KUBE_CONFIG_DATA: ${{ secrets.KUBECONFIG_BASE64 }}
17+
KUBE_NAMESPACE: code-for-philly
18+
KUBE_HOSTNAME: codeforphilly.sandbox.k8s.phl.io
19+
20+
DATABASE_NAME: codeforphilly
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+
- id: helm-projection
96+
name: 'Project holobranch: helm-chart'
97+
uses: JarvusInnovations/hologit@actions/projector/v1
98+
with:
99+
# use HEAD checked out above by checkout action
100+
ref: HEAD
101+
fetch: false
102+
holobranch: helm-chart
103+
104+
- name: Build & push Docker image
105+
uses: whoan/docker-build-with-cache-action@v5
106+
with:
107+
dockerfile: Dockerfile
108+
username: ${{ github.actor }}
109+
password: ${{ env.GITHUB_TOKEN }}
110+
registry: ${{ env.DOCKER_REGISTRY }}
111+
image_name: ${{ env.DOCKER_REPOSITORY }}/${{ env.DOCKER_PACKAGE }}
112+
image_tag: ${{ env.RELEASE_NAME }}
113+
build_extra_args: |
114+
--build-arg=SITE_TREE=${{ steps.site-projection.outputs.tree }}
115+
--build-arg=SITE_VERSION=0.0.0-${{ env.RELEASE_NAME }}
116+
--build-arg=SOURCE_COMMIT=${{ github.sha }}
117+
--build-arg=SOURCE_TAG=${{ env.RELEASE_NAME }}
118+
--build-arg=HAB_LICENSE=${{ env.HAB_LICENSE }}
119+
120+
- name: Configure kubectl
121+
run: |
122+
set -e
123+
test -e ~/.kube || mkdir ~/.kube
124+
printf '%s' "$KUBE_CONFIG_DATA" | base64 -d > ~/.kube/config
125+
126+
- name: Deploy instance via Helm template
127+
run: |
128+
release_hostname="${RELEASE_NAME}.${KUBE_HOSTNAME}"
129+
130+
echo "Ensuring current context is namespace ${KUBE_NAMESPACE}"
131+
kubectl config set-context --current --namespace="${KUBE_NAMESPACE}"
132+
133+
echo "Listing pods existing before deploy"
134+
kubectl get pods \
135+
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
136+
--template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' \
137+
| sort \
138+
| tee ./.pods-before
139+
140+
echo "Extracting projected helm-chart to temporary directory"
141+
temp_dir=$(mktemp -d)
142+
git archive --format=tar "${{ steps.helm-projection.outputs.tree }}" | ( cd "${temp_dir}" && tar -xf - )
143+
144+
echo "Using helm upgrade to apply helm-chart to release ${RELEASE_NAME}"
145+
helm upgrade "${RELEASE_NAME}" "${temp_dir}" \
146+
--install \
147+
--namespace "${KUBE_NAMESPACE}" \
148+
--set site.name="${RELEASE_NAME}" \
149+
--set site.title="laddr/${RELEASE_NAME}" \
150+
--set site.image.repository="${DOCKER_REGISTRY}/${DOCKER_REPOSITORY}/${DOCKER_PACKAGE}" \
151+
--set site.image.tag="${RELEASE_NAME}" \
152+
--set ingress.enabled=true \
153+
--set site.canonicalHostname="${release_hostname}" \
154+
--set site.displayErrors=true \
155+
--set hab.license=accept-no-persist
156+
157+
echo "Listing pods existing after deploy"
158+
kubectl get pods \
159+
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
160+
--template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' \
161+
| sort \
162+
| tee ./.pods-after
163+
164+
echo "Deleting stale pods to force image refresh"
165+
comm -12 ./.pods-before ./.pods-after \
166+
| xargs --no-run-if-empty kubectl delete pod
167+
168+
- name: Wait for Deployment to be ready
169+
timeout-minutes: 10
170+
run: |
171+
until kubectl rollout status deployment "${RELEASE_NAME}" 2>/dev/null >/dev/null; do
172+
echo -n "."
173+
sleep .5
174+
done
175+
176+
- name: Find new Pod
177+
run: |
178+
POD_NAME=$(
179+
kubectl get pod \
180+
-l app.kubernetes.io/instance="${RELEASE_NAME}" \
181+
-o jsonpath='{.items[0].metadata.name}'
182+
)
183+
184+
echo "Using POD_NAME=${POD_NAME}"
185+
echo "POD_NAME=${POD_NAME}" >> $GITHUB_ENV
186+
187+
- name: Wait For Pod to be ready
188+
timeout-minutes: 5
189+
run: kubectl wait --for condition=ready "pod/${POD_NAME}" --timeout=30s
190+
191+
- name: Wait for MySQL to be Ready
192+
timeout-minutes: 5
193+
run: |
194+
until kubectl exec "${POD_NAME}" -- hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" mysqladmin ping; do
195+
sleep .5
196+
done
197+
198+
- name: Load fixtures into database
199+
run: |
200+
echo "Dropping any existing database..."
201+
kubectl exec "${POD_NAME}" -- \
202+
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
203+
mysqladmin drop "${DATABASE_NAME}" --force \
204+
|| true
205+
206+
echo "Creating an empty database..."
207+
kubectl exec "${POD_NAME}" -- \
208+
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
209+
mysqladmin create "${DATABASE_NAME}"
210+
211+
echo "Loading fixtures..."
212+
(
213+
for fixture_file in $(git ls-tree -r --name-only ${{ steps.fixtures-projection.outputs.tree }}); do
214+
git cat-file -p "${{ steps.fixtures-projection.outputs.tree }}:${fixture_file}"
215+
done
216+
) | kubectl exec -i "${POD_NAME}" -- \
217+
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
218+
mysql "${DATABASE_NAME}"
219+
220+
echo "Running migrations..."
221+
kubectl exec "${POD_NAME}" -- \
222+
hab pkg exec "${HAB_ORIGIN}/${DOCKER_PACKAGE}" \
223+
emergence-console-run migrations:execute --all
224+
225+
- name: Update Github Deployment
226+
uses: bobheadxi/[email protected]
227+
if: ${{ always() }}
228+
with:
229+
step: finish
230+
token: ${{ secrets.GITHUB_TOKEN }}
231+
status: ${{ job.status }}
232+
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
233+
env_url: 'https://${{ env.RELEASE_NAME}}.${{ env.KUBE_HOSTNAME }}/'
234+
logs: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'

.github/workflows/k8s-destroy.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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: code-for-philly
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+
test -e ~/.kube || mkdir ~/.kube
25+
printf '%s' "$KUBE_CONFIG_DATA" | base64 -d > ~/.kube/config
26+
27+
- name: Delete PR Deployment
28+
run: |
29+
kubectl config set-context --current --namespace="${KUBE_NAMESPACE}"
30+
kubectl delete deployment,replicaset,ingress,all -l "app.kubernetes.io/instance=${RELEASE_NAME}"
31+
kubectl delete secret "${RELEASE_NAME}-tls"
32+
33+
- name: Deactivate Github Deployment
34+
uses: bobheadxi/[email protected]
35+
with:
36+
step: deactivate-env
37+
token: ${{ secrets.GITHUB_TOKEN }}
38+
env: '${{ env.RELEASE_NAME }}'

.holo/sources/laddr.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[holosource]
22
url = "https://github.com/CodeForPhilly/laddr"
3-
ref = "refs/tags/v3.0.3"
3+
ref = "refs/tags/v3.0.5"

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ RUN hab pkg exec core/hab-plan-build hab-plan-build /src/habitat/composite
5454
FROM habitat as runtime
5555

5656
# configure persistent volumes
57-
RUN hab pkg exec core/coreutils mkdir -p '/hab/svc/mysql/data' '/hab/svc/codeforphilly/data' '/hab/svc/nginx/files' \
58-
&& hab pkg exec core/coreutils chown hab:hab -R '/hab/svc/mysql/data' '/hab/svc/codeforphilly/data' '/hab/svc/nginx/files'
57+
RUN hab pkg exec core/coreutils mkdir -p '/hab/svc/mysql/data' '/hab/svc/site/data' '/hab/svc/nginx/files' \
58+
&& hab pkg exec core/coreutils chown hab:hab -R '/hab/svc/mysql/data' '/hab/svc/site/data' '/hab/svc/nginx/files'
5959

6060
# configure entrypoint
61-
VOLUME ["/hab/svc/mysql/data", "/hab/svc/codeforphilly/data", "/hab/svc/nginx/files"]
61+
VOLUME ["/hab/svc/mysql/data", "/hab/svc/site/data", "/hab/svc/nginx/files"]
6262
ENTRYPOINT ["hab", "sup", "run"]
63-
CMD ["codeforphilly/codeforphilly-composite"]
63+
CMD ["codeforphilly/site-composite"]
6464

6565
# install .hart artifact from builder stage
6666
COPY --from=projector /hab/cache/artifacts/$HAB_ORIGIN-* /hab/cache/artifacts/

habitat/composite/plan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
composite_app_pkg_name=codeforphilly
1+
composite_app_pkg_name=site
22
pkg_name="${composite_app_pkg_name}-composite"
33
pkg_origin=codeforphilly
44
pkg_maintainer="Code for Philly <[email protected]>"

habitat/plan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pkg_name=codeforphilly
1+
pkg_name=site
22
pkg_origin=codeforphilly
33
pkg_maintainer="Code for Philly <[email protected]>"
44
pkg_scaffolding=emergence/scaffolding-site

0 commit comments

Comments
 (0)