Automatically Generate Tags and Release Notes on Main Branch Merges Using GitHub Actions
Introduction
In a previous article, I introduced a way to categorize release notes by the corresponding content.
👉 Categorize GitHub Repository Release Notes by Content for a Nice Look
In this article, I will show how to automatically generate tags and release notes using GitHub Actions when certain events are triggered.
The Workflow
Here is the entire GitHub Actions workflow I put together:
name: Generate release note
on:
pull_request:
branches:
- main
types:
- closed
jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Generate tag
run: |
export TAG_NAME=$(sed -n 6P pubspec.yaml | sed 's/version: //')
git tag $TAG_NAME
git push origin $TAG_NAME
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
- name: Generate release note
uses: softprops/action-gh-release@v1
with:
name: ${{ env.TAG_NAME }}
tag_name: ${{ env.TAG_NAME }}
generate_release_notes: true
Explanation of Each Step
Trigger
For this workflow, I set it to run when a pull request targeting the main
branch is closed (merged).
There may also be cases where you want to create release notes when a branch such as release/*
is merged or when a tag is pushed. Please adjust as necessary.
on:
pull_request:
branches:
- main
types:
- closed
actions/checkout@v3
This action checks out the repository. I won’t go into detail since it’s commonly used.
Generate tag
Here, a tag is generated and pushed. In my case, I’m retrieving the version number, which will be the tag name, from the pubspec.yaml
file of my Flutter app.
You can also get the tag name from branch names like release/*
or tag/*
. You can obtain the branch name from the github.ref
context, so please adjust as needed.
export TAG_NAME=$(sed -n 6P pubspec.yaml | sed 's/version: //')
git tag $TAG_NAME
git push origin $TAG_NAME
Additionally, I want to use the tag name I created in the next step to create a release note, so I’m writing it to $GITHUB_ENV
.
By writing environment variables to the environment file $GITHUB_ENV
, subsequent steps can use the same environment variables. For more information, please read the official documentation.
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
Generate release note
For automatically generating release notes, I used the softprops/action-gh-release
action.
I specify the release note name with name
and the tag name with tag_name
.
By setting generate_release_note
to true
, release notes will be automatically generated based on .github/release.yml
. For more information on automatically generated release notes, please read:
- name: Generate release note
uses: softprops/action-gh-release@v1
with:
name: ${{ env.TAG_NAME }}
tag_name: ${{ env.TAG_NAME }}
generate_release_notes: true
There are other settings available for softprops/action-gh-release
, so I recommend reading the README for more details.
Finished!
With the above workflow set up, when a pull request targeting the main
branch is merged, tags and release notes are indeed automatically generated by GitHub Actions!
Conclusion
By using GitHub Actions, tedious tasks like creating tags and release notes can be easily automated.
Give it a try!