GitHub Actions is a powerful tool for automating your software development workflows. Below is a step-by-step guide to setting up a GitHub Actions workflow for a Go project.
Create a new file in your repository at .github/workflows/go.yml with the following content:
yamlname: Go CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
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.20
- name: Install dependencies
run: go mod tidy
- name: Run tests
run: go test -v ./...
name: Go CI: This is the name of your workflow.
on:: Specifies the events that trigger the workflow. This example runs the workflow on push and pull_request events targeting the main branch.
jobs:: Defines a series of tasks to be run.
build:: The name of the job. You can name this anything.
runs-on: ubuntu-latest: Specifies the type of virtual machine to run the job on.
steps:: Lists the individual steps to be executed as part of the job.
name: Checkout code: Uses the actions/checkout action to check out your repository code.
name: Set up Go: Uses the actions/setup-go action to set up the Go environment. The go-version parameter specifies which version of Go to use.
name: Install dependencies: Runs go mod tidy to ensure your dependencies are correctly installed.
name: Run tests: Runs go test -v ./... to execute your tests.
For more complex projects, you might need additional steps like linting, building the project, or deploying artifacts. Here's an extended example:
yamlname: Go CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
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.20
- name: Install dependencies
run: go mod tidy
- name: Install linters
run: go install golang.org/x/lint/golint@latest
- name: Run linters
run: golint ./...
- name: Run tests
run: go test -v ./...
- name: Build application
run: go build -v ./...
- name: Upload test results
uses: actions/upload-artifact@v3
with:
name: test-results
path: path/to/test-results
name: Install linters: Installs the golint linter.
name: Run linters: Runs golint to lint the Go code.
name: Build application: Builds the Go application.
name: Upload test results: Uses the actions/upload-artifact action to upload test results or other artifacts for later use.
If you need to use environment variables or secrets, you can add them to your workflow. For example:
yamljobs:
build:
runs-on: ubuntu-latest
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.20
- name: Install dependencies
run: go mod tidy
- name: Run tests with environment variable
run: go test -v ./...
Here, DATABASE_URL is an environment variable sourced from the repository secrets.
With this setup, you can automate the build, test, and deployment processes of your Go projects using GitHub Actions. The basic configuration provides a solid starting point, while the advanced configuration allows for more complex workflows and integration of additional tools.