Building a GoLang application can be done in many ways. From build scripts (e.g. shell, make), using the go
CLI, or even GitHub Actions
. The latter is what we're going to use. There will be screen shots in the repo as we need a PAT
, personal access token
to commit the built files.
Before we begin, I will give one piece of advice... If you want to make sure your repo stays secure, add security rules. This is the reason we need a PAT
. Thankfully, each repo also has a Secrets
section that can store it.
Generating a PAT
GitHub docs are relatively useless past this point, so we're going to go through the whole process, together.
From the Github landing page, login to your account.
Once logged in, click on your profile image
, and select settings
.
From there, all the way at the bottom of the menu panel, select Developer Settings
.
Then select, Personal Access Tokens
-> Fine-grained Tokens
-> and finally, Generate New Token
.
Fill in the typical information: Name
, Expiration
, and Description
.
Now, you NEED to be very careful here. Since this is going to be used in a workflow, we have to make sure we select nothing extra outside of what is absolutely needed.
The three (3) steps in this case (we'll cover them in more detail later) are pull
the repo, build
the repo, and commit
the build.
In order for the workflow to pull the repo, it needs to know which repo it has access too. If you want to really break it down, here is the detailed list of all the permissions available for a Fine-grained PAT
.
The first one you'll notice under Permissions
is metadata
. This is mandatory
as it allows it to check for changes. The first one WE are going to add, is Contents
. This will allow the workflow to pull
the repo into the workflow and commit
build files. the second will be Secrets
set to Read-only
, as this is where we are going to store out PAT
. Finally, we'll also add Actions
so we can read
and write
to the repo.
Generate your PAT
and DO NOT CLOSE THIS WINDOW!!! Once you do, you will have to tell GitHub to regenerate another key to do the next step. Click on the clipboard icon to copy the key.
Now, go to your repo and click Settings
. Scroll down to Security
-> Secrets and Variables
-> Actions
. Select New Repository Secret
. Give it a name and paste the generated key. If you didn't copy it, do so now and fill in the key.
Now for GitFlow
From the repo, click Actions
, and in the top right corner of the menu panel, select New workflow
.
Next select set up a workflow yourself
. You can choose to use a premade go
workflow, however you will end up with duplicate processes.
To initialize the workflow, there are a few key things to keep in mind:
- When the workflow should run (i.e. push, PR)
- Platform it should run on (Linux, Windows, Mac)
- Permissions (this is covered by PAT)
name: Go # workflow name
on: # tells the runner when
push:
branches: [ "main" ] # which branches
pull_request:
branches: [ "main" ]
jobs: # tells the runner what
build: # process name
runs-on: ubuntu-latest # base platform
That is the base of the runner. Next we need a few actions from the marketplace. Don't worry, they are all free and each one comes with it's own documentation. I'm going to give a brief overview of what they do, how I used it, and will leave the rest to you.
actions/checkout@v4.1.7
will make a copy of the repo for the runner to build. It is used as such:
steps:
- name: Checkout
uses: actions/checkout@v4.1.7
with:
token: ${{ secrets.[YOUR_SECRET_NAME] }}
capthiron/templ-generator-action
is the heavy lifter here. It will setup both go
and templ
for you. It also has a commit
option. I chose an additional at the end to allow for incremental commits and not massive commits where changes can be lost.
- name: templ-generator-action
uses: capthiron/templ-generator-action@v1
with:
# The directory where to look for .templ files.
directory: "./internal/views"
# Flag to enable or disable committing changes
commit: True
# Flag to enable or disable setting up Go.
# using default (true)
# The Go version to use.
go-version: '1.23.x'
# The templ version to use.
# using default (latest)
The next is a build to compile the code.
- name: Build
run:
go build ./cmd/server/main.go
Finally, the additional commit id by EndBug/add-and-commit@v9
. This has a lot of options in it. As this is for personal use, I only changed the author so it is known at which step the runner commits.
- name: Commit
uses: EndBug/add-and-commit@v9
with:
author_name: wokflow_gen
And with that, your workflow is done. commit
to the repo and check your Actions
tab to see it build.
Wrapping up
All of the files you would normally get by running templ generate
will now be done with a GitHub Runner and be commit
-ted directly to the repo. Running go build
will also be done and commit
s the executable. Here is the completed repo. As from this point on, the project can go wherever you want to take it.
Addendum
Pay attention to your .gitignore
... The runner will NOT commit anything in it... That's what took so long to write this article. I forgot I had a *_templ*
rule.
Up Next
Up next we have HTMX and possibly a CSS library. I may forego the latter one in favor of Templ css
components.