Add reusable docker and helm build workflows
Some checks failed
docker-build.yml / Add reusable docker and helm build workflows (push) Failing after 0s
helm-build.yml / Add reusable docker and helm build workflows (push) Failing after 0s
selftest / docker (push) Has been cancelled
selftest / helm (push) Has been cancelled
selftest / build (push) Has been cancelled
selftest / build-1 (push) Has been cancelled

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Jonas Leder 2026-11-18 00:10:23 +01:00
commit b0e9b75cec
8 changed files with 365 additions and 0 deletions

View file

@ -0,0 +1,99 @@
# Reusable workflow: build a multi-arch image with rootless buildkit, push it, scan it with trivy.
# Usage: see README.md
name: docker-build
on:
workflow_call:
inputs:
image-name:
description: Image without tag. Default is <registry>/<owner>/<repo>, lowercase.
type: string
default: ''
image-tag:
description: Default is the branch/tag slug, and latest on the default branch.
type: string
default: ''
dockerfile-path:
description: Directory with the Dockerfile, relative to the repository root.
type: string
default: ''
architectures:
description: Comma separated, for example amd64,arm64.
type: string
default: amd64,arm64
push:
type: boolean
default: true
additional-args:
description: Extra arguments for buildctl build, for example --opt build-arg:FOO=bar.
type: string
default: ''
trivy-skip:
type: boolean
default: false
trivy-timeout:
type: string
default: 5m
registry-mirror:
description: docker.io mirror for buildkit.
type: string
default: ''
builder-image:
type: string
default: forgejo.jonasled.dev/infra/docker-build:master
runs-on:
description: Runner label. The runner must allow privileged job containers.
type: string
default: docker
secrets:
REGISTRY_USER:
description: Registry user. Default is the workflow actor.
required: false
REGISTRY_TOKEN:
description: Token with write:package. Default is the automatic workflow token.
required: false
outputs:
digest:
description: Digest of the pushed image.
value: ${{ jobs.build.outputs.digest }}
image:
description: Image reference with tag.
value: ${{ jobs.build.outputs.image }}
jobs:
build:
runs-on: ${{ inputs.runs-on }}
container:
image: ${{ inputs.builder-image }}
outputs:
digest: ${{ steps.build.outputs.digest }}
image: ${{ steps.build.outputs.image }}
steps:
- uses: https://code.forgejo.org/actions/checkout@v7
- name: Build and push
id: build
run: build-image
env:
IMAGE_NAME: ${{ inputs.image-name }}
IMAGE_TAG: ${{ inputs.image-tag }}
DOCKERFILE_PATH: ${{ inputs.dockerfile-path }}
ARCHITECTURES: ${{ inputs.architectures }}
IMAGE_PUSH: ${{ inputs.push }}
BUILDKIT_ADDITIONAL_ARGS: ${{ inputs.additional-args }}
TRIVY_SKIP: ${{ inputs.trivy-skip }}
TRIVY_TIMEOUT: ${{ inputs.trivy-timeout }}
REGISTRY_MIRROR: ${{ inputs.registry-mirror }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
REGISTRY_USER: ${{ secrets.REGISTRY_USER || github.actor }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_TOKEN || github.token }}
- uses: https://code.forgejo.org/forgejo/upload-artifact@v4
if: always()
with:
name: build-reports
path: |
image-hash.txt
image-scan-report.json
image-scan-report.bom.json
misc-scan-report.json
if-no-files-found: ignore
retention-days: 14

View file

@ -0,0 +1,117 @@
# Reusable workflow: lint and package a helm chart, optionally push it to the Forgejo registry.
# Usage: see README.md
name: helm-build
on:
workflow_call:
inputs:
chart-path:
description: Directory with Chart.yaml, relative to the repository root.
type: string
default: .
version:
description: Chart version. Default is the tag without leading v on tag builds, otherwise Chart.yaml.
type: string
default: ''
app-version:
type: string
default: ''
push:
type: boolean
default: false
registry-type:
description: oci pushes to the container registry, classic to the helm package registry.
type: string
default: oci
oci-path:
description: Where oci pushes to, without host and chart name. Default is <owner>/<repo>/charts, lowercase.
type: string
default: ''
lint:
type: boolean
default: true
runs-on:
type: string
default: docker
secrets:
REGISTRY_USER:
required: false
REGISTRY_TOKEN:
required: false
outputs:
chart-version:
value: ${{ jobs.build.outputs.chart-version }}
jobs:
build:
runs-on: ${{ inputs.runs-on }}
outputs:
chart-version: ${{ steps.package.outputs.chart-version }}
env:
# renovate: datasource=github-releases depName=helm packageName=helm/helm
HELM_VERSION: v4.3.2
steps:
- uses: https://code.forgejo.org/actions/checkout@v7
- name: Install helm
run: |
set -eu
case "$(uname -m)" in x86_64) arch=amd64 ;; aarch64) arch=arm64 ;; *) echo "unsupported arch"; exit 1 ;; esac
mkdir -p "$HOME/helm-bin"
curl -fsSL "https://get.helm.sh/helm-${HELM_VERSION}-linux-${arch}.tar.gz" | tar -xz -C "$HOME/helm-bin" --strip-components=1 "linux-${arch}/helm"
echo "$HOME/helm-bin" >> "$GITHUB_PATH"
- name: Lint
if: inputs.lint
run: |
set -eu
helm dependency update "$CHART_PATH"
helm lint "$CHART_PATH"
env:
CHART_PATH: ${{ inputs.chart-path }}
- name: Package
id: package
run: |
set -eu
version="$INPUT_VERSION"
if [ -z "$version" ] && [ "$GITHUB_REF_TYPE" = tag ]; then version="${GITHUB_REF_NAME#v}"; fi
args=""
[ -n "$version" ] && args="$args --version $version"
[ -n "$INPUT_APP_VERSION" ] && args="$args --app-version $INPUT_APP_VERSION"
helm dependency update "$CHART_PATH"
# shellcheck disable=SC2086
helm package "$CHART_PATH" $args --destination dist
chart="$(ls dist/*.tgz)"
echo "chart-version=$(helm show chart "$chart" | sed -n 's/^version: //p')" >> "$GITHUB_OUTPUT"
echo "chart=$chart" >> "$GITHUB_OUTPUT"
env:
CHART_PATH: ${{ inputs.chart-path }}
INPUT_VERSION: ${{ inputs.version }}
INPUT_APP_VERSION: ${{ inputs.app-version }}
- name: Push
if: inputs.push
run: |
set -eu
host="${GITHUB_SERVER_URL#*://}"
chart="${{ steps.package.outputs.chart }}"
if [ "$REGISTRY_TYPE" = classic ]; then
curl -fsS --fail-with-body -u "$REGISTRY_USER:$REGISTRY_PASSWORD" -X POST --upload-file "$chart" \
"${GITHUB_SERVER_URL}/api/packages/${GITHUB_REPOSITORY_OWNER}/helm/api/charts"
exit 0
fi
path="${OCI_PATH:-$(echo "$GITHUB_REPOSITORY/charts" | tr 'A-Z' 'a-z')}"
printf '%s' "$REGISTRY_PASSWORD" | helm registry login "$host" -u "$REGISTRY_USER" --password-stdin
# Forgejo races parallel blob uploads of a new package: the second attempt finds the package.
for attempt in 1 2 3; do
helm push "$chart" "oci://${host}/${path}" && exit 0
echo "push attempt $attempt failed, retrying"; sleep 2
done
exit 1
env:
REGISTRY_TYPE: ${{ inputs.registry-type }}
OCI_PATH: ${{ inputs.oci-path }}
REGISTRY_USER: ${{ secrets.REGISTRY_USER || github.actor }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_TOKEN || github.token }}
- uses: https://code.forgejo.org/forgejo/upload-artifact@v4
with:
name: chart
path: dist/*.tgz
retention-days: 14

View file

@ -0,0 +1,21 @@
name: selftest
on:
push:
pull_request:
workflow_dispatch:
# Calls the reusable workflows with push disabled to prove they work on this instance.
jobs:
docker:
uses: ./.forgejo/workflows/docker-build.yml
with:
dockerfile-path: tests/docker
architectures: amd64
push: false
trivy-skip: true
helm:
uses: ./.forgejo/workflows/helm-build.yml
with:
chart-path: tests/helm/demo

89
README.md Normal file
View file

@ -0,0 +1,89 @@
# 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) |
| `helm-build.yml` | helm lint, package and optional push (OCI or helm package registry) |
## Docker image
`.forgejo/workflows/build.yml` in your repository:
```yaml
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): `image-name`, `image-tag`, `dockerfile-path` (directory), `architectures`, `push`,
`additional-args`, `trivy-skip`, `trivy-timeout`, `registry-mirror`, `builder-image`, `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:
```yaml
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: docker
steps:
- run: echo "${{ needs.docker.outputs.digest }}"
```
## Helm chart
```yaml
jobs:
chart:
uses: infra/ci-snippets/.forgejo/workflows/helm-build.yml@master
with:
chart-path: chart
push: ${{ github.ref_type == 'tag' }}
secrets: inherit
```
- version: `version` input, else the tag without a leading `v` on tag builds, else `Chart.yaml`
- `registry-type: oci` (default) pushes to `oci://<host>/<owner>/<repo>/charts`, so the old GitLab path
`jonasled.dev/<owner>/<repo>/charts` stays the same; change it with `oci-path`
- `registry-type: classic` pushes to `<server>/api/packages/<owner>/helm`, consume it with
`helm repo add x <server>/api/packages/<owner>/helm`
- other inputs: `app-version`, `lint`, `runs-on`; output: `chart-version`
## Requirements
- Runner with the label `docker` (or set `runs-on`). `docker-build.yml` needs a runner that allows
**privileged job containers**: rootless buildkit needs user namespaces.
- Forgejo with `workflow_call` support, and a runner version that supports it.
- Push credentials: by default the automatic workflow token. If it cannot write packages in your setup, create
a token with `write:package` and pass it as secret `REGISTRY_TOKEN` (and `REGISTRY_USER`);
`secrets: inherit` passes them on.
- `builder-image` defaults to `forgejo.jonasled.dev/infra/docker-build:master`. After the domain cutover change the
default here once.
## 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.

26
renovate.json Normal file
View file

@ -0,0 +1,26 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
],
"packageRules": [
{
"matchManagers": [
"custom.regex"
],
"extractVersion": "^v?(?<version>.*)$"
}
],
"customManagers": [
{
"customType": "regex",
"description": "Update _VERSION variables",
"managerFilePatterns": [
"/.*/"
],
"matchStrings": [
"\\s*# renovate: datasource=(?<datasource>[a-z-]+?)(?: depName=(?<depName>.+?))? packageName=(?<packageName>.+?)(?: versioning=(?<versioning>[a-z-]+?))?\\s(?:ENV|ARG)? .+?_VERSION[:=]\\s?(?<currentValue>.+?)\\s"
]
}
]
}

2
tests/docker/Dockerfile Normal file
View file

@ -0,0 +1,2 @@
FROM alpine:3.24
RUN echo ci-snippets > /x

View file

@ -0,0 +1,5 @@
apiVersion: v2
name: demo
description: Chart for the ci-snippets self-test
version: 0.1.0
appVersion: "1.0"

View file

@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name }}-demo
data:
hello: world