Improve code, fix build #106
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: Auto Release | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
release: | |
name: Release | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: 📁 Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
fetch-tags: true | |
- name: 📦 Get versions | |
env: | |
GH_TOKEN: ${{ github.token }} | |
id: version-check | |
run: | | |
# Get the latest tag | |
latest_tag=$(git describe --tags --abbrev=0) | |
# Export the current and target versions | |
echo current_version=$(echo $latest_tag | sed "s/v//") >> $GITHUB_OUTPUT | |
echo version=$(node -pe "require('./package.json').version") >> $GITHUB_OUTPUT | |
# Get all changes & authors of commits since the last release, excluding merge commits and commits from dependabot or renovate | |
IFS=$'\n' changes=($(git log $latest_tag..HEAD --pretty="%s" --no-merges --perl-regexp --author='^((?!dependabot\[bot\]|renovate\[bot\]).*)$' --invert-grep --grep='^infra: ')) | |
IFS=$'\n' authors=($(git log $latest_tag..HEAD --pretty="%ae" --no-merges --perl-regexp --author='^((?!dependabot\[bot\]|renovate\[bot\]).*)$' --invert-grep --grep='^infra: ')) | |
# If there are no changes, set changelog as "_No changes_" and exit | |
if [ ${#changes[@]} -eq 0 ]; then | |
echo "Changelog: No changes" | |
echo changelog="_No changes_" >> $GITHUB_OUTPUT | |
exit 0 | |
fi | |
# Map author emails to GitHub usernames in a map | |
declare -A author_map | |
while read -r author; do | |
if [[ $author == *"@users.noreply.github.com" ]]; then | |
# if author ends with @users.noreply.github.com, parse the username between '+' and '@' | |
username=$(echo "$author" | cut -d"@" -f1 | cut -d"+" -f2) | |
echo "Parsed username '$username' from '$author'" | |
author_map[$author]=$username | |
else | |
# otherwise, search for the author's GitHub username using the GitHub API | |
username=$(gh api search/users?q="$author"+in:email | jq -r ".items[0].login") | |
echo "Searched for '$author' and found '$username'" | |
author_map[$author]=$username | |
fi | |
done <<< "$(echo "${authors[*]}" | uniq)" | |
# Assemble changelog, by formatting it as "- <commit message> - @<author>", where 'author' is picked from the index of the current change, and is passed through the 'author_map' to get the GitHub username | |
changelog=() | |
for i in "${!changes[@]}"; do | |
author=${authors[$i]} | |
handle=${author_map[$author]} | |
changelog+=("- ${changes[$i]} - @$handle") | |
done | |
# Print the changelog to the console | |
echo "Changelog: ${changelog[*]}" | |
# Export the changelog as a string, escaping double quotes and < characters when they are followed by a letter | |
escaped_changelog=$(printf '%s\n' "${changelog[*]//\"/\\\"}" | sed -e 's/\(<[a-z]\)/\\\1/g') | |
echo "changelog<<EOF"$'\n'"${escaped_changelog}"$'\n'"EOF" >> $GITHUB_OUTPUT | |
- name: ✨ Create release | |
if: steps.version-check.outputs.version != steps.version-check.outputs.current_version | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
gh release create v${{ steps.version-check.outputs.version }} \ | |
--title ${{ steps.version-check.outputs.version }} \ | |
--notes "## What's Changed | |
${{ steps.version-check.outputs.changelog }} | |
**Full Changelog:** https://github.com/${{ github.repository }}/compare/v${{ steps.version-check.outputs.current_version }}...v${{ steps.version-check.outputs.version }}" |