Destin Lee

How To Train Your GitHub Action to Release Your Go Project

Are you tired of doing the same release process over and over again for your Go project? Let's automate it! 🚀 Here's a secret: GitHub Actions LOVES doing chores. Let's put it to work!


Step 1: Wake Up Your GitHub Actions

First, you need to write a YAML "spell" that tells GitHub Actions what to do. This spell should be written in a file under .github/workflows/. Let's call it release.yml.

name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: '^1.16'

    - name: Get dependencies
      run: make deps

    - name: Build
      run: make

    - name: Test
      run: make test

  release:
    needs: build
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        draft: false
        prerelease: false

This YAML file is like a recipe for your favorite dish, but instead of creating a delicious cake, it builds and tests your project and then creates a new release!

Step 2: The Mysterious Makefile

"But what is this make deps command?" I hear you ask. Well, it's a line in our magical scroll known as the Makefile. It looks something like this:

.PHONY: deps

deps:
	go mod download

build:
	go build -o myapp

test:
	go test ./...

Here, make deps, make, and make test are your faithful minions that fetch dependencies, build your project, and run tests, respectively.

Step 3: Unleashing the Power of Git Tags

The last piece of the puzzle is the humble Git tag. It may not look like much, but it holds great power. Here's how you create one:

# Check out the commit you want (optional)
# Replace 'commit_hash' with the actual commit hash
git checkout commit_hash

# Tag the commit
# Replace 'v1.0.0' with your desired tag
git tag -a v1.0.0 -m "First release, yay!"

# Push the tag to GitHub
git push origin v1.0.0

Now, every time you push a tag starting with 'v' to GitHub, GitHub Actions will jump into action (pun intended), run the tasks defined in release.yml, and then create a new release.

And voila! You've automated your release process! Now, you can kick back, relax, and let GitHub Actions do the heavy lifting. 🎉


Remember, GitHub Actions is always ready to help, waiting eagerly for your commands. But be careful not to work it too hard, or it might form a union! 😉