Getting Started with GoLang GitHub Action Runners
Creating GitHub actions with GoLang can be efficient and fairly straightforward. This post will guide you through creating a simple "Hello, World!" action using GoLang and Docker. Firstly, let's start with the GoLang program that prints "Hello, World!" on the console.
main.go
file:
This is a very basic GoLang program, stored in the file main.go
:
go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Each time this program is executed, it simply prints "Hello, World!" to the console. For the GitHub Action, we define a Dockerfile
and action.yml
.
Dockerfile:
Our Dockerfile
creates a Docker image using the golang:1.21.5-alpine3.19
image as the base copies our application into the image downloads any dependencies, and compiles our GoLang program:
Dockerfile
FROM golang:1.21.5-alpine3.19 AS builder
WORKDIR /app
COPY . /app
RUN go get -d -v
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o hello-world .
# final stage
FROM alpine:3.19
RUN apk --no-cache add ca-certificates
WORKDIR /app
COPY --from=builder /app/hello-world /app/
CMD sh -c 'pwd; ls -ltrh; ls -ltrh /app; /app/hello-world'
action.yml
File:
In action.yml
, we declare the metadata, specify that the action will run inside a Docker container, and specify the Dockerfile as the image to be used:
yaml
name: "Hello World Action"
description: "Runs a hello world golang program"
runs:
using: "docker"
image: "Dockerfile"
Note that the image
field points directly to Dockerfile
in the same repository. Finally, we must stitch this together in a GitHub Actions workflow.
main.yaml
Workflow File:
Our workflow file main.yaml
in .github/workflows
directory specifies an action to run for every push
event. This action checks out our code repository using actions/checkout@v2
and executes the hello-world action:
yaml
name: Example Workflow
on: [push]
jobs:
runHelloWorld:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: Run the hello world action
uses: <your_github_username>/<github_repo_name>@v1
The second step is where you'll use custom action. You need to replace <your_github_username>/<github_repo_name>@v1
it with an appropriately formatted reference to action.
To use your GitHub username and repo name, replace <your_github_username>
it with your GitHub username, and replace <github_repo_name>
it with the name of the repository containing your GitHub action. The @v1
at the end specifies the version of the action to use, in this case, version 1.
To push changes from local to the GitHub repository with the correct tag:
First, commit your changes locally using git. For example, you can use
git add .
it to stage all changes and thengit commit -m "Your commit message"
to commit them.You then need to tag this commit with the correct version. This could be done using
git tag v1
to create a new tag named v1.Finally, you need to push your changes, including the tags, to your repository. Use
git push origin master --tags
for this (assuming you're on the 'master' branch). The--tags
flag ensures that your tags are also pushed to the repository.
We're specifying the runHelloWorld
job to run on GitHub's servers (runs-on: ubuntu-latest
) and using Docker to run our application within that environment.
And voila! You've set up your first GoLang runner for GitHub Actions. Remember to push your main.go
, Dockerfile
, and action.yml
to your GitHub repository and specify the correct path in main.yaml
. Happy coding!