[GitHub Actions] Use GitHub Release note API to make Release note automation

[GitHub Actions] Use GitHub Release note API to make Release note automation

2023-09-13 hit count image

Let's see how to automatically create a simple Release note using GitHub Release note API in GitHub Actions.

Outline

When you maintain an open source or manage a version in GitHub, you might need to write Release note.

GitHub Actions release note API: Release note

Howeve, you can’t write Release note by creating Pull request or checking Git history every time.

In the previous blog post, I introduced how to automatically create Release note by the Pull request title using Release Drafter.

In this blog post, I will introduce how to automatically create a simple Release note using Release note API in GitHub Actions.

GitHub personal access token

The way I will introduce in this blog post requires Personal access token of GitHub. In order to generate Personal access token, go to GitHub settings and select Developer settings.

GitHub Actions release note API: Developer settings

And then, select Personal access token (classic) to generate Personal access token.

GitHub Actions release note API: Personal access token (classic)

And then, select repo permission to generate Personal access token.

GitHub Actions release note API: Repo permission

After generating Personal access token, store it to Settings > Secrets and variables > Repository secret of the repository that you want to automate Release note as API_KEY.

GitHub Actions release note API: api key variable

GitHub Actions

Next, let’s automate Release note using GitHub API. Create .github/workflows/release_note.yml file and modify it as follows.


name: Create release tag and release note

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

jobs:
  create-release-note:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Get previous tag
        id: pre_tag
        run: |
          echo "version=$(curl -L -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.API_KEY }}" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/${{ github.repository }}/releases/latest \
            | jq .tag_name \
            | sed 's/"//g')" >> $GITHUB_OUTPUT

      - name: Generate release tag
        id: release_tag
        run: echo "version=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT

      - name: Generate release note
        id: release_note
        run: |
          echo "note=$(curl -L \
            -X POST \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.API_KEY }}" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            https://api.github.com/repos/${{ github.repository }}/releases/generate-notes \
            -d '{"tag_name":"${{ steps.release_tag.outputs.version }}","target_commitish":"main","previous_tag_name":"${{ steps.pre_tag.outputs.version }}"}' \
            | jq .body \
            | sed 's/"//g')" >> $GITHUB_OUTPUT

      - name: Create Release
        run: |
          curl -X POST \
            -H "Authorization: token ${{ secrets.API_KEY }}" \
            -d "{ \"tag_name\": \"${{ steps.release_tag.outputs.version }}\", \"name\": \"${{ steps.release_tag.outputs.version }}\", \"body\": \"${{ steps.release_note.outputs.note }}\"}" \
            https://api.github.com/repos/${{ github.repository }}/releases

Let’s take a closer look at the code.


...
on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
...

This GitHub Actions is executed when the tag is added to GitHub in the format of Semantic Versioning.


...
- name: Get previous tag
  id: pre_tag
  run: |
    echo "version=$(curl -L -H "Accept: application/vnd.github+json" \
      -H "Authorization: Bearer ${{ secrets.API_KEY }}" \
      -H "X-GitHub-Api-Version: 2022-11-28" \
      https://api.github.com/repos/${{ github.repository }}/releases/latest \
      | jq .tag_name \
      | sed 's/"//g')" >> $GITHUB_OUTPUT
...

Next, use the /releases/latest API to get the previous tag in order to automatically generate the Release note.

This API returns the tag used in the previous Release note, so in order to use this GitHub Actions, there must be at least one Release note. If Release note is not written and there are multiple tag, it is recommended to write the Release note using the latest tag.


...
 - name: Generate release tag
  id: release_tag
  run: echo "version=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
...

And then, get the current tag and assign it to a variable for generating Release note.


...
 - name: Generate release note
  id: release_note
  run: |
    echo "note=$(curl -L \
      -X POST \
      -H "Accept: application/vnd.github+json" \
      -H "Authorization: Bearer ${{ secrets.API_KEY }}" \
      -H "X-GitHub-Api-Version: 2022-11-28" \
      https://api.github.com/repos/${{ github.repository }}/releases/generate-notes \
      -d '{"tag_name":"${{ steps.release_tag.outputs.version }}","target_commitish":"main","previous_tag_name":"${{ steps.pre_tag.outputs.version }}"}' \
      | jq .body \
      | sed 's/"//g')" >> $GITHUB_OUTPUT
...

Next, use the tag prepared above and /releases/generate-notes API to generate the Release note content.

When using this API, a simple Release note is generated as follows.

**Full Changelog**: https://github.com/dev-yakuza/release-note-test/compare/v0.0.1...v0.0.2

If you want to change the content of the Release note created by this API, create the .github/release.yml file and modify it as follows.

changelog:
  exclude:
    labels:
      - ignore-for-release
    authors:
      - octocat
  categories:
    - title: Breaking Changes 🛠
      labels:
        - Semver-Major
        - breaking-change
    - title: Exciting New Features 🎉
      labels:
        - Semver-Minor
        - enhancement
    - title: Other Changes
      labels:
        - "*"

If you want to know more about the .github/release.yml file, please refer to the official document below.

I don’t create .github/release.yml for this blog post in order to create a simple Release note.

Lastly, use the /releases API to create a Release ntoe.


...
- name: Create Release
  run: |
    curl -X POST \
      -H "Authorization: token ${{ secrets.API_KEY }}" \
      -d "{ \"tag_name\": \"${{ steps.release_tag.outputs.version }}\", \"name\": \"${{ steps.release_tag.outputs.version }}\", \"body\": \"${{ steps.release_note.outputs.note }}\"}" \
      https://api.github.com/repos/${{ github.repository }}/releases

Done! we’ve ready to create a simple Release note using GitHub API in GitHub Actions.

Execute

In order to execute this GitHub Actions, add tag to Git as follows.

git tag v0.0.2
git push origin v0.0.2

When you add tag to GitHub like this, the GitHub Actions created above will be executed and you can see that the Release note is automatically generated as follows.

GitHub Actions release note API: release note is generated

When you click it, you can see the details as follows.

GitHub Actions release note API: release note detail

Completed

Done! we’ve seen how to automatically create a simple Release note using GitHub API in GitHub Actions. If you are thinking of creating a simple Release note, please refer to this blog post.

If you want to manage Release note more systematically, please refer to the blog post below.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts