Files
template-go-web/.gitea/workflows/cd.yml
T
mathiasandClaude Opus 5 b40db025cc fix(template): ship a go.sum, and stop the templ generator drifting from the library
Closes #8.

`task check` did not pass on a clean checkout of this template, and had not
for some time. Three defects in a chain, each hiding the next:

1. No go.sum at all. `go build ./...` failed on missing sum entries, so
   nothing downstream of it ever ran.

2. With that fixed, the real skew appeared: CI installs the templ GENERATOR
   as @latest while go.mod pinned the templ LIBRARY at v0.2.778. The v0.3
   generator emits calls to templruntime.WriteString, which v0.2.778 does not
   have -- so generated code could not compile against the pinned runtime.
   Library moved to v0.3.1020 and the CI install now derives its version from
   go.mod, so generator and library cannot drift apart again.

3. .gitignore carried `*.templ.go`. templ writes `<name>_templ.go` -- an
   underscore, not a dot -- so the pattern matched nothing. Generated files
   were kept out of git by luck rather than by the ignore rule.

None of this was visible from this repo's CI, which has been green
throughout, because the guard job skips `check` here entirely (#12). A
template whose gate cannot run is a template whose defects are found by its
children.

Verified on a clean checkout: `go build ./...` clean, `task check` exit 0.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 18:04:13 +02:00

164 lines
6.7 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: CD
on:
push:
branches: [main]
tags: ["v*"]
pull_request:
branches: [main]
env:
IMAGE: __PROJECT_NAME__
jobs:
guard:
name: Detect unsubstituted template
runs-on: self-hosted
outputs:
is_template: ${{ steps.detect.outputs.is_template }}
steps:
- uses: actions/checkout@v4
- id: detect
# The detection lives in scripts/is-template.sh so it can be TESTED.
# It was inline here twice and wrong both times (#9, #10): the grep
# named a placeholder literally, and the scaffolder substitutes every
# file including this one, so in a generated repo the guard matched
# forever and check/build/deploy all skipped while the run went green.
#
# An inline shell block in a workflow cannot be run by a test, which is
# why the second fix repeated the first bug under a comment warning
# about it. scripts/tests/is-template_test.sh now substitutes the guard
# and asserts it still answers correctly.
run: |
echo "is_template=$(bash scripts/is-template.sh go.mod)" >> "$GITHUB_OUTPUT"
check:
name: Lint / Test / Vet
needs: guard
if: needs.guard.outputs.is_template != 'true'
runs-on: self-hosted
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: false
- name: Install toolchain
run: |
go version
# Pin the GENERATOR to the same version as the templ LIBRARY in
# go.mod. Installing @latest let them drift: the generator emitted
# calls (templruntime.WriteString) that the pinned library did not
# have, which is template-go-web#8. Nothing caught it because the
# guard skipped `check` in this repo entirely (#12).
templ_version="$(go list -m -f '{{.Version}}' github.com/a-h/templ)"
echo "templ library is $templ_version — installing the matching generator"
go install "github.com/a-h/templ/cmd/templ@$templ_version"
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh \
| sh -s -- -b "$(go env GOPATH)/bin" v2.11.4
- name: Run checks
run: task check
build:
name: Build & Import
needs: [guard, check]
if: needs.guard.outputs.is_template != 'true' && github.event_name != 'pull_request'
runs-on: self-hosted
outputs:
image-tag: ${{ steps.meta.outputs.sha-tag }}
steps:
- uses: actions/checkout@v4
- name: Derive image tags
id: meta
run: |
SHA=$(git rev-parse --short HEAD)
echo "sha-tag=${SHA}" >> "$GITHUB_OUTPUT"
# infra#309: the implementation lives in .gitea/build-image.sh, a
# byte-identical copy of mathias/infra's ci-templates/build-image.sh.
# Do not edit it here -- edit the canonical file, run `task ci:sync`,
# and infra's drift gate will name any repo still carrying an old copy.
#
# This matters more in a template than anywhere else: every repo
# generated from here inherits its CI, so a template that carries the
# build LOGIC manufactures a new copy of every bug in it. That is
# precisely how rootless buildah (infra#241) reached a dozen repos, and
# why the fix then had to be rediscovered here weeks later. A template
# that carries a CALL manufactures nothing.
#
# NEW REPOS: add yourself to mathias/infra's ci-templates/adopters.txt so
# the gate keeps this copy current. Until you do, the copy is unmanaged
# and will silently fall behind.
#
# `bash <script>` rather than executing it directly: files written via
# the Gitea API land 0644, and CI that depends on a file mode is a trap.
- name: Build and push to local registry
env:
SHA_TAG: ${{ steps.meta.outputs.sha-tag }}
GIT_SHA: ${{ github.sha }}
run: bash .gitea/build-image.sh
deploy:
name: Deploy via GitOps
needs: [guard, build]
if: needs.guard.outputs.is_template != 'true' && github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: self-hosted
steps:
- name: Update image tag in infra repo
env:
IMAGE_TAG: ${{ needs.build.outputs.image-tag }}
DEPLOY_KEY: ${{ secrets.INFRA_DEPLOY_KEY }}
run: |
set -euo pipefail
# infra#311: fail on an empty tag BEFORE patching anything. The
# grep verification further down CANNOT catch it -- with IMAGE_TAG
# empty its pattern matches the very corruption it exists to reject.
# The result is `image: <repo>:` in the manifest, which is invalid
# YAML, so Flux fails the ENTIRE apps Kustomization and every app
# stops reconciling, not just this one. That happened on 2026-08-25.
[ -n "${IMAGE_TAG:-}" ] || { echo "IMAGE_TAG is empty - refusing to patch"; exit 1; }
mkdir -p ~/.ssh
echo "$DEPLOY_KEY" > ~/.ssh/id_infra
chmod 600 ~/.ssh/id_infra
ssh-keyscan -p 30022 10.0.1.20 >> ~/.ssh/known_hosts 2>/dev/null
export GIT_SSH_COMMAND="ssh -i ~/.ssh/id_infra -o IdentitiesOnly=yes"
rm -rf /tmp/infra
git clone -b main ssh://git@10.0.1.20:30022/mathias/infra.git /tmp/infra
cd /tmp/infra
DEPLOYMENT="k3s/apps/__PROJECT_NAME__/deployment.yaml"
sed -i "s|image: localhost:5000/__PROJECT_NAME__:.*|image: localhost:5000/__PROJECT_NAME__:${IMAGE_TAG}|" "$DEPLOYMENT"
grep -q "localhost:5000/__PROJECT_NAME__:${IMAGE_TAG}" "$DEPLOYMENT" \
|| { echo "✗ image tag patch failed"; exit 1; }
if git diff --quiet "$DEPLOYMENT"; then
echo "ℹ image tag unchanged — skipping push"
else
git -c user.name="__PROJECT_NAME__ CI" \
-c user.email="ci@__PROJECT_NAME__.local" \
commit -m "chore(deploy): __PROJECT_NAME__ → ${IMAGE_TAG}" "$DEPLOYMENT"
git push origin main
echo "✓ pushed to infra repo"
fi
shred -u ~/.ssh/id_infra
- name: Trigger Flux reconcile
run: |
kubectl -n flux-system annotate gitrepository flux-system \
reconcile.fluxcd.io/requestedAt="$(date +%s)" --overwrite
kubectl -n flux-system annotate kustomization apps \
reconcile.fluxcd.io/requestedAt="$(date +%s)" --overwrite
- name: Verify rollout
run: |
kubectl rollout status deployment/__PROJECT_NAME__ \
--namespace __PROJECT_NAME__ \
--timeout=120s \
|| {
kubectl get pods -n __PROJECT_NAME__ -o wide
kubectl get events -n __PROJECT_NAME__ --sort-by='.lastTimestamp' | tail -20
exit 1
}