-
Notifications
You must be signed in to change notification settings - Fork 0
121 lines (104 loc) · 4.69 KB
/
Copy pathcreate-draft-release.yml
File metadata and controls
121 lines (104 loc) · 4.69 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
name: Release / Create Draft
on:
workflow_dispatch:
pull_request:
types:
- closed
branches:
- main
permissions:
contents: write
jobs:
update-draft-releases:
name: "Delete old drafts and create new draft release"
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: github.event.pull_request.merged == true || github.event_name == 'workflow_dispatch'
steps:
- uses: actions/checkout@v6
- name: Get all draft releases
id: get_drafts
run: |
gh api repos/${{ github.repository }}/releases \
| jq '.[] | select(.draft == true) | .id' > release_ids.txt
- name: Delete old draft releases
run: |
while read release_id; do
echo "Deleting draft release with ID: $release_id"
gh api -X DELETE repos/${{ github.repository }}/releases/$release_id
done < release_ids.txt
- name: Determine next semantic version for release
run: |
PACKAGE_VERSION=$(jq -r '.version' package.json)
if [ -z "$PACKAGE_VERSION" ] || [ "$PACKAGE_VERSION" = "null" ]; then
echo "No version found in package.json"
exit 1
fi
# Validate MAJOR.MINOR.PATCH with optional semver pre-release suffix (e.g. -beta.0, -rc.1)
if [[ ! $PACKAGE_VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "Invalid version format ($PACKAGE_VERSION). Expected MAJOR.MINOR.PATCH or MAJOR.MINOR.PATCH-PRERELEASE"
exit 1
fi
# Preserve the full version (including any -beta.X / -rc.X suffix) in the tag
NEW_VERSION="v${PACKAGE_VERSION}"
echo "NEW_VERSION=${NEW_VERSION}" >> $GITHUB_ENV
# A pre-release suffix (anything after a "-") makes this a GitHub pre-release
if [[ "$PACKAGE_VERSION" == *-* ]]; then
echo "IS_PRERELEASE=true" >> $GITHUB_ENV
else
echo "IS_PRERELEASE=false" >> $GITHUB_ENV
fi
- name: Create Draft Release on GitHub
id: create_release
run: |
response=$(curl -s -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/${{ github.repository }}/releases \
-d '{
"tag_name": "'"${NEW_VERSION}"'",
"target_commitish": "main",
"draft": true,
"prerelease": '"${IS_PRERELEASE}"',
"make_latest": "'"$([ "${IS_PRERELEASE}" = "true" ] && echo false || echo legacy)"'",
"generate_release_notes": true
}')
# Check if release creation succeeded
if echo "$response" | jq -e '.id' > /dev/null; then
release_url=$(echo $response | jq -r '.html_url')
release_id=$(echo $response | jq -r '.id')
body=$(echo "$response" | jq -r '.body')
echo "release_url=${release_url}" >> $GITHUB_OUTPUT
echo "release_id=${release_id}" >> $GITHUB_OUTPUT
echo "$body" > release_body.txt
echo "Draft release created: ${release_url}"
else
echo "Failed to create draft release"
echo "$response" | jq '.'
exit 1
fi
- name: Clean up release notes
id: clean_release_notes
run: |
# Remove automation-related commits and remove "by @username in" from PR references
CLEANED_BODY=$(sed '/chore: bump version/Id; /Draft PR for release/Id; /Bump Version on PR to Main/Id; /docs: sync/Id; /revert version bump after failed publish/Id' release_body.txt | sed -E 's/ by @[^ ]+ in/ /g')
echo "$CLEANED_BODY" > cleaned_body.txt
echo "Cleaned release notes:"
cat cleaned_body.txt
- name: Update Draft Release with cleaned notes
run: |
CLEANED_BODY=$(jq -Rs '.' < cleaned_body.txt)
response=$(curl -s -X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/${{ github.repository }}/releases/${{ steps.create_release.outputs.release_id }} \
-d "{\"tag_name\": \"${NEW_VERSION}\", \"target_commitish\": \"main\", \"body\": ${CLEANED_BODY}}")
if echo "$response" | jq -e '.id' > /dev/null; then
echo "Release notes updated successfully"
echo "Draft release ready: ${{ steps.create_release.outputs.release_url }}"
else
echo "Warning: Failed to update release notes"
echo "$response" | jq '.'
fi