Streamline Github Actions Releases with Github CLI

Streamline Github Actions Releases with Github CLI

ยท

3 min read

GitHub Actions is the default choice for CI/CD in many Open Source and Enterprise projects. It gets regular feature updates and is flexible enough to get most of the things done.

One of the powerful feature it provides is the ability to re-use Actions, which is available on Marketplace and we can use it from any GH repos. Most common Actions that I see being used almost everywhere are related to:

  • creating a Release

  • generating Release Notes for the release

  • and uploading any Artifacts for the release

Well, I was doing the same until recently. Playing around with the GitHub CLI I figured everything I mentioned above could be done with it effortlessly! โœจ Not to mention it is flexible and provides options to move things our way. ++ It's already installed on the GitHub Actions runner.

Enough talk, now let's see it in Action ๐Ÿฅ

Creating a Release

GitHub CLI provides a built-in sub-command to create a release. ๐ŸŒŸ

For a basic example: we want to create a release whenever we push a tag ๐Ÿท๏ธ

steps:
  - uses: actions/checkout@v3
  - name: Create Release
    run: gh release create ${GITHUB_REF#refs/*/} -t ${GITHUB_REF#refs/*/}
  • gh release create <tag> : create a new release with the latest tag pushed

  • -t <tag>: Set title of the release; same as the pushed tag

Apart from general releases, we can also create draft releases and prerelease with the following flags.

  • -d : draft release

  • -p: prerelease

Generate Release Notes

Now this one is pretty easy. We just have to pass the --generate_notes flag to the above command and it will be generated for us.

steps:
  - uses: actions/checkout@v3
  - name: Create Release
    run: gh release create ${GITHUB_REF#refs/*/} -t ${GITHUB_REF#refs/*/} --generate-notes

If we want more flexibility then it also provides options to generate release notes from a file or from STDIN with the -F flag.

Upload artifacts with the Release

Now this gets more interesting. You won't believe me if I say it.

To upload artifacts to our release all we have to do is pass the file or directory names to the same command!

I am taking an example of my repository where I build browser extension artifacts for Chrome and Firefox:

permissions: write-all
name: Create GH Release
steps:
  - uses: actions/checkout@v3
  - name: Build Artifacts
    run: ./release.sh

  - name: Create Release
    run: gh release create ${GITHUB_REF#refs/*/} -t ${GITHUB_REF#refs/*/} 12_ft_chrome.zip 12_ft_firefox.zip --generate-notes
  • permissions: write-all: For uploading artifacts, we will need to assign additional permissions to our GITHUB_TOKEN.

  • ./release.sh: Builds Release Artifacts

  • 12_ft_<browser>.zip: are the artifacts generated by the above script

If we want a separate step to upload our artifacts, we can do so with the following change:

permissions: write-all
name: "Create GH Release"
steps:
  - uses: actions/checkout@v3
  - name: "Build Artifacts"
    run: ./release.sh

  - name: Create Release
    run: gh release create ${GITHUB_REF#refs/*/} -t ${GITHUB_REF#refs/*/} --generate-notes

  - name: Upload Artifact to Release
    run: gh release upload  ${GITHUB_REF#refs/*/} 12_ft_chrome.zip 12_ft_firefox.zip

Conclusion

We don't have to dabble around with multiple third-party actions, all of it can be done from a single command using GitHub CLI. This is more manageable, and effortless.

Hope this has been helpful! โœจ

Full reference to the above Action YAML ๐Ÿš€