Search code examples
gogithubreleasegithub-actions

How to use Go Release Binary GitHub Action


Anyone able to get Go Release Binary GitHub Action working? which is supposed to

Automate publishing Go build artifacts for GitHub releases through GitHub Actions

The readme looks rather simple, but I've tried all my imaginations to get it working but not avail. Similar questions has been asked in its issue tracks but got no answer.

Somebody help please.

BTW, while searching for the answer, I came upon this commit logs, which is quite interesting/amusing to read. I.e., it seems to be quite a battle to get it working, but the author gave up eventually (no any releases from his/her latest commits/tags)

Conclusion:

Turns out that my project does not have go mod and there were issues in Go Release which stops it from working. It was then fixed by this and this.


Solution

  • I actually wrote my own release workflow for generating Go binaries.

    The only non-obvious steps from my point of view are:

    1. I have a release note generation step where I include a list of non-merge commits since the last release tag.
    2. I use a matrix build for GOOS and GOARCH pairs and do some Bash string manipulation in the "Get OS and arch info" step.

    The nice thing about softprops/action-gh-release is that you can keep adding artifacts to the same release as long as the workflow run is triggered by a push to the same tag.

    on:
      push:
        # Sequence of patterns matched against refs/tags
        tags:
          - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
    
    name: Latest Release
    
    defaults:
      run:
        shell: bash
    
    jobs:
      lint:
        name: Lint files
        runs-on: 'ubuntu-latest'
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-go@v2
            with:
              go-version: '1.16.3'
          - name: golangci-lint
            uses: golangci/[email protected]
            with:
              version: latest
      test:
        name: Run tests
        runs-on: 'ubuntu-latest'
        needs: lint
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-go@v2
            with:
              go-version: '1.16.3'
          - run: go test -v -cover
      release:
        name: Create Release
        runs-on: 'ubuntu-latest'
        needs: test
        strategy:
          matrix:
            # List of GOOS and GOARCH pairs from `go tool dist list`
            goosarch:
              - 'aix/ppc64'
              # etc
        steps:
          - name: Checkout code
            uses: actions/checkout@v2
            with:
              fetch-depth: 0
          - uses: actions/setup-go@v2
            with:
              go-version: '1.16.3'
          - name: Get OS and arch info
            run: |
              GOOSARCH=${{matrix.goosarch}}
              GOOS=${GOOSARCH%/*}
              GOARCH=${GOOSARCH#*/}
              BINARY_NAME=${{github.repository}}-$GOOS-$GOARCH
              echo "BINARY_NAME=$BINARY_NAME" >> $GITHUB_ENV
              echo "GOOS=$GOOS" >> $GITHUB_ENV
              echo "GOARCH=$GOARCH" >> $GITHUB_ENV
          - name: Build
            run: |
              go build -o "$BINARY_NAME" -v
          - name: Release Notes
            run:
              git log $(git describe HEAD~ --tags --abbrev=0)..HEAD --pretty='format:* %h %s%n  * %an <%ae>' --no-merges >> ".github/RELEASE-TEMPLATE.md"
          - name: Release with Notes
            uses: softprops/action-gh-release@v1
            with:
              body_path: ".github/RELEASE-TEMPLATE.md"
              draft: true
              files: ${{env.BINARY_NAME}}
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}