Streamline Github Actions Releases with Github CLI
Tags: githubactions, devops, cloud, programming
Published: 2023-02-14 04:08:37

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](https://github.com/marketplace?type=actions) 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](https://github.com/cli/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 🏷️

```yaml
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.

```yaml
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](https://github.com/yankeexe/12ft-browser-extension/blob/main/.github/workflows/release.yaml) where I build browser extension artifacts for Chrome and Firefox:

```yaml
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:

```yaml
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**](https://github.com/yankeexe/12ft-browser-extension/blob/main/.github/workflows/release.yaml) **🚀**