Skip to content

Commit dc7c7e8

Browse files
authored
Implement PR deployment and prod / preprod enviroment (#402) (#404)
* Implement PR deployment and prod / preprod enviroment * Restore gcr subdirectory name * Add tag remover job * test syntax * Use GITHUB_ENV * Use github env in helm * Enable Build * Fix syntax * Try fix on helm release * Fix release name * debug cors * Edit cleanup job * Fix cors * Enable build * Change staging branch name * Remove bucket * Change preprod URLs * edit cors in helm upgrade * Add prod branch name temp
1 parent c91ee5d commit dc7c7e8

File tree

6 files changed

+65
-85
lines changed

6 files changed

+65
-85
lines changed

.github/workflows/cd.yml

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@ name: CD
33
on:
44
push:
55
branches:
6-
- deploy-v3
7-
- v3
6+
- prod
7+
- staging
88
# Deploy if "deploy" label exists
99
pull_request:
1010
types: [ reopened, synchronize, labeled ]
1111

1212
# Do not use concurrency to prevent simultaneous helm deployments
1313
jobs:
14+
remove-deploy-label:
15+
name: Remove deploy label
16+
if: github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'deploy')
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: mondeja/remove-labels-gh-action@v1
20+
with:
21+
token: ${{ secrets.GITHUB_TOKEN }}
22+
labels: |
23+
deploy
24+
1425
build:
1526
name: Build
16-
if: ${{ github.repository == 'api-platform/website' && (github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'deploy')) }}
27+
if: github.event_name != 'pull_request' || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'deploy'))
1728
uses: ./.github/workflows/build.yml
1829
with:
1930
tags: |
@@ -32,16 +43,11 @@ jobs:
3243
deploy:
3344
name: Deploy
3445
needs: [ build ]
35-
if: github.event_name == 'push'
46+
if: github.event_name != 'pull_request' || (github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'deploy'))
3647
uses: ./.github/workflows/deploy.yml
3748
with:
3849
environment: prod
39-
url: test-v3.preprod-tilleuls.ovh
4050
docker-images-version: ${{ github.sha }}
41-
cors: '["https://test-v3.preprod-tilleuls.ovh", "http://localhost", "https://localhost", "http://localhost:3000"]'
42-
release: website
43-
namespace: website
44-
4551
gke-cluster: api-platform-demo
4652
gke-zone: europe-west1-c
4753
secrets:
@@ -50,24 +56,3 @@ jobs:
5056
gh-key: ${{ secrets.GH_KEY }}
5157
# cloudflare-api-token: ${{ secrets.CF_API_TOKEN }}
5258
# cloudflare-zone-id: ${{ secrets.CF_ZONE_ID }}
53-
54-
feature-deploy:
55-
name: Feature Deploy
56-
needs: [ build ]
57-
if: github.event_name == 'pull_request'
58-
uses: ./.github/workflows/deploy.yml
59-
with:
60-
environment: ${{ needs.build.outputs.version }}
61-
url: ${{ needs.build.outputs.version }}-test-v3.preprod-tilleuls.ovh
62-
docker-images-version: ${{ needs.build.outputs.version }}
63-
cors: '["https://${{ needs.build.outputs.version }}-test-v3.preprod-tilleuls.ovh", "http://localhost", "https://localhost", "http://localhost:3000"]'
64-
release: ${{ needs.build.outputs.version }}
65-
namespace: ${{ needs.build.outputs.version }}
66-
gke-cluster: api-platform-demo
67-
gke-zone: europe-west1-c
68-
secrets:
69-
gke-credentials: ${{ secrets.GKE_SA_KEY }}
70-
gke-project: ${{ secrets.GKE_PROJECT }}
71-
gh-key: ${{ secrets.GH_KEY }}
72-
# cloudflare-api-token: ${{ secrets.CF_API_TOKEN }}
73-
# cloudflare-zone-id: ${{ secrets.CF_ZONE_ID }}

.github/workflows/cleanup.yml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ jobs:
3333
gcloud components install gke-gcloud-auth-plugin
3434
gcloud --quiet auth configure-docker
3535
gcloud container clusters get-credentials api-platform-demo --zone europe-west1-c
36-
- name: Check for existing namespace
37-
id: k8s-namespace
38-
run: echo "namespace=$(kubectl get namespace pr-${{ github.event.number }} | tr -d '\n' 2> /dev/null)" >> $GITHUB_OUTPUT
39-
- name: Uninstall release
40-
if: steps.k8s-namespace.outputs.namespace != ''
41-
run: kubectl delete namespace pr-${{ github.event.number }}
36+
- name: Uninstall helm release
37+
id: uninstall_helm_release
38+
run: |
39+
export RELEASE_NAME=pr-$(jq --raw-output .pull_request.number $GITHUB_EVENT_PATH)
40+
echo "Uninstalling release ${RELEASE_NAME}"
41+
if ! helm uninstall ${RELEASE_NAME} --kube-context nonprod --wait ; then
42+
echo "HELM Uninstall has failed !"
43+
echo "Please ask the SRE team to manually clean remaining objects"
44+
exit 1
45+
fi
46+
echo "HELM uninstall successfull"
47+
echo "Cleaning remaining PVC..."
48+
kubectl delete pvc -l app.kubernetes.io/instance=$RELEASE_NAME

.github/workflows/deploy.yml

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,11 @@ on:
88
description: GitHub Environment Name
99
default: prod
1010
required: false
11-
url:
12-
type: string
13-
description: GitHub Environment Url (without scheme)
14-
required: true
1511
docker-images-version:
1612
type: string
1713
description: Docker Images Version
1814
default: latest
1915
required: false
20-
cors:
21-
type: string
22-
description: CORS
23-
required: true
24-
release:
25-
type: string
26-
description: Release Name
27-
required: true
28-
namespace:
29-
type: string
30-
description: Namespace Name
31-
required: true
3216
gke-cluster:
3317
type: string
3418
description: Google Kubernetes Engine Cluster
@@ -58,9 +42,6 @@ jobs:
5842
deploy:
5943
name: Deploy
6044
runs-on: ubuntu-latest
61-
environment:
62-
name: ${{ inputs.environment }}
63-
url: https://${{ inputs.url }}
6445
permissions:
6546
contents: 'read'
6647
id-token: 'write'
@@ -90,9 +71,32 @@ jobs:
9071
helm repo add bitnami https://charts.bitnami.com/bitnami/
9172
helm repo add stable https://charts.helm.sh/stable/
9273
helm dependency build ./helm/api-platform
74+
- name: Define namespace
75+
run: |
76+
set -o pipefail
77+
if [[ "${{ github.ref }}" == 'refs/heads/prod' ]]; then
78+
# Tags are deployed in prod
79+
echo "CONTEXT=prod" >> "$GITHUB_ENV"
80+
echo "RELEASE_NAME=website-prod" >> "$GITHUB_ENV"
81+
echo "URL=api-platform.com" >> "$GITHUB_ENV"
82+
echo "CORS='[\"https://api-platform.com\", \"http://localhost\", \"https://localhost\", \"http://localhost:3000\"]'" >> "$GITHUB_ENV"
83+
echo "NAMESPACE=prod-website" >> "$GITHUB_ENV"
84+
else
85+
CONTEXT=nonprod
86+
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then
87+
echo RELEASE_NAME=pr-$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") >> "$GITHUB_ENV"
88+
export RELEASE_NAME=pr-$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
89+
else
90+
echo "RELEASE_NAME=${{ github.ref_name }}" >> "$GITHUB_ENV"
91+
export RELEASE_NAME=${{ github.ref_name }}
92+
fi
93+
echo "URL=$RELEASE_NAME.apip.preprod-tilleuls.ovh" >> "$GITHUB_ENV"
94+
echo "NAMESPACE=nonprod-website" >> "$GITHUB_ENV"
95+
echo 'CORS=["https://${{ env.RELEASE_NAME}}.apip.preprod-tilleuls.ovh", "http://localhost", "https://localhost", "http://localhost:3000"]' >> "$GITHUB_ENV"
96+
fi
9397
- name: Check for existing namespace
9498
id: k8s-namespace
95-
run: echo "namespace=$(kubectl get namespace ${{ inputs.namespace }} | tr -d '\n' 2> /dev/null)" >> $GITHUB_OUTPUT
99+
run: echo "namespace=$(kubectl get namespace ${{ env.NAMESPACE }} | tr -d '\n' 2> /dev/null)" >> $GITHUB_OUTPUT
96100
# Release name MUST start with a letter
97101
# GitHub doesn't support multilines environment variables (JWT_*_KEY)
98102
- name: Deploy in new namespace
@@ -101,14 +105,14 @@ jobs:
101105
set -o pipefail
102106
JWT_PASSPHRASE=$(openssl rand -base64 32)
103107
JWT_SECRET_KEY=$(openssl genpkey -pass file:<(echo "$JWT_PASSPHRASE") -aes256 -algorithm rsa -pkeyopt rsa_keygen_bits:4096)
104-
helm upgrade ${{ inputs.release }} ./helm/api-platform \
108+
helm upgrade ${{ env.RELEASE_NAME }} ./helm/api-platform \
105109
--reuse-values \
106110
--install \
107111
--create-namespace \
108112
--debug \
109113
--wait \
110114
--atomic \
111-
--namespace=${{ inputs.namespace }} \
115+
--namespace=${{ env.NAMESPACE }} \
112116
--set=app.version=${{ github.sha }} \
113117
--set=php.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/php \
114118
--set=php.image.tag=${{ inputs.docker-images-version }} \
@@ -123,18 +127,18 @@ jobs:
123127
--set=bucket.s3Name=api-platform-website-v3 \
124128
--set=service.type=NodePort \
125129
--set=ingress.enabled=true \
126-
--set=ingress.hosts[0].host=${{ inputs.url }} \
130+
--set=ingress.hosts[0].host=${{ env.URL }} \
127131
--set=ingress.hosts[0].paths[0].path=/ \
128132
--set=ingress.hosts[0].paths[0].pathType=ImplementationSpecific \
129-
--set=ingress.tls[0].hosts[0]=${{ inputs.url }} \
133+
--set=ingress.tls[0].hosts[0]=${{ env.URL }} \
130134
--set=ingress.annotations."cert-manager\.io/cluster-issuer"=letsencrypt-production \
131-
--set=ingress.tls[0].secretName=website-ssl \
135+
--set=ingress.tls[0].secretName=${{ env.RELEASE_NAME }}-website-ssl \
132136
--set=php.jwt.secretKey="$JWT_SECRET_KEY" \
133137
--set=php.jwt.publicKey="$(openssl pkey -in <(echo "$JWT_SECRET_KEY") -passin file:<(echo "$JWT_PASSPHRASE") -pubout)" \
134138
--set=php.jwt.passphrase=$JWT_PASSPHRASE \
135-
--set=php.corsAllowOrigin="^$(echo "${{ join(fromJSON(inputs.cors), '|') }}" | sed 's/\./\\./g')$" \
136-
--set=php.host=${{ inputs.url }} \
137-
--set=next.rootUrl=${{ inputs.url }} \
139+
--set=php.corsAllowOrigin="^$(echo "${{ join(fromJSON(env.CORS), '|') }}" | sed 's/\./\\./g')$" \
140+
--set=php.host=${{ env.URL }} \
141+
--set=next.rootUrl=${{ env.URL }} \
138142
--set=github.key=${{ secrets.gh-key }} \
139143
--set=ressources.requests.cpu=250m \
140144
--set=ressources.requests.memory=256Mi \
@@ -146,14 +150,14 @@ jobs:
146150
if: steps.k8s-namespace.outputs.namespace != ''
147151
run: |
148152
set -o pipefail
149-
helm upgrade ${{ inputs.release }} ./helm/api-platform \
153+
helm upgrade ${{ env.RELEASE_NAME }} ./helm/api-platform \
150154
--reuse-values \
151155
--install \
152156
--create-namespace \
153157
--debug \
154158
--wait \
155159
--atomic \
156-
--namespace=${{ inputs.namespace }} \
160+
--namespace=${{ env.NAMESPACE }} \
157161
--set=app.version=${{ github.sha }} \
158162
--set=php.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/php \
159163
--set=php.image.tag=${{ inputs.docker-images-version }} \
@@ -164,9 +168,9 @@ jobs:
164168
--set=pwa.image.repository=eu.gcr.io/${{ secrets.gke-project }}/website/pwa \
165169
--set=pwa.image.tag=${{ inputs.docker-images-version }} \
166170
--set=pwa.image.pullPolicy=Always \
167-
--set=php.corsAllowOrigin="^$(echo "${{ join(fromJSON(inputs.cors), '|') }}" | sed 's/\./\\./g')$" \
171+
--set=php.corsAllowOrigin="^$(echo "${{ join(fromJSON(env.CORS), '|') }}" | sed 's/\./\\./g')$" \
168172
--set=github.key=${{ secrets.gh-key }} \
169-
--set=next.rootUrl=${{ inputs.url }} \
173+
--set=next.rootUrl=${{ env.URL }} \
170174
--set=ressources.requests.cpu=250m \
171175
--set=ressources.requests.memory=256Mi \
172176
--set=ressources.limits.memory=700Mi \
@@ -175,4 +179,4 @@ jobs:
175179
| sed --unbuffered '/USER-SUPPLIED VALUES/,$d'
176180
- name: Debug kube events
177181
if: failure()
178-
run: kubectl get events --namespace=${{ inputs.namespace }} --sort-by .metadata.creationTimestamp
182+
run: kubectl get events --namespace=${{ env.NAMESPACE }} --sort-by .metadata.creationTimestamp

helm/api-platform/templates/configmap.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ data:
1212
php-trusted-proxies: "{{ join "," .Values.php.trustedProxies }}"
1313
mercure-url: "http://{{ include "api-platform.fullname" . }}/.well-known/mercure"
1414
mercure-public-url: {{ .Values.mercure.publicUrl | default "http://127.0.0.1/.well-known/mercure" | quote }}
15-
bucket-s3-upstream: {{ .Values.bucket.s3Upstream | quote }}
16-
bucket-s3-name: {{ .Values.bucket.s3Name | quote }}
1715
next-root-url: {{ .Values.next.rootUrl | quote }}

helm/api-platform/templates/deployment.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ spec:
4848
secretKeyRef:
4949
name: {{ include "api-platform.fullname" . }}
5050
key: mercure-jwt-secret
51-
- name: BUCKET_S3_UPSTREAM
52-
valueFrom:
53-
configMapKeyRef:
54-
name: {{ include "api-platform.fullname" . }}
55-
key: bucket-s3-upstream
56-
- name: BUCKET_S3_NAME
57-
valueFrom:
58-
configMapKeyRef:
59-
name: {{ include "api-platform.fullname" . }}
60-
key: bucket-s3-name
6151
ports:
6252
- name: http
6353
containerPort: 80

helm/api-platform/values.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ next:
3434
github:
3535
key: changeMe
3636

37-
bucket:
38-
s3Upstream: ChangeMe
39-
s3Name: ChangeMe
40-
4137
caddy:
4238
image:
4339
repository: "chart-example.local/api-platform/caddy"

0 commit comments

Comments
 (0)