-
Notifications
You must be signed in to change notification settings - Fork 7
54 lines (47 loc) · 2.18 KB
/
clean-packagejson-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
name: (R) Update package.json
on:
workflow_call:
inputs:
branch:
description: "branch name to delete. Only delete branches that are created by github-actions[bot] and are not the default branch."
required: true
type: string
outputs:
branch-deleted:
description: "Indicate branch is deleted or not by boolean. true = branch deleted, false = branch not deleted."
value: ${{ jobs.cleanup.outputs.branch-deleted }}
jobs:
cleanup:
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # auto generated token
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
branch-deleted: ${{ steps.check-branch.outputs.deletable }}
steps:
- name: Check branch is deletable
id: check-branch
run: |
# Check if the branch is the default branch
if [[ "$(gh api /repos/${{ github.repository }} | jq -r '.default_branch')" == "${{ inputs.branch }}" ]]; then
echo "Branch is default, you cannot delete this branch. branch: ${{ inputs.branch }}"
exit 1
fi
# Check if the branch is created by github-actions[bot]
if gh api /repos/${{ github.repository }}/branches | jq -r '.[].name' | grep "${{ inputs.branch }}" >/dev/null; then
echo "branch exists. branch: ${{ inputs.branch }}"
# Check info of the branch
gh api /repos/${{ github.repository }}/branches/${{ inputs.branch }} | jq
if [[ "$(gh api /repos/${{ github.repository }}/branches/${{ inputs.branch }} | jq -r '.commit.author.login')" != "github-actions[bot]" ]]; then
echo "Branch is not created by github-actions[bot], you cannot delete this branch. branch: ${{ inputs.branch }}"
exit 1
fi
branch_deletable=true
else
echo "branch not exists. branch: ${{ inputs.branch }}"
branch_deletable=false
fi
echo "deletable=${branch_deletable}" | tee -a "${GITHUB_OUTPUT}"
- name: Delete branch
if: ${{ steps.check-branch.outputs.deletable == 'true' }}
run: gh api -X DELETE /repos/${{ github.repository }}/git/refs/heads/${{ inputs.branch }}