Skip to content

Commit 245dc49

Browse files
authored
ci: add automated changelog generation to release workflow (#56)
* Add changelog generation step that automatically detects changes between tags * Include changelog content in GitHub release notes * Handle edge cases for first release or when no previous tag exists Signed-off-by: Qiming Chu <cchuqiming@gmail.com>
1 parent 850cd7a commit 245dc49

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

.github/workflows/release.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,49 @@ jobs:
4242
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
4343
fi
4444
echo "RELEASE_DATE=$(date +'%Y-%m-%d')" >> $GITHUB_ENV
45+
- name: Generate Changelog
46+
id: changelog
47+
run: |
48+
CURRENT_VERSION_TAG="${{ env.VERSION }}"
49+
echo "Generating changelog for version $CURRENT_VERSION_TAG"
50+
51+
git fetch --tags --force
52+
53+
# Get the commit hash for the current version tag
54+
CURRENT_TAG_COMMIT=$(git rev-parse "$CURRENT_VERSION_TAG^{commit}")
55+
56+
PREVIOUS_TAG_NAME=$(git describe --tags --abbrev=0 "${CURRENT_TAG_COMMIT}^" 2>/dev/null || echo "")
57+
58+
CHANGELOG_RANGE=""
59+
if [ -n "$PREVIOUS_TAG_NAME" ] && [ "$PREVIOUS_TAG_NAME" != "$CURRENT_VERSION_TAG" ]; then
60+
echo "Previous tag found: $PREVIOUS_TAG_NAME. Current tag: $CURRENT_VERSION_TAG"
61+
CHANGELOG_RANGE="${PREVIOUS_TAG_NAME}..${CURRENT_VERSION_TAG}"
62+
else
63+
if [ "$PREVIOUS_TAG_NAME" == "$CURRENT_VERSION_TAG" ]; then
64+
echo "Warning: Previous tag detection resolved to the current tag ('$CURRENT_VERSION_TAG'). This can happen in specific tagging scenarios."
65+
fi
66+
echo "No distinct previous tag found, or it's the first release. Generating changelog for all commits leading to $CURRENT_VERSION_TAG."
67+
# This will list all commits that are ancestors of CURRENT_VERSION_TAG.
68+
# For the first tag, it lists all commits from the beginning.
69+
CHANGELOG_RANGE="$CURRENT_VERSION_TAG"
70+
fi
71+
72+
echo "Using commit range for git log: $CHANGELOG_RANGE"
73+
# Format: - Commit subject (short_hash)
74+
GENERATED_CHANGELOG=$(git log --pretty="format:- %s (%h)" $CHANGELOG_RANGE)
75+
76+
if [ -z "$GENERATED_CHANGELOG" ]; then
77+
echo "Changelog is empty. Setting a default message."
78+
GENERATED_CHANGELOG="- Initial release or no new commits identified for this version."
79+
fi
80+
81+
echo "--- Generated Changelog ---"
82+
echo "$GENERATED_CHANGELOG"
83+
echo "--------------------------"
84+
85+
echo "CHANGELOG_CONTENT<<__EOF__" >> $GITHUB_ENV
86+
echo "$GENERATED_CHANGELOG" >> $GITHUB_ENV
87+
echo "__EOF__" >> $GITHUB_ENV
4588
- name: Setup Nix
4689
uses: cachix/install-nix-action@v31
4790
with:
@@ -68,6 +111,9 @@ jobs:
68111
69112
Release date: ${{ env.RELEASE_DATE }}
70113
114+
## Changelog
115+
${{env.CHANGELOG_CONTENT}}
116+
71117
## 📦 Support Platforms
72118
- Linux (x86_64)
73119
- macOS (x86_64, arm64)

0 commit comments

Comments
 (0)