From e2e21e84f78e936152c7c612b1d0a7fa91311e2c Mon Sep 17 00:00:00 2001 From: Gabriel20xx Date: Thu, 6 Aug 2026 09:44:54 +0200 Subject: [PATCH] Publish versioned shared Go module --- .gitea/workflows/publish-go.yml | 53 +++++++++++++++++++++++++++++++ README.md | 32 ++++++++++++------- backend/README.md | 2 +- backend/go.mod | 2 +- backend/postgres/postgres.go | 2 +- package.json | 2 +- packages/python-backend/README.md | 2 +- scripts/package-go-module.sh | 46 +++++++++++++++++++++++++++ 8 files changed, 125 insertions(+), 16 deletions(-) create mode 100644 .gitea/workflows/publish-go.yml create mode 100644 scripts/package-go-module.sh diff --git a/.gitea/workflows/publish-go.yml b/.gitea/workflows/publish-go.yml new file mode 100644 index 0000000..d3155d8 --- /dev/null +++ b/.gitea/workflows/publish-go.yml @@ -0,0 +1,53 @@ +name: Publish Go package + +on: + push: + tags: + - 'backend/v*' + +jobs: + publish: + name: Publish shared Go module + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout source + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: backend/go.mod + cache: false + + - name: Test Go module + working-directory: backend + run: go test ./... + + - name: Install ZIP tools + run: sudo apt-get update && sudo apt-get install --no-install-recommends -y zip + + - name: Package Go module + env: + RELEASE_TAG: ${{ github.ref_name }} + run: bash scripts/package-go-module.sh "$RELEASE_TAG" "$RUNNER_TEMP/gitea-go-package" + + - name: Publish to Gitea Package Registry + env: + PACKAGE_REGISTRY_USERNAME: ${{ secrets.PACKAGE_REGISTRY_USERNAME }} + PACKAGE_REGISTRY_TOKEN: ${{ secrets.PACKAGE_REGISTRY_TOKEN }} + GITEA_GO_REGISTRY: https://repos.gfranz.ch/api/packages/Gabriel20xx/go/upload + run: | + set -euo pipefail + if [ -z "$PACKAGE_REGISTRY_USERNAME" ] || [ -z "$PACKAGE_REGISTRY_TOKEN" ]; then + echo 'PACKAGE_REGISTRY_USERNAME and PACKAGE_REGISTRY_TOKEN secrets are required.' >&2 + exit 1 + fi + package_file="$(find "$RUNNER_TEMP/gitea-go-package" -maxdepth 1 -type f -name '*.zip' -print -quit)" + test -n "$package_file" + curl --fail-with-body --user "$PACKAGE_REGISTRY_USERNAME:$PACKAGE_REGISTRY_TOKEN" \ + --upload-file "$package_file" \ + "$GITEA_GO_REGISTRY" diff --git a/README.md b/README.md index f78c399..8362a12 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The first packages are: - `@gabriel20xx/design-tokens` — colors, typography, spacing, radii, shadows, and light/dark theme variables. - `@gabriel20xx/web-ui` — Svelte components for the application shell, header/navigation, authentication, dialogs, profile/settings/about windows, buttons, cards, badges, and page headers. -- `repos.gfranz.ch/Gabriel20xx/SharedCode/backend` — Go helpers for environment configuration, `pgx` PostgreSQL pools, transactions, common auth persistence/passwords/tokens, and embedded Goose migrations. +- `repos.gfranz.ch/Gabriel20xx/shared/backend` — Go helpers for environment configuration, `pgx` PostgreSQL pools, transactions, common auth persistence/passwords/tokens, and embedded Goose migrations. - `@gabriel20xx/node-backend` — Node.js `pg` pool/configuration, transactions, migrations, and auth/session persistence. - `gabriel20xx-shared-backend` — Python `asyncpg` pool/configuration and auth/session persistence. @@ -25,7 +25,7 @@ npm run test:node-backend `npm run build` creates the distributable package in `packages/web-ui/dist`. The generated `dist` directory is intentionally ignored by the package configuration and should not be committed. -Validate the Go backend package from `SharedCode/backend`: +Validate the Go backend package from `shared/backend`: ```bash cd backend @@ -39,7 +39,7 @@ The repository is the canonical source and is installable directly from Gitea. T builds the Svelte package during installation and exposes the runtime packages as subpaths: ```bash -npm install --save-exact git+https://repos.gfranz.ch/Gabriel20xx/SharedCode# +npm install --save-exact git+https://repos.gfranz.ch/Gabriel20xx/shared# ``` Use the shared UI from the root package: @@ -96,10 +96,20 @@ The stylesheet imports the design tokens and uses an `sc-` prefix, so it does no ## Use the shared Go backend package -From a Go backend directory, add the module (the module is a subdirectory of the SharedCode repository): +The Go module is published to the private Gitea Go Package Registry. Configure a +read-only package token in the consuming workflow as `PACKAGE_REGISTRY_USERNAME` +and `PACKAGE_REGISTRY_TOKEN`, then configure Go to use the registry before running +`go get`, `go test`, or `go build`: ```bash -go get repos.gfranz.ch/Gabriel20xx/SharedCode/backend@backend/v0.1.0 +export GOPROXY=https://repos.gfranz.ch/api/packages/Gabriel20xx/go,https://proxy.golang.org +export GONOSUMDB=repos.gfranz.ch/Gabriel20xx/shared/* +``` + +From a Go backend directory, add the released module (the module is a subdirectory of the shared repository): + +```bash +go get repos.gfranz.ch/Gabriel20xx/shared/backend@v0.1.0 ``` Then initialize the pool and shared auth store at startup: @@ -109,8 +119,8 @@ import ( "context" "log" - "repos.gfranz.ch/Gabriel20xx/SharedCode/backend/auth" - "repos.gfranz.ch/Gabriel20xx/SharedCode/backend/postgres" + "repos.gfranz.ch/Gabriel20xx/shared/backend/auth" + "repos.gfranz.ch/Gabriel20xx/shared/backend/postgres" ) cfg, err := postgres.FromEnv() @@ -133,7 +143,7 @@ Use `auth.SchemaOptions{SingleUser: false, APITokens: true}` for a multi-user se For embedded SQL migrations, use `migrate.Up(ctx, databaseURL, embeddedMigrations, "migrations")`. The package does not contain application tables, so migrations remain versioned with the service that owns those tables. -The Go module lives under `backend`, so its release tag must use the submodule prefix (`backend/v0.1.0`). If the SharedCode code is moved into its own repository, update the module path and use ordinary `v0.1.0` tags instead. +The Go module lives under `backend`, so its release tag must use the submodule prefix (`backend/v0.1.0`). The consuming `go.mod` uses the normal module version (`v0.1.0`). To publish a release, push a tag such as `backend/v0.1.0`; the Gitea workflow packages the module and uploads it to the private registry. Never reuse an existing package version. For Node services, install the shared package after publishing it: @@ -237,14 +247,14 @@ The shared reset controls request a short-lived code printed by the server-side Before publishing, build the packages and install them from the local workspace in a frontend project: ```bash -# From Web/SharedCode +# From Web/shared npm run build # From a project frontend directory; do not commit this file dependency for CI -npm install ../../../SharedCode/packages/design-tokens ../../../SharedCode/packages/web-ui +npm install ../../../shared/packages/design-tokens ../../../shared/packages/web-ui ``` -For CI and normal development, use the published version instead of a `file:` dependency. A local file dependency only works when the checkout also contains the sibling `SharedCode` directory. +For CI and normal development, use the published version instead of a `file:` dependency. A local file dependency only works when the checkout also contains the sibling `shared` directory. ## Updating the design system diff --git a/backend/README.md b/backend/README.md index 7065c5a..7d4f5d5 100644 --- a/backend/README.md +++ b/backend/README.md @@ -9,7 +9,7 @@ services, not application-specific SQL or HTTP handlers. - `migrate` — embedded filesystem adapter for Goose migrations. Run `go test ./...` and `go vet ./...` from this directory. The module path is -`repos.gfranz.ch/Gabriel20xx/SharedCode/backend`; change it if the SharedCode folder is +`repos.gfranz.ch/Gabriel20xx/shared/backend`; change it if the shared folder is hosted under a different repository path. Application-specific table definitions and HTTP behavior stay in the service. diff --git a/backend/go.mod b/backend/go.mod index 17ac281..7510493 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,4 +1,4 @@ -module repos.gfranz.ch/Gabriel20xx/SharedCode/backend +module repos.gfranz.ch/Gabriel20xx/shared/backend go 1.25.7 diff --git a/backend/postgres/postgres.go b/backend/postgres/postgres.go index f1bc0ee..6d0c379 100644 --- a/backend/postgres/postgres.go +++ b/backend/postgres/postgres.go @@ -13,7 +13,7 @@ import ( "github.com/jackc/pgx/v5/pgconn" "github.com/jackc/pgx/v5/pgxpool" - "repos.gfranz.ch/Gabriel20xx/SharedCode/backend/config" + "repos.gfranz.ch/Gabriel20xx/shared/backend/config" ) // Config controls a shared pgx connection pool. URL is normally read from diff --git a/package.json b/package.json index d69289c..ed180f7 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "license": "MIT", "repository": { "type": "git", - "url": "https://repos.gfranz.ch/Gabriel20xx/SharedCode" + "url": "https://repos.gfranz.ch/Gabriel20xx/shared" }, "type": "module", "files": [ diff --git a/packages/python-backend/README.md b/packages/python-backend/README.md index a2fbba7..855b2f5 100644 --- a/packages/python-backend/README.md +++ b/packages/python-backend/README.md @@ -13,5 +13,5 @@ published index or directly from the SharedCode repository with a subdirectory install during development: ```bash -pip install "git+https://repos.gfranz.ch/Gabriel20xx/SharedCode@v0.1.0#subdirectory=packages/python-backend" +pip install "git+https://repos.gfranz.ch/Gabriel20xx/shared@v0.1.0#subdirectory=packages/python-backend" ``` diff --git a/scripts/package-go-module.sh b/scripts/package-go-module.sh new file mode 100644 index 0000000..810769d --- /dev/null +++ b/scripts/package-go-module.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -euo pipefail + +readonly module_path='repos.gfranz.ch/Gabriel20xx/shared/backend' +readonly module_directory='backend' + +release_tag="${1:?usage: package-go-module.sh backend/vX.Y.Z [output-directory]}" +output_directory="${2:-dist}" +version="${release_tag#backend/}" + +if [[ "$release_tag" != backend/v* ]] || [[ ! "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.-]+)?$ ]]; then + echo "Expected a release tag such as backend/v0.1.0; got: $release_tag" >&2 + exit 1 +fi + +repository_root="$(git rev-parse --show-toplevel)" +git rev-parse --verify --quiet "${release_tag}^{commit}" >/dev/null || { + echo "Release tag does not resolve to a commit: $release_tag" >&2 + exit 1 +} + +stage_directory="$(mktemp -d)" +trap 'rm -rf "$stage_directory"' EXIT + +module_root="$stage_directory/${module_path}@${version}" +mkdir -p "$module_root" + +# Go module ZIP files must contain @/ at their root. The +# module is stored in the backend/ subdirectory of the shared repository, +# so strip that repository directory while extracting the tagged source. +git archive --format=tar "$release_tag" "$module_directory" \ + | tar -x --strip-components=1 -C "$module_root" + +test -f "$module_root/go.mod" +grep -Fxq "module $module_path" "$module_root/go.mod" +mkdir -p "$repository_root/$output_directory" +zip_file="$repository_root/$output_directory/${module_path//\//_}@${version}.zip" +rm -f "$zip_file" + +( + cd "$stage_directory" + zip -qr "$zip_file" "${module_path}@${version}" +) + +unzip -tq "$zip_file" >/dev/null +echo "Created $zip_file"