-
Notifications
You must be signed in to change notification settings - Fork 30
84 lines (77 loc) · 3.53 KB
/
delete-pr-build-on-close.yml
File metadata and controls
84 lines (77 loc) · 3.53 KB
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
name: Delete pre-release when a branch is deleted
# This workflow action deletes pre-releases when a PR is merged or closed.
#
# The CircleCI configuration builds CLI binaries when a PR is opened.
# These are uploaded to the upstream project as GitHub (pre-)releases.
#
# The release tag matches one of the following patterns:
# - v1.2.3-example-branch-placeholder # Branches on upstream
# - v1.2.3-pull-12-head # Branches from forks
#
# A "pull_request_target" event is used to delete pre-releases upstream.
#
# See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-your-pull_request-workflow-when-a-pull-request-merges
# See https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request_target
on:
pull_request_target: # zizmor: ignore[dangerous-triggers]
types:
- closed
jobs:
delete-pre-release:
name: Delete pre-release if exists
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Delete pre-release and tag named after branch
env:
GH_TOKEN: ${{ github.token }}
PR_BRANCH: ${{ github.event.pull_request.head.ref }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_REPO: ${{ github.event.pull_request.head.repo.full_name }}
shell: bash
run: |
# Use either an upstream or fork PR branch
if [[ "$PR_REPO" != "slackapi/slack-cli" ]]; then
BRANCH="pull/$PR_NUMBER/head"
else
BRANCH="$PR_BRANCH"
fi
# Escape tags to create a semantic version
REF=$(echo "${BRANCH}" | sed 's/\//-/g')
RELEASE_FOUND=1
# Delete tags matching the pull request branch from forks
if GH_DEBUG=1 gh release --repo="slackapi/slack-cli" delete "${REF}" -y --cleanup-tag; then
echo "Successfully deleted ${REF}"
RELEASE_FOUND=0
else
echo "Did not find ${REF} tag, trying version prefixes..."
fi
# Figure out tag name from branch name. This is coupled to the tag name generation that exists in .circleci/config.yml's `create-github-release` job.
RELEASES=$(gh release list --repo="slackapi/slack-cli" --order="desc" --json="tagName" --exclude-drafts --exclude-pre-releases --limit=24 --jq ".[] | .tagName")
for TAGS in $RELEASES; do
TAG_NAME="${TAGS}-${REF}"
echo "Identified pre-release tagname to 🔪: $TAG_NAME"
# Delete the pre-release
if GH_DEBUG=1 gh release --repo="slackapi/slack-cli" delete "$TAG_NAME" -y --cleanup-tag; then
echo "Successfully deleted $TAG_NAME"
RELEASE_FOUND=0
else
echo "Failed to find $TAG_NAME, trying next..."
fi
sleep 1
FEATURE_TAG_NAME="${TAGS}-${REF}-feature"
echo "Identified feature-release tagname as 🔪: $FEATURE_TAG_NAME"
# Delete a feature-release
if GH_DEBUG=1 gh release --repo="slackapi/slack-cli" delete "$FEATURE_TAG_NAME" -y --cleanup-tag; then
echo "Successfully deleted $FEATURE_TAG_NAME"
RELEASE_FOUND=0
else
echo "Failed to find $FEATURE_TAG_NAME, trying next..."
fi
sleep 1
done
if [ "$RELEASE_FOUND" -ne 0 ]; then
echo "No matching pre-releases tag was found for the branch $TAG_NAME in recent versions"
fi
exit "$RELEASE_FOUND"