go-binaries: reusable Go cross-compile workflow
Some checks failed
selftest / build-1 (push) Successful in 23s
selftest / helm (push) Successful in 0s
selftest / docker (push) Has been cancelled
selftest / go (push) Has been cancelled
selftest / upload (push) Has been cancelled
selftest / build (windows/arm64) (push) Has been cancelled
selftest / sbom (push) Has been cancelled
docker-build.yml / go-binaries: reusable Go cross-compile workflow (push) Failing after 0s
go-binaries.yml / go-binaries: reusable Go cross-compile workflow (push) Failing after 0s
helm-build.yml / go-binaries: reusable Go cross-compile workflow (push) Failing after 0s
selftest / build (push) Successful in 29s
selftest / build (linux/amd64) (push) Has been cancelled
Some checks failed
selftest / build-1 (push) Successful in 23s
selftest / helm (push) Successful in 0s
selftest / docker (push) Has been cancelled
selftest / go (push) Has been cancelled
selftest / upload (push) Has been cancelled
selftest / build (windows/arm64) (push) Has been cancelled
selftest / sbom (push) Has been cancelled
docker-build.yml / go-binaries: reusable Go cross-compile workflow (push) Failing after 0s
go-binaries.yml / go-binaries: reusable Go cross-compile workflow (push) Failing after 0s
helm-build.yml / go-binaries: reusable Go cross-compile workflow (push) Failing after 0s
selftest / build (push) Successful in 29s
selftest / build (linux/amd64) (push) Has been cancelled
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
270370ac2a
commit
3ff9f37591
5 changed files with 217 additions and 0 deletions
170
.forgejo/workflows/go-binaries.yml
Normal file
170
.forgejo/workflows/go-binaries.yml
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
# Reusable workflow: cross-compile a Go program for a GOOS/GOARCH matrix, keep the binaries as artifacts,
|
||||
# optionally upload them to S3 (mc), write update.json and generate a CycloneDX SBOM.
|
||||
# Usage: see README.md
|
||||
name: go-binaries
|
||||
|
||||
on:
|
||||
workflow_call:
|
||||
inputs:
|
||||
binary-name:
|
||||
description: Output name, binaries are called <binary-name>-<goos>-<goarch>[.exe].
|
||||
type: string
|
||||
required: true
|
||||
targets:
|
||||
description: JSON array of goos/goarch pairs.
|
||||
type: string
|
||||
default: '["linux/amd64","linux/arm64","windows/amd64","windows/arm64","darwin/amd64","darwin/arm64"]'
|
||||
package:
|
||||
description: Package to build.
|
||||
type: string
|
||||
default: ./
|
||||
ldflags:
|
||||
description: Value of -ldflags.
|
||||
type: string
|
||||
default: -s -w
|
||||
cgo-enabled:
|
||||
description: Value of CGO_ENABLED, empty leaves it unset.
|
||||
type: string
|
||||
default: '0'
|
||||
windows-exe:
|
||||
description: Rename windows binaries to *.exe.
|
||||
type: boolean
|
||||
default: true
|
||||
build-number-var:
|
||||
description: Go variable (for example main.BuildNumber) set to run number + build-number-offset with -X. Empty disables it.
|
||||
type: string
|
||||
default: ''
|
||||
build-time-var:
|
||||
description: Go variable set to the build date with -X.
|
||||
type: string
|
||||
default: ''
|
||||
build-number-offset:
|
||||
description: Added to the run number, set it to the last pipeline number of the old CI.
|
||||
type: string
|
||||
default: '0'
|
||||
go-version:
|
||||
type: string
|
||||
default: '1.27'
|
||||
upload:
|
||||
description: Upload the binaries with mc on the default branch. Needs the secrets S3_HOST, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_BUCKET.
|
||||
type: boolean
|
||||
default: false
|
||||
update-json:
|
||||
description: Also write update.json (downloadUrl, buildNumber, time) and upload it (needs upload and build-number-offset). Needs the secret S3_BUCKET.
|
||||
type: boolean
|
||||
default: false
|
||||
update-url:
|
||||
description: Base URL of the bucket for downloadUrl in update.json.
|
||||
type: string
|
||||
default: https://s3.jonasled.de
|
||||
sbom-file:
|
||||
description: If set, generate a CycloneDX SBOM of the module into this file (artifact of the same name).
|
||||
type: string
|
||||
default: ''
|
||||
runs-on:
|
||||
type: string
|
||||
default: default
|
||||
secrets:
|
||||
S3_HOST:
|
||||
required: false
|
||||
S3_ACCESS_KEY_ID:
|
||||
required: false
|
||||
S3_SECRET_ACCESS_KEY:
|
||||
required: false
|
||||
S3_BUCKET:
|
||||
required: false
|
||||
|
||||
env:
|
||||
GOPROXY: https://goproxy.jonasled.dev,direct
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ${{ inputs.runs-on }}
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: ${{ fromJSON(inputs.targets) }}
|
||||
steps:
|
||||
- uses: https://code.forgejo.org/actions/checkout@v7
|
||||
- uses: https://code.forgejo.org/actions/setup-go@v7
|
||||
with:
|
||||
go-version: ${{ inputs.go-version }}
|
||||
- name: Build
|
||||
env:
|
||||
TARGET: ${{ matrix.target }}
|
||||
NAME: ${{ inputs.binary-name }}
|
||||
PKG: ${{ inputs.package }}
|
||||
LDFLAGS: ${{ inputs.ldflags }}
|
||||
CGO: ${{ inputs.cgo-enabled }}
|
||||
WINEXE: ${{ inputs.windows-exe }}
|
||||
NUMVAR: ${{ inputs.build-number-var }}
|
||||
TIMEVAR: ${{ inputs.build-time-var }}
|
||||
OFFSET: ${{ inputs.build-number-offset }}
|
||||
RUN: ${{ github.run_number }}
|
||||
run: |
|
||||
set -e
|
||||
export GOOS="${TARGET%/*}" GOARCH="${TARGET#*/}"
|
||||
[ -n "$CGO" ] && export CGO_ENABLED="$CGO"
|
||||
flags="$LDFLAGS"
|
||||
[ -n "$NUMVAR" ] && flags="$flags -X '$NUMVAR=$((RUN + OFFSET))'"
|
||||
[ -n "$TIMEVAR" ] && flags="$flags -X '$TIMEVAR=$(date)'"
|
||||
out="$NAME-$GOOS-$GOARCH"
|
||||
if [ -n "$flags" ]; then go build -ldflags="$flags" -o "$out" "$PKG"; else go build -o "$out" "$PKG"; fi
|
||||
if [ "$GOOS" = windows ] && [ "$WINEXE" = true ]; then mv "$out" "$out.exe"; fi
|
||||
mkdir -p dist && mv "$out"* dist/
|
||||
- id: art
|
||||
env:
|
||||
TARGET: ${{ matrix.target }}
|
||||
run: echo "name=${TARGET/\//-}" >> "$GITHUB_OUTPUT"
|
||||
- uses: https://code.forgejo.org/forgejo/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ inputs.binary-name }}-${{ steps.art.outputs.name }}
|
||||
path: dist/*
|
||||
retention-days: 7
|
||||
|
||||
sbom:
|
||||
if: inputs.sbom-file != ''
|
||||
runs-on: ${{ inputs.runs-on }}
|
||||
steps:
|
||||
- uses: https://code.forgejo.org/actions/checkout@v7
|
||||
- uses: https://code.forgejo.org/actions/setup-go@v7
|
||||
with:
|
||||
go-version: ${{ inputs.go-version }}
|
||||
- run: |
|
||||
go install github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@latest
|
||||
"$(go env GOPATH)/bin/cyclonedx-gomod" app -output "${{ inputs.sbom-file }}"
|
||||
- uses: https://code.forgejo.org/forgejo/upload-artifact@v4
|
||||
with:
|
||||
name: sbom
|
||||
path: ${{ inputs.sbom-file }}
|
||||
retention-days: 7
|
||||
|
||||
upload:
|
||||
needs: build
|
||||
if: inputs.upload && github.ref_name == github.event.repository.default_branch && github.ref_type == 'branch'
|
||||
runs-on: ${{ inputs.runs-on }}
|
||||
steps:
|
||||
- uses: https://code.forgejo.org/forgejo/download-artifact@v4
|
||||
with:
|
||||
path: dist
|
||||
merge-multiple: true
|
||||
- name: Upload
|
||||
env:
|
||||
S3_HOST: ${{ secrets.S3_HOST }}
|
||||
S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }}
|
||||
S3_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }}
|
||||
S3_BUCKET: ${{ secrets.S3_BUCKET }}
|
||||
NAME: ${{ inputs.binary-name }}
|
||||
UPDATE_JSON: ${{ inputs.update-json }}
|
||||
URL: ${{ inputs.update-url }}
|
||||
OFFSET: ${{ inputs.build-number-offset }}
|
||||
RUN: ${{ github.run_number }}
|
||||
run: |
|
||||
set -e
|
||||
curl -fsSL -o /tmp/mc https://dl.min.io/client/mc/release/linux-amd64/mc && chmod +x /tmp/mc
|
||||
/tmp/mc alias set minio "$S3_HOST" "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" >/dev/null
|
||||
if [ "$UPDATE_JSON" = true ]; then
|
||||
printf '{\n "downloadUrl": "%s/%s/%s-%%GOOS%%-%%GOARCH%%%%FILEXTENSION%%",\n "buildNumber": "%s",\n "time": "%s"\n }\n' \
|
||||
"$URL" "$S3_BUCKET" "$NAME" "$((RUN + OFFSET))" "$(date)" > dist/update.json
|
||||
fi
|
||||
/tmp/mc cp dist/* "minio/$S3_BUCKET/"
|
||||
|
|
@ -20,3 +20,14 @@ jobs:
|
|||
with:
|
||||
chart-path: tests/helm/demo
|
||||
version-from-run-number: true
|
||||
|
||||
go:
|
||||
uses: ./.forgejo/workflows/go-binaries.yml
|
||||
with:
|
||||
binary-name: selftest
|
||||
package: ./tests/go
|
||||
targets: '["linux/amd64","windows/arm64"]'
|
||||
build-number-var: main.BuildNumber
|
||||
build-time-var: main.BuildTime
|
||||
build-number-offset: '10'
|
||||
sbom-file: selftest.bom.xml
|
||||
|
|
|
|||
26
README.md
26
README.md
|
|
@ -8,6 +8,7 @@ The repository must stay **public**: Forgejo only resolves reusable workflows fr
|
|||
| Workflow | Does |
|
||||
|---|---|
|
||||
| `docker-build.yml` | multi-arch image build with rootless buildkit, push, trivy scan (image and repo) |
|
||||
| `go-binaries.yml` | Go cross-compile matrix (GOOS/GOARCH), artifacts, optional S3 upload (mc), update.json and SBOM |
|
||||
| `helm-build.yml` | helm lint, package and optional push (OCI or helm package registry) |
|
||||
|
||||
## Docker image
|
||||
|
|
@ -72,6 +73,31 @@ jobs:
|
|||
- `pre-package` (shell, before lint; `REGISTRY` is set), `extra-checks` (shell, after lint), `post-package` (shell, after packaging; `CHART` is the .tgz), `lint-args` (for example `--strict`)
|
||||
- other inputs: `registry`, `app-version` (default: commit SHA), `lint`, `runs-on`; output: `chart-version`
|
||||
|
||||
## Go binaries
|
||||
|
||||
```yaml
|
||||
jobs:
|
||||
binaries:
|
||||
uses: infra/ci-snippets/.forgejo/workflows/go-binaries.yml@master
|
||||
with:
|
||||
binary-name: myapp
|
||||
# targets: '["linux/amd64","darwin/arm64"]' # default: linux, windows, darwin x amd64, arm64
|
||||
# build-number-var: main.BuildNumber # -X <var>=<run number + build-number-offset>
|
||||
# build-time-var: main.BuildTime
|
||||
# build-number-offset: '244' # last pipeline number of the old GitLab project
|
||||
# upload: true # mc cp to S3 on the default branch
|
||||
# update-json: true # also write and upload update.json
|
||||
# sbom-file: myapp.bom.xml # CycloneDX SBOM artifact
|
||||
secrets:
|
||||
S3_HOST: ${{ secrets.S3_HOST }} # only with upload
|
||||
S3_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY_ID }}
|
||||
S3_SECRET_ACCESS_KEY: ${{ secrets.S3_SECRET_ACCESS_KEY }}
|
||||
S3_BUCKET: ${{ secrets.S3_BUCKET }}
|
||||
```
|
||||
|
||||
Other inputs: `package` (default `./`), `ldflags` (default `-s -w`), `cgo-enabled` (default `0`, empty leaves it unset), `windows-exe`, `go-version`, `update-url`, `runs-on`.
|
||||
Each target is an artifact `<binary-name>-<goos>-<goarch>` (7 days). Do not pass `github.*` values through `with:`, they arrive empty.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Runner labels: `buildkit` for `docker-build.yml` and `default` for `helm-build.yml` (`runs-on` input to change).
|
||||
|
|
|
|||
3
tests/go/go.mod
Normal file
3
tests/go/go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module selftest
|
||||
|
||||
go 1.21
|
||||
7
tests/go/main.go
Normal file
7
tests/go/main.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var BuildNumber, BuildTime string
|
||||
|
||||
func main() { fmt.Println("selftest", BuildNumber, BuildTime) }
|
||||
Loading…
Add table
Add a link
Reference in a new issue