DEV Community

Cover image for Streamline Github Actions Releases with Github CLI
Yankee Maharjan
Yankee Maharjan

Posted on

Streamline Github Actions Releases with Github CLI

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/*/}
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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 🚀

Top comments (0)