Skip to content

Commit 0a6a71b

Browse files
committed
Extract workflow scripts into standalone files
Signed-off-by: Bryan Richter <b@chreekat.net>
1 parent fc44bac commit 0a6a71b

5 files changed

Lines changed: 89 additions & 91 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
# Checks if there are new commits since the latest tag.
3+
# Requires: GH_TOKEN, GITHUB_REPOSITORY, GITHUB_OUTPUT
4+
set -eu
5+
6+
REPO="${GITHUB_REPOSITORY}"
7+
TAG="$(gh api "repos/${REPO}/tags" --jq '.[0].name' 2>/dev/null || echo '')"
8+
9+
if [ -z "$TAG" ]; then
10+
echo "No tags found, proceeding with release."
11+
echo "has_commits=true" >> "$GITHUB_OUTPUT"
12+
else
13+
AHEAD="$(gh api "repos/${REPO}/compare/${TAG}...main" --jq '.ahead_by')"
14+
if [ "$AHEAD" -eq 0 ]; then
15+
echo "No new commits since $TAG, skipping release."
16+
echo "has_commits=false" >> "$GITHUB_OUTPUT"
17+
else
18+
echo "$AHEAD new commits since $TAG."
19+
echo "has_commits=true" >> "$GITHUB_OUTPUT"
20+
fi
21+
fi

.github/scripts/check-tag.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
# Reads VERSION file and checks if the corresponding git tag already exists.
3+
# Requires: GITHUB_OUTPUT, GITHUB_ENV
4+
set -eu
5+
6+
VERSION="$(cat VERSION)"
7+
TAG="v${VERSION}"
8+
echo "VERSION=$VERSION" >> "$GITHUB_ENV"
9+
echo "TAG=$TAG" >> "$GITHUB_ENV"
10+
11+
if git rev-parse "$TAG" >/dev/null 2>&1; then
12+
echo "Tag $TAG already exists, skipping release creation."
13+
echo "tag_exists=true" >> "$GITHUB_OUTPUT"
14+
else
15+
echo "Tag $TAG does not exist."
16+
echo "tag_exists=false" >> "$GITHUB_OUTPUT"
17+
fi

.github/scripts/prepare-release.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
# Increments the patch version, generates a changelog, and writes outputs
3+
# for the release PR workflow.
4+
# Requires: GITHUB_OUTPUT, GITHUB_ENV
5+
set -eu
6+
7+
# Increment patch version
8+
perl -i -pe 's/\b(\d+)(?=\D*$)/$1+1/e' VERSION
9+
10+
# Generate changelog
11+
TAG="$(git describe --tags --abbrev=0 2>/dev/null || git rev-list --max-parents=0 HEAD)"
12+
GIT_LOG="$(git log "$TAG"..HEAD --oneline --pretty=format:"- %s [%an]")"
13+
CHANGELOG="Changelog:
14+
${GIT_LOG}"
15+
# Strip '#' characters that break GitHub markdown rendering
16+
CHANGELOG="${CHANGELOG//'#'/''}"
17+
18+
VERSION="$(cat VERSION)"
19+
echo "VERSION=$VERSION" >> "$GITHUB_ENV"
20+
21+
echo "$CHANGELOG"
22+
23+
COMMIT_MSG="Release v${VERSION}
24+
25+
${CHANGELOG}"
26+
27+
PR_BODY="Auto-generated pull request for version ${VERSION}.
28+
29+
Please use **Squash and merge** to include the changelog in the release message.
30+
31+
${CHANGELOG}"
32+
33+
{
34+
echo "changelog<<EOF"
35+
echo "$CHANGELOG"
36+
echo "EOF"
37+
echo "commit_msg<<EOF"
38+
echo "$COMMIT_MSG"
39+
echo "EOF"
40+
echo "pr_body<<EOF"
41+
echo "$PR_BODY"
42+
echo "EOF"
43+
} >> "$GITHUB_OUTPUT"

.github/workflows/monthly-release.yml

Lines changed: 6 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,7 @@ jobs:
2020
id: check_commits
2121
env:
2222
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23-
run: |
24-
REPO="${{ github.repository }}"
25-
TAG="$(gh api "repos/${REPO}/tags" --jq '.[0].name' 2>/dev/null || echo '')"
26-
if [ -z "$TAG" ]; then
27-
echo "No tags found, proceeding with release."
28-
echo "has_commits=true" >> $GITHUB_OUTPUT
29-
else
30-
AHEAD="$(gh api "repos/${REPO}/compare/${TAG}...main" --jq '.ahead_by')"
31-
if [ "$AHEAD" -eq 0 ]; then
32-
echo "No new commits since $TAG, skipping release."
33-
echo "has_commits=false" >> $GITHUB_OUTPUT
34-
else
35-
echo "$AHEAD new commits since $TAG."
36-
echo "has_commits=true" >> $GITHUB_OUTPUT
37-
fi
38-
fi
23+
run: .github/scripts/check-new-commits.sh
3924

4025
release-pr:
4126
needs: check
@@ -47,72 +32,19 @@ jobs:
4732
with:
4833
fetch-depth: 0
4934

50-
- name: Increment version number
51-
run: perl -i -pe 's/\b(\d+)(?=\D*$)/$1+1/e' VERSION
52-
53-
- name: Get changelog
54-
id: changelog
55-
run: |
56-
TAG="$(git describe --tags --abbrev=0 2>/dev/null || git rev-list --max-parents=0 HEAD)"
57-
GIT_LOG="$(git log $TAG..HEAD --oneline --pretty=format:"- %s [%an]")"
58-
CHANGELOG=$(cat << EOF
59-
Changelog:
60-
$GIT_LOG
61-
EOF
62-
)
63-
CHANGELOG="${CHANGELOG//'#'/''}"
64-
echo "content<<EOF" >> "$GITHUB_OUTPUT"
65-
echo "$CHANGELOG" >> "$GITHUB_OUTPUT"
66-
echo "EOF" >> "$GITHUB_OUTPUT"
67-
68-
- name: Display changelog
69-
run: |
70-
cat <<'EOF'
71-
${{ steps.changelog.outputs.content }}
72-
EOF
73-
74-
- name: Get version
75-
run: |
76-
VERSION="$(cat VERSION)"
77-
echo "VERSION=$VERSION" >> $GITHUB_ENV
78-
79-
- name: Get commit message
80-
id: message
81-
run: |
82-
COMMIT_MSG=$(cat << 'EOF'
83-
Release v${{ env.VERSION }}
84-
85-
${{ steps.changelog.outputs.content }}
86-
EOF
87-
)
88-
echo "content<<EOF" >> "$GITHUB_OUTPUT"
89-
echo "$COMMIT_MSG" >> "$GITHUB_OUTPUT"
90-
echo "EOF" >> "$GITHUB_OUTPUT"
91-
92-
- name: Get pull request body message
93-
id: body
94-
run: |
95-
MSG=$(cat << 'EOF'
96-
Auto-generated pull request for version ${{ env.VERSION }}.
97-
98-
Please use **Squash and merge** to include the changelog in the release message.
99-
100-
${{ steps.changelog.outputs.content }}
101-
EOF
102-
)
103-
echo "content<<EOF" >> "$GITHUB_OUTPUT"
104-
echo "$MSG" >> "$GITHUB_OUTPUT"
105-
echo "EOF" >> "$GITHUB_OUTPUT"
35+
- name: Prepare release
36+
id: release
37+
run: .github/scripts/prepare-release.sh
10638

10739
- name: Create Pull Request
10840
uses: peter-evans/create-pull-request@v8
10941
with:
11042
base: main
11143
add-paths: VERSION
11244
reviewers: antoninbas, jafingerhut, smolkaj
113-
commit-message: ${{ steps.message.outputs.content }}
45+
commit-message: ${{ steps.release.outputs.commit_msg }}
11446
signoff: false
11547
branch: v${{ env.VERSION }}
11648
delete-branch: true
11749
title: Automated Release v${{ env.VERSION }}
118-
body: ${{ steps.body.outputs.content }}
50+
body: ${{ steps.release.outputs.pr_body }}

.github/workflows/publish-release.yml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,9 @@ jobs:
2525
with:
2626
fetch-depth: 0
2727

28-
- name: Get version
29-
run: |
30-
VERSION="$(cat VERSION)"
31-
TAG="v$(cat VERSION)"
32-
echo "VERSION=$VERSION" >> $GITHUB_ENV
33-
echo "TAG=$TAG" >> $GITHUB_ENV
34-
35-
- name: Check if tag exists
28+
- name: Check tag
3629
id: check_tag
37-
run: |
38-
TAG="${{ env.TAG }}"
39-
if git rev-parse "$TAG" >/dev/null 2>&1; then
40-
echo "Tag $TAG already exists, skipping release creation."
41-
echo "tag_exists=true" >> $GITHUB_OUTPUT
42-
else
43-
echo "Tag $TAG does not exist."
44-
echo "tag_exists=false" >> $GITHUB_OUTPUT
45-
fi
30+
run: .github/scripts/check-tag.sh
4631

4732
- name: Release
4833
if: steps.check_tag.outputs.tag_exists == 'false'

0 commit comments

Comments
 (0)