Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core eng] modern CICD #723

Merged
merged 57 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
46a3cf1
add dev deploy commands
maxmwang Aug 15, 2024
5fd6d04
add cert template
maxmwang Aug 15, 2024
f70c3e6
add dev deploy github action
maxmwang Aug 15, 2024
7514d6c
fix: dev deploy action
maxmwang Aug 15, 2024
fc5f401
fix: dev deploy action
maxmwang Aug 15, 2024
1177861
use short sha
maxmwang Aug 15, 2024
9cd328c
use env
maxmwang Aug 15, 2024
d4bff43
echo into env
maxmwang Aug 15, 2024
1962e91
remove old helm release if exists
maxmwang Aug 15, 2024
0cbd008
use default values for prod deploys
maxmwang Aug 15, 2024
95169ec
add env label to helm charts
maxmwang Aug 15, 2024
1acae4e
add cleanup for dev environments
maxmwang Aug 15, 2024
68785f0
add working cleanup job with roles
maxmwang Aug 16, 2024
e32197c
add ttl input to workflow dispatch
maxmwang Aug 16, 2024
a19fdf1
deploy staging workflow
maxmwang Aug 16, 2024
8510ca1
break apart dev action
maxmwang Aug 16, 2024
585e805
fix cd dev action
maxmwang Aug 16, 2024
48f3d25
fix typo
maxmwang Aug 16, 2024
0ba8638
remove slashes from artifact names
maxmwang Aug 16, 2024
67d965e
break apart dev action (?)
maxmwang Aug 16, 2024
570894f
forward sha_short to all jobs
maxmwang Aug 16, 2024
9862708
fix forwarding
maxmwang Aug 16, 2024
d51a4bd
fix forwarding
maxmwang Aug 16, 2024
2ddde8a
define outputs
maxmwang Aug 16, 2024
07dd8ff
fix typo
maxmwang Aug 16, 2024
babde79
staging workflow
maxmwang Aug 16, 2024
9f11a05
rename dev to development
maxmwang Aug 16, 2024
08e5d53
prod workflow
maxmwang Aug 16, 2024
719cc45
fix job
maxmwang Aug 16, 2024
6d3b56b
test environment placement
maxmwang Aug 16, 2024
9d73c77
fix job
maxmwang Aug 16, 2024
ef6fbe7
branch check is needed
maxmwang Aug 16, 2024
7e6ffa0
add environments
maxmwang Aug 16, 2024
611bfcf
change ssh user to ga from root
maxmwang Aug 30, 2024
ffe69bb
Revert "change ssh user to ga from root"
maxmwang Aug 30, 2024
c31de2c
prod workflow artifact push depends on branch-check
maxmwang Sep 9, 2024
8dbd54b
add platform to docker build
maxmwang Sep 18, 2024
050e7a6
wait for build to finish before uploading artifact
maxmwang Sep 19, 2024
f101c2a
temp test
maxmwang Sep 19, 2024
c72a949
use 0 compression
maxmwang Sep 19, 2024
07fa476
fix typo
maxmwang Sep 19, 2024
f1c2e0d
do not upload as artifacts
maxmwang Sep 19, 2024
5084c11
env-mode=loose
maxmwang Sep 19, 2024
2f3e98e
fix mongo uri
maxmwang Sep 19, 2024
a9bdd6b
use default docker entrypoint
maxmwang Sep 19, 2024
f50ef53
fix mongo uri
maxmwang Sep 19, 2024
c32bfd5
update staging cicd
maxmwang Sep 19, 2024
428aa58
set imagePullPolicy to always
maxmwang Sep 26, 2024
f6a1859
Merge branch 'gql' into cicd
mathhulk Sep 26, 2024
ed0fd3d
test reusable workflows
maxmwang Sep 28, 2024
f9db87b
inherit secrets
maxmwang Sep 28, 2024
dc89580
use --no-cache flag
maxmwang Sep 29, 2024
4cb53c5
try no COPY
maxmwang Sep 29, 2024
3550982
use hours not seconds for TTL
maxmwang Sep 29, 2024
8a5cf88
surround sha with quotes
maxmwang Sep 29, 2024
2d97918
use --set-string for tag
maxmwang Sep 29, 2024
dba3fbd
use reusable workflows in prod and stage
maxmwang Sep 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions .github/workflows/cd-branch.yml

This file was deleted.

64 changes: 64 additions & 0 deletions .github/workflows/cd-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Deploy to Development

concurrency:
group: dev-${{ github.ref }}
cancel-in-progress: true

on:
workflow_dispatch:
inputs:
ttl:
description: "Deployment time to live in hours"
required: true
type: number
default: 24

jobs:
compute-sha:
name: Compute sha_short
runs-on: ubuntu-latest
outputs:
sha_short: ${{ steps.vars.outputs.sha_short }}

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set vars
id: vars
run: |
echo "sha_short=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_OUTPUT

build-push:
name: Build and Push Images
needs: [compute-sha]
uses: ./.github/workflows/cd.yaml
with:
tag: ${{ needs.compute-sha.outputs.sha_short }}
secrets: inherit

deploy:
name: Deploy with SSH
needs: [compute-sha, build-push]
runs-on: ubuntu-latest
environment: development

steps:
- name: SSH and Helm Install
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: root
key: ${{ secrets.SSH_KEY }}
script: |
cd ./infra
helm uninstall bt-dev-app-${{ needs.compute-sha.outputs.sha_short }} || true
helm install bt-dev-app-${{ needs.compute-sha.outputs.sha_short }} ./app --namespace=bt \
--set env=dev \
--set ttl=${{ inputs.ttl }} \
--set-string frontend.image.tag=${{ needs.compute-sha.outputs.sha_short }} \
--set-string backend.image.tag=${{ needs.compute-sha.outputs.sha_short }} \
--set host=${{ needs.compute-sha.outputs.sha_short }}.stanfurdtime.com \
--set mongoUri=mongodb://bt-dev-mongo-mongodb.bt.svc.cluster.local:27017/bt \
--set redisUri=redis://bt-dev-redis-master.bt.svc.cluster.local:6379 \
--set nodeEnv=development
30 changes: 0 additions & 30 deletions .github/workflows/cd-master.yml

This file was deleted.

46 changes: 46 additions & 0 deletions .github/workflows/cd-prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Deploy to Production

concurrency: prod

on:
workflow_dispatch:

jobs:
branch-check:
name: Environment Check
runs-on: ubuntu-latest
environment: production

steps:
- name: Pass
run: echo "Passed check"

build-push:
name: Build and Push Images
needs: [branch-check]
uses: ./.github/workflows/cd.yaml
with:
tag: prod
secrets: inherit

deploy:
name: Deploy with SSH
needs: [build-push]
runs-on: ubuntu-latest

steps:
- name: SSH and Helm Install
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: root
key: ${{ secrets.SSH_KEY }}
script: |
cd ./infra
if helm status bt-prod-app ; then
kubectl rollout restart bt-prod-app-backend
kubectl rollout restart bt-prod-app-frontend
else
helm install bt-prod-app ./app --namespace=bt \
--set host=stanfurdtime.com
fi
43 changes: 43 additions & 0 deletions .github/workflows/cd-stage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Deploy to Staging

concurrency: stage

on:
push:
branches: [master, gql]

jobs:
build-push:
name: Build and Push Images
uses: ./.github/workflows/cd.yaml
with:
tag: latest
secrets: inherit

deploy:
name: Deploy with SSH
needs: [build-push]
runs-on: ubuntu-latest
environment: staging

steps:
- name: SSH and Helm Install
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: root
key: ${{ secrets.SSH_KEY }}
script: |
cd ./infra
if helm status bt-stage-app ; then
kubectl rollout restart bt-stage-app-backend
kubectl rollout restart bt-stage-app-frontend
else
helm install bt-stage-app ./app --namespace=bt \
--set env=stage \
--set frontend.image.tag=latest \
--set backend.image.tag=latest \
--set host=staging.stanfurdtime.com \
--set mongoUri=mongodb://bt-stage-mongo-mongodb.bt.svc.cluster.local:27017/bt \
--set redisUri=redis://bt-stage-redis-master.bt.svc.cluster.local:6379 \
fi
48 changes: 48 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Deploy to Development

on:
workflow_call:
inputs:
tag:
description: "Image build tag"
required: true
type: string

jobs:
build-push-backend:
name: Build and Push Backend Image
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and Push Image with Tag
run: |
docker build --no-cache --platform=linux/amd64 --target backend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ inputs.tag }}" .
docker push "${{ secrets.DOCKER_USERNAME }}/bt-backend:${{ inputs.tag }}"

build-push-frontend:
name: Build and Push Frontend Image
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and Push Image with Tag
run: |
docker build --no-cache --platform=linux/amd64 --target frontend-prod --tag "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ inputs.tag }}" .
docker push "${{ secrets.DOCKER_USERNAME }}/bt-frontend:${{ inputs.tag }}"
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,4 @@ WORKDIR /frontend

RUN ["turbo", "run", "build", "--filter=frontend", "--env-mode=loose"]

COPY /apps/frontend/dist ./apps/frontend/dist
ENTRYPOINT ["turbo", "run", "start", "--filter=frontend"]
16 changes: 13 additions & 3 deletions infra/app/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Labels applied to all resources.
helm.sh/chart: {{ include "bt-app.chart" . }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
app.kubernetes.io/instance: {{ .Release.Name }}
env: {{ .Values.env }}
{{- end -}}

{{- define "bt-app.backendLabels" -}}
Expand All @@ -24,6 +25,11 @@ app.kubernetes.io/name: frontend
{{ include "bt-app.labels" . }}
{{- end -}}

{{- define "bt-app.updaterLabels" -}}
app.kubernetes.io/name: updater
{{ include "bt-app.labels" . }}
{{- end -}}

{{- define "bt-app.backendName" -}}
{{ .Release.Name }}-backend
{{- end -}}
Expand All @@ -32,6 +38,10 @@ app.kubernetes.io/name: frontend
{{ .Release.Name }}-frontend
{{- end -}}

{{- define "bt-app.cronJobName" -}}
{{ .Release.Name }}-cronjob
{{- end -}}
{{- define "bt-app.updaterName" -}}
{{ .Release.Name }}-updater
{{- end -}}

{{- define "bt-app.cleanupName" -}}
{{ .Release.Name }}-cleanup
{{- end -}}
2 changes: 1 addition & 1 deletion infra/app/templates/backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
containers:
- name: backend
image: {{ printf "%s/%s:%s" .Values.backend.image.registry .Values.backend.image.repository .Values.backend.image.tag }}
command: {{ .Values.backend.command | toJson}}
imagePullPolicy: Always
ports:
- containerPort: {{ .Values.backend.port }}
env:
Expand Down
16 changes: 16 additions & 0 deletions infra/app/templates/cleanup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{ if eq .Values.env "dev" }}
# only run cleanup job in dev environment
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "bt-app.cleanupName" . }}
spec:
template:
spec:
serviceAccountName: bt-app-cleanup
containers:
- name: cleanup
image: alpine/helm
command: ['sh', '-c', 'sleep $(( {{ .Values.ttl }} * 60 * 60 )); helm uninstall {{ .Release.Name }}']
restartPolicy: Never
{{ end }}
2 changes: 1 addition & 1 deletion infra/app/templates/frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
containers:
- name: frontend
image: {{ printf "%s/%s:%s" .Values.frontend.image.registry .Values.frontend.image.repository .Values.frontend.image.tag }}
command: {{ .Values.frontend.command | toJson }}
imagePullPolicy: Always
ports:
- containerPort: {{ .Values.frontend.port }}

Expand Down
8 changes: 6 additions & 2 deletions infra/app/templates/updater.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{{ if not (eq .Values.env "dev") }}
# do not run CronJob in dev environment
apiVersion: batch/v1
kind: CronJob
metadata:
name: {{ include "bt-app.cronJobName" . }}
name: {{ include "bt-app.updaterName" . }}
labels:
{{- include "bt-app.backendLabels" . | nindent 4 }}
{{- include "bt-app.updaterLabels" . | nindent 4 }}
spec:
schedule: {{ .Values.updater.schedule }}
suspend: {{ .Values.updater.suspend }}
Expand All @@ -14,6 +16,7 @@ spec:
containers:
- name: backend-cron-job
image: {{ printf "%s/%s:%s" .Values.backend.image.registry .Values.backend.image.repository .Values.backend.image.tag }}
imagePullPolicy: Always
command: {{ .Values.updater.command | toJson }}
ports:
- containerPort: {{ .Values.backend.port }}
Expand Down Expand Up @@ -47,3 +50,4 @@ spec:
- name: SESSION_SECRET
value: "_"
restartPolicy: OnFailure
{{ end }}
Loading
Loading