diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..eac1fae --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,23 @@ +name: ci + +on: + pull_request: + push: + branches: [main] + +jobs: + check: + name: build and test + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: false + + - name: Run checks + run: ./scripts/check.sh diff --git a/docs/workflow.md b/docs/workflow.md index 90b4c25..d1582fe 100644 --- a/docs/workflow.md +++ b/docs/workflow.md @@ -18,6 +18,32 @@ It works with any project type and supports monorepos. --- +## Continuous Integration + +Every pull request and every push to `main` runs the `ci` workflow in +[`.gitea/workflows/ci.yml`](../.gitea/workflows/ci.yml). Its single `check` job +runs [`scripts/check.sh`](../scripts/check.sh), which: + +1. fails if any Go file is not `gofmt`-formatted, +2. runs `go mod verify` to confirm the dependency modules match `go.sum`, +3. runs `go vet ./...` and `go build ./...`, +4. runs `go test -cover ./...`. + +Run the same checks locally with: + +```sh +./scripts/check.sh +``` + +The job fails when any check fails, so a pull request cannot land with broken +code or tests. + +Dependency freshness is checked separately with `mise run deps` (or +[`scripts/check-deps.sh`](../scripts/check-deps.sh)), which fails when a module +pinned in `go.mod` has a newer release available. + +--- + ## Two Release Workflows ### Direct release @@ -483,4 +509,4 @@ Tag format: `@v` for monorepos (e.g. `my-app@v1.3.0`), or `v module graph is loadable' +go list -m -e all >/dev/null + +echo '==> modules are up to date' +updates="$(go list -m -u -e -f '{{.Path}} {{.Version}} {{if .Update}}{{.Update.Version}}{{end}}' all | awk 'NF == 3')" +if [ -n "$updates" ]; then + echo 'These modules have newer versions available:' >&2 + printf '%s\n' "$updates" >&2 + exit 1 +fi + +if [ "$(go env GOPROXY)" != "off" ]; then + exit 0 +fi + +# The module proxy is disabled, so `go list -m -u` cannot report updates. +# Compare the pins in go.mod with the versions in the local module cache. +cache="$(go env GOMODCACHE)/cache/download" + +# escape_path applies the case encoding used by the module cache for paths that +# contain upper-case letters (for example BurntSushi becomes !burnt!sushi). +escape_path() { + local path="$1" out='' char i + for ((i = 0; i < ${#path}; i++)); do + char="${path:i:1}" + if [[ $char == [A-Z] ]]; then + out+="!${char,,}" + else + out+="$char" + fi + done + printf '%s' "$out" +} + +checked=0 +stale='' +while read -r path version; do + list="$cache/$(escape_path "$path")/@v/list" + if [ ! -f "$list" ]; then + continue + fi + checked=$((checked + 1)) + latest="$(sort -V "$list" | tail -n 1)" + if [ "$latest" != "$version" ]; then + stale+="$path $version (newest in cache: $latest)"$'\n' + fi +done < <(go mod edit -json | awk -F'"' '/"Path":/ { path = $4 } /"Version":/ { print path, $4 }') + +if [ -n "$stale" ]; then + echo 'These go.mod pins are not the newest cached versions:' >&2 + printf '%s' "$stale" >&2 + exit 1 +fi + +if [ "$checked" -eq 0 ]; then + echo 'note: proxy disabled and module cache is empty; freshness not checked' >&2 +else + echo "all $checked go.mod pins match the newest versions in the module cache" +fi diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100755 index 0000000..73569a9 --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# +# check.sh runs every verification step required before a change can land. +# `.gitea/workflows/ci.yml` invokes it on every pull request and every push to +# main, so this script is the single source of truth for what "CI passed" means. + +set -euo pipefail + +cd "$(dirname "${BASH_SOURCE[0]}")/.." + +echo '==> gofmt' +unformatted="$(gofmt -l .)" +if [ -n "$unformatted" ]; then + echo 'These files are not gofmt-formatted:' >&2 + printf '%s\n' "$unformatted" >&2 + exit 1 +fi + +echo '==> go mod verify' +go mod verify + +echo '==> go vet' +go vet ./... + +echo '==> go build' +go build ./... + +echo '==> go test' +go test -cover ./...