Create Release in Github action - How to do it?
Automating the release process in software development can help improve project performance and reliability. GitHub Actions is a powerful tool that allows automating tasks such as creating release tags, building source code, and deploying. This article will show you how to create automatic release tags using GitHub Actions for the HionCoding website, with clear steps and illustrations with example code.
1: Introducing GitHub Actions and Release Tags
GitHub Actions is a feature of GitHub that allows you to automate tasks using YAML
files to define workflows. Release tags are a way to mark stable versions of a project, for easy future reference, and are an important step in the software release process.
2: Config GitHub Actions
To create a GitHub Action to automate release tag generation, you need to create a YAML file in your project's .github/workflows
directory. This YAML file defines the steps required to create a release tag when a specific event occurs.
Below I have 3 ways to create a release:
name: Create Release Tag
on:
push:
branches:
- main
jobs:
create_tag:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Create tag name
id: create_tag_name
run: |
echo "tag_name=v1.3.3" >> $GITHUB_OUTPUT
echo "tag_name_gh=v1.3.4" >> $GITHUB_OUTPUT
echo "tag_name_api=v1.3.5" >> $GITHUB_OUTPUT
- name: Create New Tag
run: |
echo "${{ steps.create_tag_name.outputs.tag_name }}"
tag_name="${{ steps.create_tag_name.outputs.tag_name }}"
git tag $tag_name
git push origin $tag_name
- name: Create Release by actions/create-release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: "${{ steps.create_tag_name.outputs.tag_name }}"
release_name: Release "${{ steps.create_tag_name.outputs.tag_name }}"
body: |
Changes in this Release
- First Change
- Second Change
draft: false
prerelease: false
# By GH
- name: Install GitHub CLI
run: |
sudo apt update
sudo apt install -y gh
- name: Create GitHub Release by GH
env:
GH_TOKEN: ${{ github.token }}
run: |
tag_name="${{ steps.create_tag_name.outputs.tag_name_gh }}"
gh release create $tag_name -t "Release $tag_name" -n "Description for Release $tag_name"
- name: Create GitHub Release with API
run: |
tag_name="${{ steps.create_tag_name.outputs.tag_name_api }}"
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d '{
"tag_name": "'$tag_name'",
"target_commitish": "main",
"name": "Release '$tag_name'",
"body": "Description for release '$tag_name'",
"draft": false,
"prerelease": false
}' \
https://api.github.com/repos/${{ github.repository }}/releases
Note:
draft: false
: If you want to create a draft release, set it to true.prerelease: false
: Set to true if this is a prerelease version.
Result:
Conclusion:
By using GitHub Actions, you can automate the release tag generation process, helping to increase efficiency and reliability in software development. The example code in this article will get you started, but you can customize to your project's specific needs. Wishing you success in implementing an automated release process and enjoying its benefits for your project.
If you need further assistance or have other questions about GitHub Actions, join the GitHub community or refer to the official documentation to learn more about advanced features and how to apply them to your project.