Changelog #8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Changelog | |
on: | |
push: | |
paths: | |
- 'setup.py' # Only trigger when setup.py is changed | |
branches: | |
- main # Adjust to the branches you want to trigger releases | |
workflow_dispatch: | |
jobs: | |
release: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v3 | |
with: | |
python-version: '3.9' # Use the Python version you are targeting | |
- name: Extract versions from setup.py commits | |
id: extract_versions | |
run: | | |
echo "Extracting versions from setup.py commits..." | |
# Find all commits that modified setup.py | |
COMMITS=$(git log --pretty=format:"%H" -- setup.py) | |
echo "COMMITS=$COMMITS" >> $GITHUB_ENV | |
# Extract versions from each commit | |
VERSIONS=() | |
for COMMIT in $COMMITS; do | |
VERSION=$(git show $COMMIT:setup.py | python -c "import re, sys; version_line = next(line for line in sys.stdin if line.strip().startswith('version=')); print(re.search(r'version=[\"\\']([^\"\\']+)[\"\\']', version_line).group(1))") | |
VERSIONS+=("$VERSION:$COMMIT") | |
done | |
echo "VERSIONS=$(IFS=,; echo "${VERSIONS[*]}")" >> $GITHUB_ENV | |
- name: Generate changelog | |
id: changelog | |
run: | | |
CURRENT_DATE=$(date +'%Y-%m-%d') | |
VERSIONS=${{ env.VERSIONS }} | |
VERSIONS_ARRAY=(${VERSIONS//,/ }) | |
CHANGELOG="" | |
echo "Generating complete changelog" | |
for (( i=0; i<${#VERSIONS_ARRAY[@]}; i++ )); do | |
VERSION_COMMIT=${VERSIONS_ARRAY[$i]} | |
VERSION=${VERSION_COMMIT%%:*} | |
COMMIT=${VERSION_COMMIT##*:} | |
if [ $i -lt $((${#VERSIONS_ARRAY[@]} - 1)) ]; then | |
PREV_COMMIT=${VERSIONS_ARRAY[$((i + 1))]##*:} | |
MAIN_CHANGE=$(git log -1 --pretty=format:"**%s** ([%h](https://github.com/OWNER/REPO/commit/%H))" $COMMIT) | |
CHANGELOG+="## Version $VERSION - $CURRENT_DATE\n" | |
CHANGELOG+="$MAIN_CHANGE\n" | |
# Include other commits between this and the previous setup.py commit | |
CHANGES=$(git log $PREV_COMMIT..$COMMIT --pretty=format:"- %s ([%h](https://github.com/OWNER/REPO/commit/%H))") | |
CHANGELOG+="$CHANGES\n" | |
else | |
MAIN_CHANGE=$(git log -1 --pretty=format:"**%s** ([%h](https://github.com/OWNER/REPO/commit/%H))" $COMMIT) | |
CHANGELOG+="## Version $VERSION - $CURRENT_DATE\n" | |
CHANGELOG+="$MAIN_CHANGE\n" | |
# Include all commits down to the initial commit | |
CHANGES=$(git log $COMMIT --pretty=format:"- %s ([%h](https://github.com/OWNER/REPO/commit/%H))") | |
CHANGELOG+="$CHANGES\n" | |
fi | |
CHANGELOG+="\n" | |
done | |
echo -e "$CHANGELOG\n$(cat CHANGELOG.md)" > CHANGELOG.md | |
echo "" >> CHANGELOG.md | |
cat CHANGELOG.md | |
- name: Commit and push changelog | |
run: | | |
git config --global user.name 'github-actions' | |
git config --global user.email 'github-actions@github.com' | |
git add CHANGELOG.md | |
git commit -m "Update changelog" | |
git push | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |