-
Notifications
You must be signed in to change notification settings - Fork 15
141 lines (125 loc) · 4.57 KB
/
Copy pathcd-dev.yaml
File metadata and controls
141 lines (125 loc) · 4.57 KB
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: "CD: Deploy to Development"
run-name: "Deployed `${{ github.sha }}` for `${{ github.event.inputs.ttl }}` hours"
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: 1
auth:
description: "Enable authentication support"
required: true
type: boolean
default: false
env:
HOSTS: "auth0.dev.berkeleytime.com auth1.dev.berkeleytime.com auth2.dev.berkeleytime.com auth3.dev.berkeleytime.com auth4.dev.berkeleytime.com auth5.dev.berkeleytime.com auth6.dev.berkeleytime.com auth7.dev.berkeleytime.com auth8.dev.berkeleytime.com auth9.dev.berkeleytime.com"
jobs:
compute-sha:
name: Compute sha_short and host
runs-on: ubuntu-latest
outputs:
sha_short: ${{ steps.vars.outputs.sha_short }}
host: ${{ steps.vars.outputs.host }}
steps:
- name: SSH and Query Hosts
uses: appleboy/ssh-action@v1.2.2
if: ${{ inputs.auth }}
id: ssh
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
capture_stdout: true
script: |
set -e # Exit immediately if a command fails
for host in ${{ env.HOSTS }}; do
if ! kubectl get ingress -o jsonpath='{.items[*].spec.rules[*].host}' | grep -qw "$host"; then
echo $host
exit 0
fi
done
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set vars
id: vars
run: |
sha_short=$(git rev-parse --short ${{ github.sha }})
auth_host=$(echo ${{ steps.ssh.outputs.stdout || '' }} | head -n 1)
echo "sha_short=${sha_short}" >> $GITHUB_OUTPUT
if [ -z $auth_host ]; then
echo "host=$sha_short.dev.berkeleytime.com" >> $GITHUB_OUTPUT
else
echo "host=$auth_host" >> $GITHUB_OUTPUT
fi
build-push:
name: Build and Push Images and Charts
needs: [compute-sha]
uses: ./.github/workflows/cd-build.yaml
with:
image_tag: ${{ needs.compute-sha.outputs.sha_short }}
chart_ver: 0.1.0-dev-${{ needs.compute-sha.outputs.sha_short }}
secrets: inherit
deploy:
name: SSH and Deploy
needs: [compute-sha, build-push]
uses: ./.github/workflows/cd-deploy.yaml
with:
environment: development
name: bt-dev-app-${{ needs.compute-sha.outputs.sha_short }}
version: 0.1.0-dev-${{ needs.compute-sha.outputs.sha_short }}
deploy_staff: false
values: |
env: dev
ttl: ${{ inputs.ttl }}
frontend:
image:
tag: '${{ needs.compute-sha.outputs.sha_short }}'
backend:
image:
tag: '${{ needs.compute-sha.outputs.sha_short }}'
semanticSearch:
image:
tag: '${{ needs.compute-sha.outputs.sha_short }}'
datapuller:
suspend: true
image:
tag: '${{ needs.compute-sha.outputs.sha_short }}'
host: ${{ needs.compute-sha.outputs.host }}
mongoUri: mongodb://bt-stage-mongo-mongodb-0.bt-stage-mongo-mongodb-headless.bt.svc.cluster.local:27017/bt
redisUri: redis://bt-stage-redis-master.bt.svc.cluster.local:6379
host: ${{ needs.compute-sha.outputs.host }}
secrets: inherit
limit-deploy:
name: SSH and Limit Deployments
needs: [deploy]
runs-on: ubuntu-latest
steps:
- name: SSH and Check Deployments
uses: appleboy/ssh-action@v1.2.0
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
set -e # Exit immediately if a command fails
# Get bt-dev-app- deployments sorted by creation timestamp
deployments=$(helm list \
--namespace=bt \
--date \
--short | grep '^bt-dev-app') || true
deployment_count=$(echo "$deployments" | wc -l)
# Check if deployment count > 8
if [ "$deployment_count" -gt 8 ]; then
echo "Too many deployments. Deleting the oldest deployment."
# Get oldest deployment from first line of deployments
oldest_deployment=$(echo "$deployments" | head -n 1)
# Uninstall deployment
helm uninstall "${oldest_deployment}"
else
echo "Deployment count is <= 8."
fi