-
-
Notifications
You must be signed in to change notification settings - Fork 67
208 lines (205 loc) · 9.61 KB
/
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
name: Deploy
on:
workflow_call:
inputs:
environment:
type: string
description: GitHub Environment Name
default: prod
required: false
docker-images-version:
type: string
description: Docker Images Version
default: latest
required: false
gke-cluster:
type: string
description: Google Kubernetes Engine Cluster
required: true
gke-zone:
type: string
description: Google Kubernetes Engine Zone
required: true
secrets:
gke-credentials:
description: Google Kubernetes Engine Credentials as JSON
required: true
gke-project:
description: Google Kubernetes Engine Project
required: true
gh-key:
description: Github authentication key
required: true
# cloudflare-api-token:
# description: Cloudflare API Token
# required: true
# cloudflare-zone-id:
# description: Cloudflare Zone Id
# required: true
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
permissions:
contents: 'read'
id-token: 'write'
steps:
- name: Checkout
uses: actions/checkout@v3
# gcloud does not work with Python 3.10 because collections.Mappings was removed in Python 3.10.
- uses: actions/setup-python@v4
with:
python-version: 3.9.15
- name: Auth gcloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.gke-credentials }}
- name: Setup gcloud
uses: google-github-actions/setup-gcloud@v1
with:
project_id: ${{ secrets.gke-project }}
- name: Configure gcloud
run: |
gcloud components install gke-gcloud-auth-plugin
gcloud --quiet auth configure-docker
gcloud container clusters get-credentials ${{ inputs.gke-cluster }} --zone ${{ inputs.gke-zone }}
# https://github.com/helm/helm/issues/8036
- name: Build helm dependencies
run: |
helm repo add bitnami https://charts.bitnami.com/bitnami/
helm repo add stable https://charts.helm.sh/stable/
helm dependency build ./helm/api-platform
- name: Define namespace
run: |
set -o pipefail
if [[ "${{ github.ref }}" == 'refs/heads/main' ]]; then
# Tags are deployed in prod
echo "CONTEXT=prod" >> "$GITHUB_ENV"
echo "RELEASE_NAME=website-prod" >> "$GITHUB_ENV"
echo "URL=api-platform.com" >> "$GITHUB_ENV"
echo 'CORS=["https://api-platform.com", "http://localhost", "https://localhost", "http://localhost:3000"]' >> "$GITHUB_ENV"
echo "NAMESPACE=prod-website" >> "$GITHUB_ENV"
echo "REPLICA=1" >> "$GITHUB_ENV"
echo "CPU_REQUEST=10m" >> "$GITHUB_ENV"
echo "MEM_REQUEST=400Mi" >> "$GITHUB_ENV"
echo "MEM_LIMIT=1400Mi" >> "$GITHUB_ENV"
else
CONTEXT=nonprod
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
echo RELEASE_NAME=pr-$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") >> "$GITHUB_ENV"
export RELEASE_NAME=pr-$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
else
echo "RELEASE_NAME=${{ github.ref_name }}" >> "$GITHUB_ENV"
export RELEASE_NAME=${{ github.ref_name }}
fi
echo "URL=$RELEASE_NAME.apip.preprod-tilleuls.ovh" >> "$GITHUB_ENV"
echo "REPLICA=1" >> "$GITHUB_ENV"
echo "NAMESPACE=nonprod-website" >> "$GITHUB_ENV"
echo 'CORS=["https://${{ env.RELEASE_NAME}}.apip.preprod-tilleuls.ovh", "http://localhost", "https://localhost", "http://localhost:3000"]' >> "$GITHUB_ENV"
echo "CPU_REQUEST=5m" >> "$GITHUB_ENV"
echo "MEM_REQUEST=100Mi" >> "$GITHUB_ENV"
echo "MEM_LIMIT=600Mi" >> "$GITHUB_ENV"
fi
- name: HELM Deploy
run: |
set -o pipefail
if ! helm -n ${{ env.NAMESPACE }} status ${{ env.RELEASE_NAME }} &>/dev/null; then
JWT_PASSPHRASE=$(openssl rand -base64 32)
JWT_SECRET_KEY=$(openssl genpkey -pass file:<(echo "$JWT_PASSPHRASE") -aes256 -algorithm rsa -pkeyopt rsa_keygen_bits:4096)
helm upgrade ${{ env.RELEASE_NAME }} ./helm/api-platform \
--reuse-values \
--install \
--create-namespace \
--debug \
--wait \
--atomic \
--namespace=${{ env.NAMESPACE }} \
--set=app.version=${{ github.sha }} \
--set=php.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/php \
--set=php.image.tag=${{ inputs.docker-images-version }} \
--set=php.image.pullPolicy=Always \
--set=caddy.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/caddy \
--set=caddy.image.tag=${{ inputs.docker-images-version }} \
--set=caddy.image.pullPolicy=Always \
--set=pwa.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/pwa \
--set=pwa.image.tag=${{ inputs.docker-images-version }} \
--set=pwa.image.pullPolicy=Always \
--set=pwa.resources.requests.cpu=${{ env.CPU_REQUEST }} \
--set=pwa.resources.requests.memory=${{ env.MEM_REQUEST }} \
--set=pwa.resources.limits.memory=${{ env.MEM_LIMIT }} \
--set=pwa.replicaCount=${{ env.REPLICA }} \
--set=bucket.s3Upstream=storage.googleapis.com \
--set=bucket.s3Name=api-platform-website-v3 \
--set=service.type=NodePort \
--set=ingress.enabled=true \
--set=ingress.hosts[0].host=${{ env.URL }} \
--set=ingress.hosts[0].paths[0].path=/ \
--set=ingress.hosts[0].paths[0].pathType=ImplementationSpecific \
--set=ingress.tls[0].hosts[0]=${{ env.URL }} \
--set=ingress.annotations."cert-manager\.io/cluster-issuer"=letsencrypt-production \
--set=ingress.tls[0].secretName=${{ env.RELEASE_NAME }}-website-ssl \
--set=php.jwt.secretKey="$JWT_SECRET_KEY" \
--set=php.jwt.publicKey="$(openssl pkey -in <(echo "$JWT_SECRET_KEY") -passin file:<(echo "$JWT_PASSPHRASE") -pubout)" \
--set=php.jwt.passphrase=$JWT_PASSPHRASE \
--set=php.corsAllowOrigin="^$(echo "${{ join(fromJSON(env.CORS), '|') }}" | sed 's/\./\\./g')$" \
--set=php.host=${{ env.URL }} \
--set=next.rootUrl=${{ env.URL }} \
--set=github.key=${{ secrets.gh-key }} \
--set=postgresql.global.postgresql.auth.password=$(openssl rand -base64 32 | tr -d "=+/") \
--set=postgresql.global.postgresql.auth.username=website \
| sed --unbuffered '/USER-SUPPLIED VALUES/,$d'
else
helm upgrade ${{ env.RELEASE_NAME }} ./helm/api-platform \
--reuse-values \
--install \
--create-namespace \
--debug \
--wait \
--atomic \
--namespace=${{ env.NAMESPACE }} \
--set=app.version=${{ github.sha }} \
--set=php.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/php \
--set=php.image.tag=${{ inputs.docker-images-version }} \
--set=php.image.pullPolicy=Always \
--set=caddy.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/caddy \
--set=caddy.image.tag=${{ inputs.docker-images-version }} \
--set=caddy.image.pullPolicy=Always \
--set=pwa.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/pwa \
--set=pwa.image.tag=${{ inputs.docker-images-version }} \
--set=pwa.image.pullPolicy=Always \
--set=pwa.resources.requests.cpu=${{ env.CPU_REQUEST }} \
--set=pwa.resources.requests.memory=${{ env.MEM_REQUEST }} \
--set=pwa.resources.limits.memory=${{ env.MEM_LIMIT }} \
--set=pwa.replicaCount=${{ env.REPLICA }} \
--set=php.corsAllowOrigin="^$(echo "${{ join(fromJSON(env.CORS), '|') }}" | sed 's/\./\\./g')$" \
--set=github.key=${{ secrets.gh-key }} \
--set=next.rootUrl=${{ env.URL }} \
--set=bucket.s3Upstream=storage.googleapis.com \
--set=bucket.s3Name=api-platform-website-v3 \
| sed --unbuffered '/USER-SUPPLIED VALUES/,$d'
fi
- name: Debug kube events
if: failure()
run: kubectl get events --namespace=${{ env.NAMESPACE }} --sort-by .metadata.creationTimestamp
links:
name: Check for dead links
needs: deploy
runs-on: ubuntu-latest
steps:
- name: Define release name
run: |
set -o pipefail
if [[ "${{ github.ref }}" == 'refs/heads/main' ]]; then
echo "RELEASE_NAME=website-prod" >> "$GITHUB_ENV"
else
CONTEXT=nonprod
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
echo RELEASE_NAME=pr-$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") >> "$GITHUB_ENV"
else
echo "RELEASE_NAME=${{ github.ref_name }}" >> "$GITHUB_ENV"
fi
fi
- name: Check for dead links
uses: lycheeverse/lychee-action@v1.8.0
with:
args: --verbose --no-progress https://${{ env.RELEASE_NAME}}.apip.preprod-tilleuls.ovh