Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
5.8 KiB
ci-snippets
Reusable Forgejo Actions workflows for the infra organization. Other repositories include them with
uses:, so a docker or helm build is a few lines.
The repository must stay public: Forgejo only resolves reusable workflows from public repositories.
| 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 (curl SigV4), update.json and SBOM |
helm-build.yml |
helm lint, package and optional push (OCI or helm package registry) |
Docker image
.forgejo/workflows/build.yml in your repository:
on:
push:
branches: ['**']
tags: ['**']
jobs:
docker:
uses: infra/ci-snippets/.forgejo/workflows/docker-build.yml@master
secrets: inherit
Pushes <registry>/<owner>/<repo>:<branch> and :<commit sha>, latest on the default branch.
Inputs (all optional): registry, image-name, image-suffix (appended to the default name, for several images per repo),
context-dir (build subdirectory), pre-build (shell command before the build), image-tag, dockerfile-path (directory), architectures, push,
additional-args, trivy-skip, trivy-timeout, registry-mirror, runs-on.
Outputs: digest, image. Reports (trivy JSON, SBOM, image-hash.txt) are uploaded as artifact build-reports.
Example that keeps the image name from GitLab, builds one architecture and uses the digest:
jobs:
docker:
uses: infra/ci-snippets/.forgejo/workflows/docker-build.yml@master
with:
image-name: forgejo.jonasled.dev/infra/some-image
architectures: amd64
secrets: inherit
deploy:
needs: docker
runs-on: default
steps:
- run: echo "${{ needs.docker.outputs.digest }}"
Helm chart
jobs:
chart:
uses: infra/ci-snippets/.forgejo/workflows/helm-build.yml@master
with:
chart-path: chart
push: ${{ github.ref_type == 'tag' }}
secrets: inherit
- version:
versioninput, else the tag without a leadingvon tag builds, elseChart.yaml registry-type: oci(default) pushes tooci://<host>/<owner>/<repo>/charts, so the old GitLab pathjonasled.dev/<owner>/<repo>/chartsstays the same; change it withoci-pathregistry-type: classicpushes tohttps://<registry>/api/packages/<owner>/helm, consume it withhelm repo add x https://<registry>/api/packages/<owner>/helmversion-from-run-number: true: version becomes<major.minor of Chart.yaml>.<run number>, so versions only grow (Flux picks the highest);version-offsetis added to the run number, set it to the last pipeline number of the old GitLab projectpre-package(shell, before lint;REGISTRYis set),extra-checks(shell, after lint),post-package(shell, after packaging;CHARTis the .tgz),lint-args(for example--strict)- other inputs:
registry,app-version(default: commit SHA),lint,runs-on; output:chart-version
Go binaries
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 # PUT 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: module-dir (go.mod directory, default .), 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:
buildkitfordocker-build.ymlanddefaultforhelm-build.yml(runs-oninput to change). Thebuildkitlabel is defined in the flux repo (services/forgejo-runner): its pod spec runs theinfra/docker-buildimage as non-root withseccompandapparmorunconfined, not privileged. The builder image and its tag are set there, not in the workflows. registrydefaults toforgejo.jonasled.devand is not derived from the server URL, because the runner reports its internal cluster URL (http://forgejo-http.forgejo.svc.cluster.local:3000). After the domain cutover change the default in both workflows once.- Forgejo with
workflow_callsupport, and a runner version that supports it. - Push credentials: the automatic workflow token cannot push packages on this instance (401). The org
infrahas the secretsREGISTRY_USER(ci-bot) andREGISTRY_TOKEN(scopewrite:package,read:package). Other orgs and users need the same two secrets;secrets: inheritpasses them on.
Known behaviour
Forgejo's container registry answers parallel uploads of a new package with HTTP 500 (duplicate package row). Both workflows retry the push, and the builder pushes each tag in its own build to avoid pushing the same blobs twice.
Tests
.forgejo/workflows/selftest.yml calls both workflows against tests/ without pushing.
selftest-push.yml (manual) pushes a multi-arch test image and a test chart; delete the packages afterwards.