Skip to content

Commit

Permalink
build(preview): add branch preview cleanup workflow (#1023)
Browse files Browse the repository at this point in the history
* build(preview): cleanup kubernetes resources on preview branch closure

* build(preview): remove preview active label
  • Loading branch information
chrispymm authored Dec 17, 2024
1 parent 4b9995f commit 0fd695d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/branch-preview-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Cleanup branch preview
on:
pull_request:
types: [closed]

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ECR_NAME: ${{ secrets.ECR_NAME }}
KUBE_CLUSTER: ${{ secrets.KUBE_CLUSTER }}
KUBE_NAMESPACE: ${{ secrets.KUBE_NAMESPACE }}

jobs:
cleanup-preview:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
pull-requests: write
if: contains(github.event.pull_request.labels.*.name, 'preview:active')
steps:
- run: |
echo "${KUBE_CERT}" > ca.crt
kubectl config set-cluster ${KUBE_CLUSTER} --certificate-authority=./ca.crt --server=https://${KUBE_CLUSTER}
kubectl config set-credentials deploy-user --token=${KUBE_TOKEN}
kubectl config set-context ${KUBE_CLUSTER} --cluster=${KUBE_CLUSTER} --user=deploy-user --namespace=${KUBE_NAMESPACE}
kubectl config use-context ${KUBE_CLUSTER}
kubectl delete deployment moj-frontend-${{ github.head_ref }} -n ${KUBE_NAMESPACE}
kubectl delete service moj-frontend-service-${{ github.head_ref }} -n ${KUBE_NAMESPACE}
kubectl delete ingress moj-frontend-ingress-${{ github.head_ref }} -n ${KUBE_NAMESPACE}
env:
KUBE_CERT: ${{ secrets.KUBE_CERT }}
KUBE_TOKEN: ${{ secrets.KUBE_TOKEN }}
KUBE_CLUSTER: ${{ secrets.KUBE_CLUSTER }}
- name: Remove preview:active label
run: |
gh pr edit $PRNUM --remove-label "preview:active"
env:
PRNUM: ${{ github.event.number }}

0 comments on commit 0fd695d

Please sign in to comment.