Automated GitHub releases for Rust projects using Actions

2019/12/01

Tweet

I’ve been using GitHub Actions more and more across the projects I maintain. It’s a great offering and integrates well into GitHub.

There are a few Rust projects I have where I wanted to automate the publishing of prebuilt binaries to the GitHub releases page. I’ve seen others do it by using Travis but the setup seemed overly complex.

I found the upload-release-action repo, which actually lists Rust as an example but the workflow given doesn’t seem to work and the problem is discussed on a GitHub community forum thread. I managed to implement the fix given at the bottom of the page and the whole thing has been working great for me so far.

In order to trigger a release, I just create a tag:

$ git tag v0.1.1

and then push the tag to GitHub:

$ git push --tags

The build begins and the assets are uploaded to the repository. The workflow I used to achieve this is below. Be sure to change the <name> sections to something suitable. I placed the file in .github/workflows/publish.yml.

name: Publish

on:
  push:
    tags:
      - '*'

jobs:
  publish:
    name: Publish for ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        name: [
            linux,
            windows,
            macos
        ]

        include:
          - name: linux
            os: ubuntu-latest
            artifact_name: <name>
            asset_name: <name>-linux
          - name: windows
            os: windows-latest
            artifact_name: <name>.exe
            asset_name: <name>-windows
          - name: macos
            os: macos-latest
            artifact_name: <name>
            asset_name: <name>-macos

    steps:
    - uses: actions/[email protected]

    - uses: actions-rs/[email protected]
      with:
        profile: minimal
        toolchain: stable

    - name: Build
      run: cargo build --release --locked

    - name: Upload binaries to release
      uses: svenstaro/[email protected]
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        file: target/release/${{ matrix.artifact_name }}
        asset_name: ${{ matrix.asset_name }}
        tag: ${{ github.ref }}

Tweet