From b5864632bb787978bc197a8cac7205a680eef9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sat, 5 Dec 2026 15:04:32 +0100 Subject: [PATCH] ci: caches, prebuilt linters, image from the built binaries - Go module/build cache, golangci-lint cache and npm cache in the runners' S3 cache for lint, test and build; only main saves (one entry per commit), PRs and tags restore; release restores the build cache - golangci-lint from the release tarball (sha256-checked) instead of compiling it with go install on every run - shellcheck from its static release binary instead of apt-get on every run - the image job packs the build job's amd64 binaries (Dockerfile.ci, distroless, same entrypoint) with the runner's Docker daemon instead of Kaniko rebuilding frontend and Go from the git context; Dockerfile stays for local/make builds Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/ci.yaml | 160 +++++++++++++++++++++++++--------- .gitea/workflows/release.yaml | 15 +++- Dockerfile.ci | 7 ++ 3 files changed, 138 insertions(+), 44 deletions(-) create mode 100644 Dockerfile.ci diff --git a/.gitea/workflows/ci.yaml b/.gitea/workflows/ci.yaml index 1103a01..869522f 100644 --- a/.gitea/workflows/ci.yaml +++ b/.gitea/workflows/ci.yaml @@ -13,7 +13,10 @@ concurrency: env: GOLANGCI_LINT_VERSION: v2.15.0 SQLC_VERSION: v1.31.1 + # sha256 of golangci-lint--linux-amd64.tar.gz from the release's checksums.txt + GOLANGCI_LINT_SHA256: 9b7fe6a6bf6334b98decf8d86b11415ea7889afdabdd1959fdce23af2a9ebcea HELM_VERSION: v3.19.0 + SHELLCHECK_VERSION: v0.12.0 jobs: lint: @@ -29,8 +32,24 @@ jobs: curl -fsSL "https://go.dev/dl/go${v}.linux-amd64.tar.gz" | tar -C /usr/local -xz echo /usr/local/go/bin >> "$GITHUB_PATH" echo "$HOME/go/bin" >> "$GITHUB_PATH" + echo "GO_VERSION=$v" >> "$GITHUB_ENV" + # Module, build and npm caches live in the runners' S3 cache. Only main + # saves them (one entry per commit, expired by the bucket after 7 days); + # pull requests and tags restore the newest one from main. + - name: Restore caches + uses: actions/cache/restore@v4 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/.cache/golangci-lint + ~/.npm + key: lint-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}-${{ github.sha }} + restore-keys: | + lint-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}- + lint-go${{ env.GO_VERSION }}- - name: Build frontend - run: npm ci --no-audit --no-fund && npm run build + run: npm ci --no-audit --no-fund --prefer-offline && npm run build - name: Generated code is up to date run: | go tool templ generate @@ -42,10 +61,15 @@ jobs: run: test -z "$(gofmt -l internal cmd tools deploy)" || { gofmt -l internal cmd tools deploy; exit 1; } - name: go vet run: go vet ./... + # The release binary instead of `go install`, which compiled the linter + # from source on every run. - name: golangci-lint run: | - go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@${GOLANGCI_LINT_VERSION} - "$(go env GOPATH)/bin/golangci-lint" run ./... + v="${GOLANGCI_LINT_VERSION#v}" + curl -fsSLo /tmp/golangci-lint.tar.gz "https://github.com/golangci/golangci-lint/releases/download/${GOLANGCI_LINT_VERSION}/golangci-lint-${v}-linux-amd64.tar.gz" + echo "${GOLANGCI_LINT_SHA256} /tmp/golangci-lint.tar.gz" | sha256sum -c - + tar -xzf /tmp/golangci-lint.tar.gz -C /usr/local/bin --strip-components=1 "golangci-lint-${v}-linux-amd64/golangci-lint" + golangci-lint run ./... - name: Helm chart run: | curl -fsSL "https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz" | tar -xz -C /usr/local/bin --strip-components=1 linux-amd64/helm @@ -65,8 +89,20 @@ jobs: # Debian and Raspberry Pi OS; the test renders and checks it. - name: shellcheck run: | - command -v shellcheck >/dev/null || { apt-get update -qq && apt-get install -y -qq shellcheck; } + # Static release binary instead of apt-get update + install on every run. + curl -fsSL "https://github.com/koalaman/shellcheck/releases/download/${SHELLCHECK_VERSION}/shellcheck-${SHELLCHECK_VERSION}.linux.x86_64.tar.xz" \ + | tar -xJ -C /usr/local/bin --strip-components=1 "shellcheck-${SHELLCHECK_VERSION}/shellcheck" go test -count=1 -run 'TestScript|TestInstall' -v ./deploy/agent/ | grep -v '^=== RUN' + - name: Save caches + if: github.ref == 'refs/heads/main' + uses: actions/cache/save@v4 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/.cache/golangci-lint + ~/.npm + key: lint-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}-${{ github.sha }} test: runs-on: ubuntu-latest @@ -126,8 +162,20 @@ jobs: curl -fsSL "https://go.dev/dl/go${v}.linux-amd64.tar.gz" | tar -C /usr/local -xz echo /usr/local/go/bin >> "$GITHUB_PATH" echo "$HOME/go/bin" >> "$GITHUB_PATH" + echo "GO_VERSION=$v" >> "$GITHUB_ENV" + - name: Restore caches + uses: actions/cache/restore@v4 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/.npm + key: test-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}-${{ github.sha }} + restore-keys: | + test-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}- + test-go${{ env.GO_VERSION }}- - name: Build frontend - run: npm ci --no-audit --no-fund && npm run build + run: npm ci --no-audit --no-fund --prefer-offline && npm run build - name: Prepare database # The same roles and databases as the local environment: the # application role must not be a superuser, or row-level security @@ -136,6 +184,15 @@ jobs: - name: Test # Integration tests share one database, so packages run one at a time. run: go test -p 1 -count=1 ./... + - name: Save caches + if: github.ref == 'refs/heads/main' + uses: actions/cache/save@v4 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/.npm + key: test-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}-${{ github.sha }} build: # Nothing is built or pushed from code that fails lint or tests; the @@ -155,8 +212,20 @@ jobs: curl -fsSL "https://go.dev/dl/go${v}.linux-amd64.tar.gz" | tar -C /usr/local -xz echo /usr/local/go/bin >> "$GITHUB_PATH" echo "$HOME/go/bin" >> "$GITHUB_PATH" + echo "GO_VERSION=$v" >> "$GITHUB_ENV" + - name: Restore caches + uses: actions/cache/restore@v4 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/.npm + key: build-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}-${{ github.sha }} + restore-keys: | + build-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}- + build-go${{ env.GO_VERSION }}- - name: Build frontend - run: npm ci --no-audit --no-fund && npm run build + run: npm ci --no-audit --no-fund --prefer-offline && npm run build - name: Build binaries id: binaries run: | @@ -199,38 +268,59 @@ jobs: ./image -name statuspage -repo "$host/$owner/uptimesebi-statuspage" -port 8082/tcp \ -env UPTIME_STATUSPAGE_CACHE_DIR=/var/lib/uptimesebi $variant $flags done + - name: Save caches + if: github.ref == 'refs/heads/main' + uses: actions/cache/save@v4 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/.npm + key: build-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}-${{ github.sha }} + # The image job packs the amd64 binaries instead of compiling everything again. + - name: Hand the binaries to the image job + uses: actions/upload-artifact@v4 + with: + name: image-context + path: | + Dockerfile.ci + dist/linux_amd64/ + retention-days: 1 image: - # Kaniko runs rootless from docker:cli, the same way as the other - # repositories on this instance: the default job image has no docker - # CLI, and Kaniko's own image lacks what the runner needs to keep a job - # container alive. Images are pushed only from main and tags, and only - # once the PACKAGE_TOKEN secret (write:package) exists. + # Builds Dockerfile.ci (the build job's amd64 binaries on distroless) + # with the runner's Docker daemon instead of Kaniko rebuilding frontend + # and Go from the git context. Images are pushed only from main and tags, + # and only once the PACKAGE_TOKEN secret (write:package) exists. # The build job's version, so the API reports the release its agent # images are tagged with. needs: build runs-on: ubuntu-latest + # Node for the artifact action; the Docker CLI is added below. container: - image: docker:cli@sha256:34507eb5069d8be82c95dd3b6987d4250fd5fee49b82e2509b7d7a7030740d6d + image: node:24-alpine defaults: run: shell: sh steps: + - uses: actions/download-artifact@v4 + with: + name: image-context + path: ctx - name: Build (and push from main and tags) env: - GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} PACKAGE_TOKEN: ${{ secrets.PACKAGE_TOKEN }} VERSION: ${{ needs.build.outputs.version }} run: | set -eu + apk add --no-cache docker-cli >/dev/null REGISTRY="${GITHUB_SERVER_URL#*://}" IMAGE="$(printf '%s/%s' "$REGISTRY" "$GITHUB_REPOSITORY" | tr 'A-Z' 'a-z')" - SHORT="$(printf '%s' "$GITHUB_SHA" | cut -c1-12)" TAG="$VERSION" - PUSH="--no-push" - DEST="--destination ${IMAGE}:${TAG#v}" + TAGS="${IMAGE}:${TAG#v}" # Channels: edge follows main, beta the newest prerelease, latest # and stable the newest release, X.Y its newest patch. + CHANNELS="" case "$GITHUB_REF" in refs/heads/main) CHANNELS="edge" ;; refs/tags/v*-beta.*) CHANNELS="beta" ;; @@ -238,33 +328,17 @@ jobs: MINOR="$(printf '%s' "${TAG#v}" | cut -d. -f1-2)" CHANNELS="latest stable $MINOR" ;; esac + for c in $CHANNELS; do TAGS="$TAGS ${IMAGE}:$c"; done + set -- + for t in $TAGS; do set -- "$@" -t "$t"; done + docker build --platform linux/amd64 -f ctx/Dockerfile.ci "$@" ctx case "$GITHUB_REF" in refs/heads/main|refs/tags/v*) - if [ -n "${PACKAGE_TOKEN:-}" ]; then - PUSH="" - for c in $CHANNELS; do DEST="$DEST --destination ${IMAGE}:$c"; done - else - echo "PACKAGE_TOKEN is not set; building without pushing." - fi ;; + if [ -z "${PACKAGE_TOKEN:-}" ]; then + echo "PACKAGE_TOKEN is not set; built without pushing." + exit 0 + fi + printf '%s' "$PACKAGE_TOKEN" | docker login "$REGISTRY" -u "$GITHUB_ACTOR" --password-stdin + for t in $TAGS; do docker push "$t"; done ;; + *) echo "Pull request: built without pushing." ;; esac - case "$GITHUB_SERVER_URL" in https://*) PULL=https ;; *) PULL=http ;; esac - # Pull requests are built from their head, which Gitea exposes as - # refs/pull//head. - REF="$GITHUB_REF" - case "$REF" in refs/pull/*/merge) REF="${REF%/merge}/head" ;; esac - AUTH="$(printf '%s:%s' "$GITHUB_ACTOR" "${PACKAGE_TOKEN:-none}" | base64 | tr -d '\n')" - CFG="$(printf '{"auths":{"%s":{"auth":"%s"}}}' "$REGISTRY" "$AUTH")" - docker run --rm \ - -e GIT_PULL_METHOD="$PULL" \ - -e GIT_USERNAME="x-access-token" \ - -e GIT_PASSWORD="$GITEA_TOKEN" \ - -e CFG="$CFG" \ - --entrypoint /busybox/sh \ - gcr.io/kaniko-project/executor:v1.23.2-debug \ - -c 'mkdir -p /kaniko/.docker && printf %s "$CFG" > /kaniko/.docker/config.json && exec /kaniko/executor "$@"' _ \ - --context "git://${REGISTRY}/${GITHUB_REPOSITORY}.git#${REF}" \ - --dockerfile Dockerfile \ - --build-arg VERSION="$TAG" \ - --build-arg COMMIT="$SHORT" \ - $DEST $PUSH \ - --compressed-caching=false diff --git a/.gitea/workflows/release.yaml b/.gitea/workflows/release.yaml index c8270fb..99adb50 100644 --- a/.gitea/workflows/release.yaml +++ b/.gitea/workflows/release.yaml @@ -20,6 +20,19 @@ jobs: v="$(awk '/^go /{print $2}' go.mod)" curl -fsSL "https://go.dev/dl/go${v}.linux-amd64.tar.gz" | tar -C /usr/local -xz echo /usr/local/go/bin >> "$GITHUB_PATH" + echo "GO_VERSION=$v" >> "$GITHUB_ENV" + # Read-only: the ci workflow's build job saves these caches on main. + - name: Restore caches + uses: actions/cache/restore@v4 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/.npm + key: build-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}-${{ github.sha }} + restore-keys: | + build-go${{ env.GO_VERSION }}-${{ hashFiles('go.sum', 'package-lock.json') }}- + build-go${{ env.GO_VERSION }}- - name: Check tag run: | go run ./tools/release check "$GITHUB_REF_NAME" @@ -32,7 +45,7 @@ jobs: exit 1 fi - name: Build frontend - run: npm ci --no-audit --no-fund && npm run build + run: npm ci --no-audit --no-fund --prefer-offline && npm run build - name: Package Helm chart run: | curl -fsSL "https://get.helm.sh/helm-v3.19.0-linux-amd64.tar.gz" | tar -xz -C /usr/local/bin --strip-components=1 linux-amd64/helm diff --git a/Dockerfile.ci b/Dockerfile.ci new file mode 100644 index 0000000..4f77e37 --- /dev/null +++ b/Dockerfile.ci @@ -0,0 +1,7 @@ +# CI image: packs the binaries the build job already compiled (dist/linux_amd64) +# instead of building frontend and Go a second time. `make image` and local +# builds keep using Dockerfile, which builds everything from source. +FROM gcr.io/distroless/static-debian12:nonroot@sha256:afa5c872c891853ca7fcf1f12c3edb23f7eeef36189728842dd51042ff57f7ab +COPY dist/linux_amd64/ /usr/local/bin/ +USER nonroot +ENTRYPOINT ["/usr/local/bin/api"]