-
Notifications
You must be signed in to change notification settings - Fork 50
103 lines (92 loc) · 4.56 KB
/
deployment-pr-cleanup.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
name: CD for feature branches (cleanup job)
on:
schedule:
- cron: '*/30 * * * *' # Run every 30 minutes
workflow_dispatch: # Allow triggering manually
jobs:
find-prs-to-undeploy:
name: Find PRs to uninstall
runs-on: ubuntu-latest
outputs:
to-uninstall: ${{ steps.to-uninstall.outputs.list }}
never-uninstall: ${{ steps.to-uninstall.outputs.never_uninstall }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Find all open PRs that have a "deploy!" label
id: deployment-candidates
# create a list of JSON objects like:
# [
# {"name": "pr1234", "sha": "... commit sha ...", "number": "1234"}
# ]
run: |
PULL_REQUESTS=$(curl -X POST --location "https://api.github.com/graphql" \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${{ secrets.REPO_ACCESS_TOKEN }}" \
-d "{\"query\": \"query { search(query: \\\"is:open is:pr label:deploy! repo:${{ github.repository }}\\\", type: ISSUE, first: 100) { edges { node { ... on PullRequest { number headRefOid } } } } }\"}")
LIST=$(printf "$PULL_REQUESTS" | jq '.data.search.edges' \
| jq 'map({"name":("pr"+(.node.number|tostring)),sha:.node.headRefOid,number:(.node.number|tostring)})' )
echo "Deployment candidates: $LIST"
echo "list=$LIST" | tr -d "\n" >> $GITHUB_OUTPUT
- name: Read the list of all currently active deployments
id: current-deployments
run: |
# Setup authentication
mkdir ~/.kube && echo '${{ secrets.KUBECONFIG }}' > ~/.kube/config && chmod go-r ~/.kube/config
# Read list of deployments using helm, and remove the leading ecamp3- from each of the names.
# Creates a list of objects like [{"name": "pr1234", "sha": "... commit sha ..."}, {"name": "dev", "sha": "..."}]
LIST=$(helm list -o json | jq 'map(.name|=sub("^ecamp3-";""))' | jq 'map({name:.name,sha:.app_version})')
echo "Currently active deployments: $LIST"
echo "list=$LIST" | tr -d "\n" >> $GITHUB_OUTPUT
- name: List deployments without PR
id: to-uninstall
env:
deployments: ${{ toJSON(fromJSON(steps.current-deployments.outputs.list).*.name) }}
prs: ${{ toJSON(fromJSON(steps.deployment-candidates.outputs.list).*.name) }}
never_uninstall: '["dev", "devel", "stage", "staging", "prod"]'
run: |
TO_UNINSTALL=$(jq --null-input --argjson prs '${{ env.prs }}' --argjson deployments '${{ env.deployments }}' --argjson never_uninstall '${{ env.never_uninstall }}' '$deployments-$prs-$never_uninstall')
echo "Will uninstall: $TO_UNINSTALL"
echo "list=$TO_UNINSTALL" | tr -d "\n" >> $GITHUB_OUTPUT
echo "\n" >> $GITHUB_OUTPUT
echo "never_uninstall=$never_uninstall" | tr -d "\n" >> $GITHUB_OUTPUT
uninstall-old-deployment:
name: Uninstall old deployment
runs-on: ubuntu-latest
needs:
- find-prs-to-undeploy
if: fromJSON(needs.find-prs-to-undeploy.outputs.to-uninstall)[0] != null
strategy:
fail-fast: false
matrix:
deployment-name: ${{ fromJSON(needs.find-prs-to-undeploy.outputs.to-uninstall) }}
steps:
- name: Uninstall helm release
run: |
# Setup authentication
mkdir ~/.kube && echo '${{ secrets.KUBECONFIG }}' > ~/.kube/config && chmod go-r ~/.kube/config
# Uninstall the release
helm delete ecamp3-${{ matrix.deployment-name }}
- name: Remove GitHub deployment
uses: bobheadxi/[email protected]
with:
step: deactivate-env
token: ${{ secrets.REPO_ACCESS_TOKEN }}
env: ${{ matrix.deployment-name }}
- name: Find former PR number based on deployment name
id: pr-number
env:
name: ${{ matrix.deployment-name }}
regex: '^pr([0-9]+)$'
run: |
[[ $name =~ $regex ]]
echo number=${BASH_REMATCH[1]} >> $GITHUB_OUTPUT
- name: Comment about uninstallation on PR
uses: thollander/actions-comment-pull-request@v3
if: steps.pr-number.outputs.number
with:
pr-number: ${{ steps.pr-number.outputs.number }}
message: |
### <span aria-hidden="true">⛔</span> Feature branch deployment currently inactive.
If the PR is still open, you can add the `deploy!` label to this PR to trigger a feature branch deployment.
comment-tag: feature-branch-deployment-status
create-if-not-exists: false