Using git tags is quite easy and can be useful in setting up a simple CI/CD like
pipeline i.e. streamlining the process of packaging or deploying your code.
First thing you need to do is to create a tag in your git repo, you do this by
running git tag v0.1.2
where v0.1.2
is your tag name, usually a version
number or some other more human friendly identifier than a commit hash. It
doesn’t have to start with v
, but it’s a common to do so. Any tag creation
that you do takes place on your local machine until you push it to the remote
repo with git push --tags
.
This is a good reference point for your codebase that you can use yourself or
refer others to, but let’s take it a step further and make use of GitHub
workflows to automate some tasks when a tag is pushed. Start by creating a file
in a directory .github/workflows
in your repo, create them if they don’t
exist. For example it can be .github/workflows/release.yml
the filename
doesn’t matter as long as you understand what it does.
Here’s a flow that creates a binary from a Go project and releases it as an
asset which means it can be accessed in the “Releases” section of your
repository. First block defines a trigger for the workflow, in this case it runs
when a tag is pushed that starts with v
. The jobs
block defines the steps
needed to achieve our goal of releasing a binary for my-project
. The first two
steps are pretty self-explanatory and make use of the official GitHub actions.
Next we define how we want to build the binary. And the final step is to use a
3rd party action to release the binary where the file name is the most important
parameter, but it can has other params if needed.
name: Build and Release Go Binaries
on:
push:
tags:
- 'v*'
jobs:
build:
name: Build and Release Go Binaries
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.23
- name: Build Go Binary
run: |
mkdir -p dist
GOOS=linux GOARCH=amd64 go build -o my-project-linux-amd64
- name: Release
uses: softprops/action-gh-release@v2
with:
files: |
my-project-linux-amd64
Now git add, commit and push this file to your repo and you’re all set. Next time you tag a commit and push it the workflow will run and create a binary release asset.