A github actions to help build your golang program with CGO_ENABLED = 1
name: Build Go Program
on:
pull_request:
branches:
- main
push:
branches:
- main
permissions:
contents: read
jobs:
cgo-action:
strategy:
matrix:
targets:
- '!(*musl*|*windows-arm64*|*android*|*freebsd*|*windows7*)' # xgo
- 'linux-*-musl*' #musl
- 'windows-arm64' #win-arm64
- 'android-*' #android
- 'freebsd-*' #freebsd
- 'windows7-*' #windows7
name: Build Go Program
runs-on: ubuntu-latest
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Build go program
id: cgo-action
uses: go-cross/cgo-actions@v1
with:
dir: '.'
targets: ${{ matrix.targets }}
- name: Print Output
id: output
run: echo "${{ steps.cgo-action.outputs.files }}"
NOTE
Of course, you can use the * wildcard to match multiple targets, but it will
take longer to build and may fail due to the space limit of the github
actions.
| Input | Description | Required | Default |
|---|---|---|---|
| dir | The directory to work | No | . |
| packages | The packages to build | No | . |
| flags | The flags to pass to the go build command | No | -ldflags=-w -s |
| static-link-for-musl | Whether to statically link for the musl libc | No | true |
| targets | The targets to build for | No | * |
| out-dir | The output directory | No | bin |
| output | The output binary name | No | target$ext |
| musl-target-format | The format of the musl target | No | arch-$musl |
| x-flags | Extra X ldflags to pass to the go build command | No | |
| musl-base-url | Where to download musl compilers | No | https://github.com/OpenListTeam/musl-compilers/releases/latest/download |
Use glob patterns to match the targets you want to build for.
| Output | Description |
|---|---|
| files | The files that were built |