-
Notifications
You must be signed in to change notification settings - Fork 1
62 lines (53 loc) · 2.07 KB
/
delete-gitops-branch.yaml
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
name: Delete gitops deployment branch
on:
pull_request:
types: [unlabeled, closed]
branches: ['main']
env:
HUSKY: 0
# save resources. Don't take up a queue space doing a duplicate build
concurrency:
group: delete-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
should-run:
runs-on: ubuntu-latest
outputs:
SHOULD_RUN: ${{ steps.should_run.outputs.SHOULD_RUN }}
steps:
- name: Check if this workflow should run
id: should_run
run: |
does_closed_pr_have_preview_label=${{ github.event.action == 'closed' && contains(github.event.pull_request.labels.*.name, 'preview') }}
was_preview_label_removed=${{ github.event.action == 'unlabeled' && github.event.label.name == 'preview' }}
should_run=false
if $does_closed_pr_have_preview_label || $was_preview_label_removed ; then
should_run=true
fi
echo "SHOULD_RUN=$should_run" >> "$GITHUB_OUTPUT"
get-pr-branch-name:
needs: should-run
if: needs.should-run.outputs.SHOULD_RUN == 'true'
uses: ./.github/workflows/get-pr-branch-name.yaml
delete-gitops-branch:
runs-on: ubuntu-latest
needs: [get-pr-branch-name, should-run]
if: needs.should-run.outputs.SHOULD_RUN == 'true'
steps:
- name: Setup gitops repo access
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.MANIFEST_REPO_DEPLOY_KEY }}
- name: Checkout CD repo
uses: actions/checkout@v4
with:
ssh-key: ${{ secrets.MANIFEST_REPO_DEPLOY_KEY }}
repository: ${{ secrets.MANIFEST_REPO }}
fetch-depth: 0 # get all branches. Needed for deleting gitops branch
- name: Delete branch in gitops
run: |
if git rev-parse --verify --quiet origin/${{ needs.get-pr-branch-name.outputs.BRANCH_NAME }}; then
git push origin --delete ${{ needs.get-pr-branch-name.outputs.BRANCH_NAME }}
else
echo "Branch ${{ needs.get-pr-branch-name.outputs.BRANCH_NAME }} already does not exist"
fi