Port builder to Forgejo Actions
- helper reads Forgejo (GITHUB_*) variables, CI_* stay as fallback - sequential tag pushes and one retry for Forgejo registry upload races - trivy: native JSON reports and HIGH/CRITICAL table instead of GitLab code quality - step outputs digest and image via GITHUB_OUTPUT - image gets node, git and bash so actions/checkout runs inside it - .forgejo/workflows/build.yml replaces .gitlab-ci.yml Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f571afa98d
commit
2ecb8ddb2b
8 changed files with 368 additions and 56 deletions
67
.forgejo/workflows/build.yml
Normal file
67
.forgejo/workflows/build.yml
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
name: build
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: ['**']
|
||||
tags: ['**']
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: docker
|
||||
steps:
|
||||
- uses: https://code.forgejo.org/actions/checkout@v7
|
||||
- uses: https://code.forgejo.org/actions/setup-go@v7
|
||||
with:
|
||||
go-version-file: helper/go.mod
|
||||
- run: go test ./...
|
||||
working-directory: helper
|
||||
|
||||
# Kaniko builds the image because this image is the builder for everything else:
|
||||
# it cannot build itself on the first run. The kaniko image has no node, so no
|
||||
# checkout action: kaniko clones the repo itself through its git context.
|
||||
build:
|
||||
runs-on: docker
|
||||
if: github.event_name != 'pull_request'
|
||||
needs: test
|
||||
container:
|
||||
image: ghcr.io/kaniko-build/dist/chainguard-dev-kaniko/executor:v1.25.15-debug
|
||||
env:
|
||||
GIT_USERNAME: ${{ github.actor }}
|
||||
GIT_PASSWORD: ${{ github.token }}
|
||||
REGISTRY_USER: ${{ secrets.REGISTRY_USER || github.actor }}
|
||||
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_TOKEN || github.token }}
|
||||
steps:
|
||||
- name: Build and push
|
||||
run: |
|
||||
REGISTRY="${GITHUB_SERVER_URL#*://}"
|
||||
IMAGE="${REGISTRY}/$(echo "$GITHUB_REPOSITORY" | tr 'A-Z' 'a-z')"
|
||||
BRANCH="$(echo "$GITHUB_REF_NAME" | sed 's/\//-/g')"
|
||||
mkdir -p /kaniko/.docker
|
||||
printf '{"auths":{"%s":{"auth":"%s"}}}' "$REGISTRY" "$(printf '%s:%s' "$REGISTRY_USER" "$REGISTRY_PASSWORD" | base64 | tr -d '\n')" > /kaniko/.docker/config.json
|
||||
/kaniko/executor \
|
||||
--context "git://${REGISTRY}/${GITHUB_REPOSITORY}.git#${GITHUB_REF}#${GITHUB_SHA}" \
|
||||
--dockerfile Dockerfile \
|
||||
--destination "${IMAGE}:${BRANCH}" \
|
||||
--destination "${IMAGE}:${GITHUB_SHA}"
|
||||
|
||||
generate-sbom:
|
||||
runs-on: docker
|
||||
steps:
|
||||
- uses: https://code.forgejo.org/actions/checkout@v7
|
||||
- uses: https://code.forgejo.org/actions/setup-go@v7
|
||||
with:
|
||||
go-version-file: helper/go.mod
|
||||
- name: Generate SBOM
|
||||
env:
|
||||
# renovate: datasource=go packageName=github.com/CycloneDX/cyclonedx-gomod
|
||||
CYCLONEDX_GOMOD_VERSION: v1.12.0
|
||||
run: |
|
||||
go install github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@${CYCLONEDX_GOMOD_VERSION}
|
||||
"$(go env GOPATH)/bin/cyclonedx-gomod" app -output docker-build-helper.bom.xml helper/
|
||||
- uses: https://code.forgejo.org/forgejo/upload-artifact@v4
|
||||
with:
|
||||
name: docker-build-helper-bom
|
||||
path: docker-build-helper.bom.xml
|
||||
retention-days: 7
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
build:
|
||||
stage: build
|
||||
image:
|
||||
name: ghcr.io/kaniko-build/dist/chainguard-dev-kaniko/executor:v1.25.15-debug
|
||||
entrypoint: [ "" ]
|
||||
script:
|
||||
- CI_COMMIT_BRANCH=$(echo "$CI_COMMIT_BRANCH" | sed 's/\//-/g')
|
||||
- /kaniko/executor --context "${CI_PROJECT_DIR}" --dockerfile "${CI_PROJECT_DIR}/Dockerfile" --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_BRANCH}" --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}"
|
||||
|
||||
generate-sbom:
|
||||
stage: build
|
||||
image: golang:1.27.4-alpine
|
||||
needs: []
|
||||
variables:
|
||||
# renovate: datasource=go packageName=github.com/CycloneDX/cyclonedx-gomod
|
||||
CYCLONEDX_GOMOD_VERSION: v1.12.0
|
||||
before_script:
|
||||
- go install github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@${CYCLONEDX_GOMOD_VERSION}
|
||||
script:
|
||||
- /go/bin/cyclonedx-gomod app -output docker-build-helper.bom.xml helper/
|
||||
artifacts:
|
||||
paths:
|
||||
- docker-build-helper.bom.xml
|
||||
expire_in: 1 week
|
||||
|
||||
|
|
@ -13,8 +13,11 @@ RUN apk add --no-cache curl tar && \
|
|||
FROM moby/buildkit:v0.35.0-rootless
|
||||
USER root
|
||||
ENV BUILDKIT_COLORS=run=green:warning=yellow:error=red:cancel=255,165,0
|
||||
RUN mkdir /etc/buildkit && \
|
||||
chown user:user /etc/buildkit
|
||||
# nodejs + git: Forgejo Actions run node actions (actions/checkout, upload-artifact) inside the job container.
|
||||
# /workspace and /var/run/act: the runner creates its work dirs there as the container user.
|
||||
RUN apk add --no-cache nodejs git bash && \
|
||||
mkdir /etc/buildkit /workspace /var/run/act && \
|
||||
chown user:user /etc/buildkit /workspace /var/run/act
|
||||
COPY --from=helper-tool /build-image /usr/local/bin/build-image
|
||||
COPY --from=download-trivy /app/ /trivy
|
||||
USER user
|
||||
|
|
|
|||
42
Readme.md
42
Readme.md
|
|
@ -1 +1,43 @@
|
|||
# Docker Build Container using buildkit
|
||||
|
||||
Rootless buildkit plus the `build-image` helper and trivy. It is the builder image behind the
|
||||
reusable workflows in `infra/ci-snippets`, which is the normal way to use it.
|
||||
|
||||
Image: `<forgejo host>/infra/docker-build:master`
|
||||
|
||||
## What `build-image` does
|
||||
|
||||
1. writes the registry login, starts rootless `buildkitd`
|
||||
2. builds for `linux/amd64,linux/arm64` and pushes `<image>:<tag>` and `<image>:<commit sha>`
|
||||
3. writes `image-hash.txt` and the step outputs `digest` and `image` (`$GITHUB_OUTPUT`)
|
||||
4. scans image and repo with trivy: native JSON reports plus a table of HIGH/CRITICAL findings in the log
|
||||
|
||||
## Environment
|
||||
|
||||
Forgejo Actions values are used when no override is set. GitLab CI `CI_*` variables still work as fallback.
|
||||
|
||||
| Variable | Default (Forgejo) | Meaning |
|
||||
|---|---|---|
|
||||
| `REGISTRY` | host of `GITHUB_SERVER_URL` | registry host |
|
||||
| `REGISTRY_USER` / `REGISTRY_PASSWORD` | `GITHUB_ACTOR` / `GITHUB_TOKEN` | registry login |
|
||||
| `IMAGE_NAME` | `<registry>/<lowercase GITHUB_REPOSITORY>` | image without tag |
|
||||
| `IMAGE_TAG` | slug of the branch or tag, `latest` on the default branch | tag |
|
||||
| `DEFAULT_BRANCH` | none | branch that gets `latest` |
|
||||
| `DOCKERFILE_PATH` | `Dockerfile` | path relative to the checkout |
|
||||
| `ARCHITECTURES` | `amd64,arm64` | comma separated |
|
||||
| `IMAGE_PUSH` | `true` | `false` builds without push |
|
||||
| `BUILDKIT_ADDITIONAL_ARGS` | | extra `buildctl build` arguments |
|
||||
| `REGISTRY_MIRROR` | | docker.io mirror for buildkit |
|
||||
| `GOPROXY` / `GOPROXY_DISABLE` | `https://goproxy.jonasled.dev,direct` | passed as build arg |
|
||||
| `TRIVY_SKIP` / `TRIVY_TIMEOUT` | `false` / `5m` | scan control |
|
||||
| `BUILDCTL_TTY` | `false` | colored progress |
|
||||
|
||||
## Runner requirements
|
||||
|
||||
The job container needs a runner that allows `privileged` job containers (rootless buildkit needs user namespaces).
|
||||
The image contains node and git so `actions/checkout` works inside it.
|
||||
|
||||
## This repository
|
||||
|
||||
`.forgejo/workflows/build.yml` runs the helper tests, builds this image with kaniko (the image cannot build
|
||||
itself on the first run) and creates the helper SBOM.
|
||||
|
|
|
|||
|
|
@ -4,12 +4,68 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// firstEnv returns the first non-empty value of the given variables.
|
||||
// Forgejo Actions variables (GITHUB_*) come after the explicit overrides;
|
||||
// GitLab CI variables (CI_*) stay as a fallback for projects not migrated yet.
|
||||
func firstEnv(keys ...string) string {
|
||||
for _, k := range keys {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var slugInvalid = regexp.MustCompile(`[^a-z0-9]+`)
|
||||
|
||||
// slug turns a ref name into a valid image tag, like GitLab's CI_COMMIT_REF_SLUG.
|
||||
func slug(s string) string {
|
||||
s = strings.Trim(slugInvalid.ReplaceAllString(strings.ToLower(s), "-"), "-")
|
||||
if len(s) > 63 {
|
||||
s = strings.TrimRight(s[:63], "-")
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// getRegistry returns the registry host. Forgejo serves its container
|
||||
// registry on the same host as the web UI.
|
||||
func getRegistry() string {
|
||||
if r := firstEnv("REGISTRY", "CI_REGISTRY"); r != "" {
|
||||
return r
|
||||
}
|
||||
url := os.Getenv("GITHUB_SERVER_URL")
|
||||
url = strings.TrimPrefix(strings.TrimPrefix(url, "https://"), "http://")
|
||||
return strings.TrimSuffix(url, "/")
|
||||
}
|
||||
|
||||
func getRegistryUser() string {
|
||||
return firstEnv("REGISTRY_USER", "CI_REGISTRY_USER", "GITHUB_ACTOR")
|
||||
}
|
||||
|
||||
func getRegistryPassword() string {
|
||||
return firstEnv("REGISTRY_PASSWORD", "CI_REGISTRY_PASSWORD", "GITHUB_TOKEN")
|
||||
}
|
||||
|
||||
func getBranch() string {
|
||||
if b := os.Getenv("CI_COMMIT_BRANCH"); b != "" {
|
||||
return b
|
||||
}
|
||||
if os.Getenv("GITHUB_REF_TYPE") == "branch" {
|
||||
return os.Getenv("GITHUB_REF_NAME")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getImageTag() string {
|
||||
imageTag := os.Getenv("CI_COMMIT_REF_SLUG")
|
||||
if os.Getenv("CI_COMMIT_BRANCH") == os.Getenv("CI_DEFAULT_BRANCH") {
|
||||
imageTag := firstEnv("CI_COMMIT_REF_SLUG")
|
||||
if imageTag == "" {
|
||||
imageTag = slug(os.Getenv("GITHUB_REF_NAME"))
|
||||
}
|
||||
if b := getBranch(); b != "" && b == firstEnv("DEFAULT_BRANCH", "CI_DEFAULT_BRANCH") {
|
||||
imageTag = "latest"
|
||||
}
|
||||
if os.Getenv("IMAGE_TAG") != "" {
|
||||
|
|
@ -19,10 +75,14 @@ func getImageTag() string {
|
|||
}
|
||||
|
||||
func getImageName() string {
|
||||
if os.Getenv("IMAGE_NAME") != "" {
|
||||
return os.Getenv("IMAGE_NAME")
|
||||
if n := firstEnv("IMAGE_NAME", "CI_REGISTRY_IMAGE"); n != "" {
|
||||
return n
|
||||
}
|
||||
return os.Getenv("CI_REGISTRY_IMAGE")
|
||||
// Image names must be lowercase, repository names may not be.
|
||||
if repo, reg := os.Getenv("GITHUB_REPOSITORY"), getRegistry(); repo != "" && reg != "" {
|
||||
return reg + "/" + strings.ToLower(repo)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getDockerfilePath() string {
|
||||
|
|
@ -60,21 +120,21 @@ func getPushImage() string {
|
|||
}
|
||||
|
||||
func getCommitSha() string {
|
||||
return os.Getenv("CI_COMMIT_SHA")
|
||||
return firstEnv("CI_COMMIT_SHA", "GITHUB_SHA")
|
||||
}
|
||||
|
||||
// validateEnv fails early when a variable the build depends on is unset,
|
||||
// instead of producing a broken image or docker config.
|
||||
func validateEnv() error {
|
||||
var missing []string
|
||||
if os.Getenv("CI_REGISTRY") == "" {
|
||||
missing = append(missing, "CI_REGISTRY")
|
||||
if getRegistry() == "" {
|
||||
missing = append(missing, "REGISTRY (or GITHUB_SERVER_URL, CI_REGISTRY)")
|
||||
}
|
||||
if getImageName() == "" {
|
||||
missing = append(missing, "IMAGE_NAME or CI_REGISTRY_IMAGE")
|
||||
missing = append(missing, "IMAGE_NAME (or GITHUB_REPOSITORY, CI_REGISTRY_IMAGE)")
|
||||
}
|
||||
if getCommitSha() == "" {
|
||||
missing = append(missing, "CI_COMMIT_SHA")
|
||||
missing = append(missing, "GITHUB_SHA (or CI_COMMIT_SHA)")
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return fmt.Errorf("missing required environment variables: %s", strings.Join(missing, ", "))
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ func clearEnv(t *testing.T, overrides map[string]string) {
|
|||
"IMAGE_NAME", "CI_REGISTRY_IMAGE", "CI_REGISTRY", "CI_COMMIT_SHA", "ARCHITECTURES",
|
||||
"GOPROXY", "GOPROXY_DISABLE", "IMAGE_PUSH", "DOCKERFILE_PATH", "BUILDKIT_ADDITIONAL_ARGS",
|
||||
"CI_REGISTRY_USER", "CI_REGISTRY_PASSWORD", "REGISTRY_MIRROR",
|
||||
"GITHUB_SERVER_URL", "GITHUB_REPOSITORY", "GITHUB_SHA", "GITHUB_REF_NAME", "GITHUB_REF_TYPE",
|
||||
"GITHUB_ACTOR", "GITHUB_TOKEN", "GITHUB_OUTPUT", "DEFAULT_BRANCH", "REGISTRY", "REGISTRY_USER",
|
||||
"REGISTRY_PASSWORD", "GITLAB_CI",
|
||||
} {
|
||||
t.Setenv(k, "")
|
||||
}
|
||||
|
|
@ -31,6 +34,10 @@ func TestGetImageTag(t *testing.T) {
|
|||
{"branch uses ref slug", map[string]string{"CI_COMMIT_REF_SLUG": "feature-x", "CI_COMMIT_BRANCH": "feature/x", "CI_DEFAULT_BRANCH": "master"}, "feature-x"},
|
||||
{"default branch is latest", map[string]string{"CI_COMMIT_REF_SLUG": "master", "CI_COMMIT_BRANCH": "master", "CI_DEFAULT_BRANCH": "master"}, "latest"},
|
||||
{"IMAGE_TAG overrides default branch", map[string]string{"CI_COMMIT_BRANCH": "master", "CI_DEFAULT_BRANCH": "master", "IMAGE_TAG": "v1.2.3"}, "v1.2.3"},
|
||||
{"forgejo branch is slugged", map[string]string{"GITHUB_REF_NAME": "Feature/X_y", "GITHUB_REF_TYPE": "branch", "DEFAULT_BRANCH": "master"}, "feature-x-y"},
|
||||
{"forgejo default branch is latest", map[string]string{"GITHUB_REF_NAME": "master", "GITHUB_REF_TYPE": "branch", "DEFAULT_BRANCH": "master"}, "latest"},
|
||||
{"forgejo tag is slugged", map[string]string{"GITHUB_REF_NAME": "v1.2.3", "GITHUB_REF_TYPE": "tag", "DEFAULT_BRANCH": "master"}, "v1-2-3"},
|
||||
{"forgejo tag named like default branch is not latest", map[string]string{"GITHUB_REF_NAME": "master", "GITHUB_REF_TYPE": "tag", "DEFAULT_BRANCH": "master"}, "master"},
|
||||
{"IMAGE_TAG overrides branch slug", map[string]string{"CI_COMMIT_REF_SLUG": "feature-x", "CI_COMMIT_BRANCH": "feature/x", "CI_DEFAULT_BRANCH": "master", "IMAGE_TAG": "pinned"}, "pinned"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
|
@ -111,7 +118,7 @@ func TestValidateEnv(t *testing.T) {
|
|||
if err == nil {
|
||||
t.Fatal("validateEnv() = nil, want error")
|
||||
}
|
||||
for _, name := range []string{"CI_REGISTRY", "IMAGE_NAME or CI_REGISTRY_IMAGE", "CI_COMMIT_SHA"} {
|
||||
for _, name := range []string{"REGISTRY", "IMAGE_NAME", "GITHUB_SHA"} {
|
||||
if !strings.Contains(err.Error(), name) {
|
||||
t.Errorf("error %q does not mention %s", err, name)
|
||||
}
|
||||
|
|
@ -133,3 +140,52 @@ func TestValidateEnv(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestForgejoEnv(t *testing.T) {
|
||||
clearEnv(t, map[string]string{
|
||||
"GITHUB_SERVER_URL": "https://forgejo.example.com/",
|
||||
"GITHUB_REPOSITORY": "Infra/Docker-Build",
|
||||
"GITHUB_SHA": "abc123",
|
||||
"GITHUB_ACTOR": "ci-user",
|
||||
"GITHUB_TOKEN": "tok",
|
||||
})
|
||||
if err := validateEnv(); err != nil {
|
||||
t.Fatalf("validateEnv() = %v, want nil", err)
|
||||
}
|
||||
checks := map[string][2]string{
|
||||
"registry": {getRegistry(), "forgejo.example.com"},
|
||||
"image": {getImageName(), "forgejo.example.com/infra/docker-build"},
|
||||
"user": {getRegistryUser(), "ci-user"},
|
||||
"password": {getRegistryPassword(), "tok"},
|
||||
"sha": {getCommitSha(), "abc123"},
|
||||
}
|
||||
for name, c := range checks {
|
||||
if c[0] != c[1] {
|
||||
t.Errorf("%s = %q, want %q", name, c[0], c[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplicitOverridesWin(t *testing.T) {
|
||||
clearEnv(t, map[string]string{
|
||||
"GITHUB_SERVER_URL": "https://forgejo.example.com", "GITHUB_REPOSITORY": "a/b", "GITHUB_ACTOR": "actor", "GITHUB_TOKEN": "t",
|
||||
"REGISTRY": "r.example.com", "IMAGE_NAME": "r.example.com/custom", "REGISTRY_USER": "u", "REGISTRY_PASSWORD": "p",
|
||||
})
|
||||
if getRegistry() != "r.example.com" || getImageName() != "r.example.com/custom" || getRegistryUser() != "u" || getRegistryPassword() != "p" {
|
||||
t.Errorf("explicit variables must override GITHUB_*: %q %q %q %q", getRegistry(), getImageName(), getRegistryUser(), getRegistryPassword())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlug(t *testing.T) {
|
||||
long := strings.Repeat("a", 70)
|
||||
for in, want := range map[string]string{
|
||||
"Feature/X_y": "feature-x-y",
|
||||
"--odd--": "odd",
|
||||
"v1.2.3": "v1-2-3",
|
||||
long: long[:63],
|
||||
} {
|
||||
if got := slug(in); got != want {
|
||||
t.Errorf("slug(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
109
helper/main.go
109
helper/main.go
|
|
@ -70,20 +70,54 @@ func buildImage() error {
|
|||
run = runCmdTTY
|
||||
}
|
||||
|
||||
args := []string{"--addr", buildkitAddr(), "build", "--progress=" + progress, "--frontend=dockerfile.v0", "--opt", "platform=" + getArchitectures()}
|
||||
base := []string{"--addr", buildkitAddr(), "build", "--progress=" + progress, "--frontend=dockerfile.v0", "--opt", "platform=" + getArchitectures()}
|
||||
if goProxy := getGoProxy(); goProxy != "" {
|
||||
args = append(args, "--opt", "build-arg:GOPROXY="+goProxy)
|
||||
base = append(base, "--opt", "build-arg:GOPROXY="+goProxy)
|
||||
}
|
||||
args = append(args,
|
||||
base = append(base,
|
||||
"--local", "context="+cwd,
|
||||
"--local", "dockerfile="+getDockerfilePath(),
|
||||
"--output", fmt.Sprintf(`type=image,"name=%s:%s,%s:%s",push=%s`, imageName, getImageTag(), imageName, getCommitSha(), getPushImage()),
|
||||
"--metadata-file", buildMetadataPath,
|
||||
)
|
||||
args = append(args, strings.Fields(getAdditionalArgs())...)
|
||||
|
||||
fmt.Println("Assembled build command: ", "buildctl", strings.Join(args, " "))
|
||||
return run(append([]string{"buildctl"}, args...))
|
||||
// Forgejo answers concurrent uploads of the same blob with HTTP 500, which is what pushing
|
||||
// two names in one build does. Build once per name instead: the later builds are fully
|
||||
// cached and find the blobs already in the registry.
|
||||
// Forgejo also races parallel layer uploads of a brand new image, hence the retry.
|
||||
attempts := 1
|
||||
if getPushImage() == "true" {
|
||||
attempts = 2
|
||||
}
|
||||
for i, tag := range []string{getImageTag(), getCommitSha()} {
|
||||
metadata := buildMetadataPath
|
||||
if i > 0 {
|
||||
metadata = fmt.Sprintf("%s.%d", buildMetadataPath, i)
|
||||
}
|
||||
args := append(append([]string{}, base...),
|
||||
"--output", fmt.Sprintf(`type=image,"name=%s:%s",push=%s`, imageName, tag, getPushImage()),
|
||||
"--metadata-file", metadata,
|
||||
)
|
||||
args = append(args, strings.Fields(getAdditionalArgs())...)
|
||||
|
||||
fmt.Println("Assembled build command: ", "buildctl", strings.Join(args, " "))
|
||||
if err := withRetry(attempts, func() error { return run(append([]string{"buildctl"}, args...)) }); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// withRetry calls f up to attempts times and returns the last error.
|
||||
func withRetry(attempts int, f func() error) error {
|
||||
var err error
|
||||
for i := 1; i <= attempts; i++ {
|
||||
if err = f(); err == nil {
|
||||
return nil
|
||||
}
|
||||
if i < attempts {
|
||||
fmt.Printf("Attempt %d/%d failed (%v), retrying\n", i, attempts, err)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func runTrivyScan() error {
|
||||
|
|
@ -95,12 +129,20 @@ func runTrivyScan() error {
|
|||
timeout = os.Getenv("TRIVY_TIMEOUT")
|
||||
}
|
||||
image := getImageName() + ":" + getImageTag()
|
||||
gitlab := os.Getenv("GITLAB_CI") == "true"
|
||||
|
||||
// Forgejo has no code quality widget: keep trivy's native JSON as artifact
|
||||
// and print a table of the serious findings to the log instead.
|
||||
reportFormat := []string{"--format", "json"}
|
||||
if gitlab {
|
||||
reportFormat = []string{"--format", "template", "--template", codeQualityTemplate}
|
||||
}
|
||||
|
||||
// The image scan runs first so the vulnerability DB is downloaded once,
|
||||
// before the other scans read it in parallel.
|
||||
fmt.Println("Using trivy to scan image")
|
||||
if err := runCmd(trivy("image", "--format", "template", "--template", codeQualityTemplate,
|
||||
"-o", "image-scan-report.json", "--timeout", timeout, image)); err != nil {
|
||||
if err := runCmd(trivy("image", append(reportFormat,
|
||||
"-o", "image-scan-report.json", "--timeout", timeout, image)...)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -108,16 +150,24 @@ func runTrivyScan() error {
|
|||
if err := runCmdsConcurrently(
|
||||
trivy("image", "--format", "cyclonedx",
|
||||
"--output", "image-scan-report.bom.json", "--timeout", timeout, image),
|
||||
trivy("filesystem", "--scanners", "misconfig,vuln",
|
||||
"--format", "template", "--template", codeQualityTemplate,
|
||||
"-o", "misc-scan-report.json", "--timeout", timeout, "."),
|
||||
trivy("filesystem", append([]string{"--scanners", "misconfig,vuln"}, append(reportFormat,
|
||||
"-o", "misc-scan-report.json", "--timeout", timeout, ".")...)...),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("merging scan reports")
|
||||
if err := mergeJSONFiles("image-scan-report.json", "misc-scan-report.json", "scan-report.json"); err != nil {
|
||||
return fmt.Errorf("merging scan reports: %w", err)
|
||||
if gitlab {
|
||||
fmt.Println("merging scan reports")
|
||||
if err := mergeJSONFiles("image-scan-report.json", "misc-scan-report.json", "scan-report.json"); err != nil {
|
||||
return fmt.Errorf("merging scan reports: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, report := range []string{"image-scan-report.json", "misc-scan-report.json"} {
|
||||
fmt.Println("Findings in", report)
|
||||
if err := runCmd([]string{trivyBin, "convert", "--format", "table", "--severity", "HIGH,CRITICAL", report}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -128,10 +178,10 @@ func writeDockerConfig() error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("finding home directory: %w", err)
|
||||
}
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(os.Getenv("CI_REGISTRY_USER") + ":" + os.Getenv("CI_REGISTRY_PASSWORD")))
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(getRegistryUser() + ":" + getRegistryPassword()))
|
||||
config := map[string]any{
|
||||
"auths": map[string]any{
|
||||
os.Getenv("CI_REGISTRY"): map[string]string{"auth": auth},
|
||||
getRegistry(): map[string]string{"auth": auth},
|
||||
},
|
||||
}
|
||||
data, err := json.Marshal(config)
|
||||
|
|
@ -190,6 +240,29 @@ func writeManifestDigest() error {
|
|||
if err := os.WriteFile("./image-hash.txt", []byte(result.ContainerImageDigest), 0644); err != nil {
|
||||
return fmt.Errorf("writing digest to file: %w", err)
|
||||
}
|
||||
return writeStepOutputs(map[string]string{
|
||||
"digest": result.ContainerImageDigest,
|
||||
"image": getImageName() + ":" + getImageTag(),
|
||||
})
|
||||
}
|
||||
|
||||
// writeStepOutputs appends outputs for later workflow steps. It does nothing
|
||||
// outside of Forgejo/GitHub Actions, where GITHUB_OUTPUT is unset.
|
||||
func writeStepOutputs(outputs map[string]string) error {
|
||||
path := os.Getenv("GITHUB_OUTPUT")
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening GITHUB_OUTPUT: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
for k, v := range outputs {
|
||||
if _, err := fmt.Fprintf(f, "%s=%s\n", k, v); err != nil {
|
||||
return fmt.Errorf("writing GITHUB_OUTPUT: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
|
@ -114,3 +115,38 @@ func TestWriteDockerConfig(t *testing.T) {
|
|||
t.Errorf("decoded auth = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStepOutputs(t *testing.T) {
|
||||
clearEnv(t, nil)
|
||||
if err := writeStepOutputs(map[string]string{"digest": "sha256:x"}); err != nil {
|
||||
t.Fatalf("without GITHUB_OUTPUT: %v", err)
|
||||
}
|
||||
out := filepath.Join(t.TempDir(), "output")
|
||||
t.Setenv("GITHUB_OUTPUT", out)
|
||||
if err := writeStepOutputs(map[string]string{"digest": "sha256:x"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, _ := os.ReadFile(out)
|
||||
if string(data) != "digest=sha256:x\n" {
|
||||
t.Errorf("GITHUB_OUTPUT = %q", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithRetry(t *testing.T) {
|
||||
calls := 0
|
||||
err := withRetry(2, func() error {
|
||||
calls++
|
||||
if calls == 1 {
|
||||
return errors.New("first fails")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil || calls != 2 {
|
||||
t.Errorf("err = %v after %d calls, want nil after 2", err, calls)
|
||||
}
|
||||
|
||||
calls = 0
|
||||
if err := withRetry(2, func() error { calls++; return errors.New("always") }); err == nil || calls != 2 {
|
||||
t.Errorf("err = %v after %d calls, want error after 2", err, calls)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue