diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 40a2c15..34ee03f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,7 +1,9 @@ # The release: on a v* tag (pushed by Teg - a tag publishes), the tests, # then GoReleaser (.goreleaser.yaml) builds the binaries and archives and -# drafts the GitHub release, then the release is published: as a -# pre-release while urbex is 0.x or the tag has a suffix (v0.1.0-rc.1). +# drafts the GitHub release; the operator image is built from those +# binaries and pushed to ghcr.io/urbforge/urbex-op, urbex-op is attached +# to the release, and the release is published: as a pre-release while +# urbex is 0.x or the tag has a suffix (v0.1.0-rc.1). name: release on: @@ -36,6 +38,7 @@ jobs: timeout-minutes: 30 permissions: contents: write # the release and its assets + packages: write # the operator image on ghcr.io id-token: write # build provenance attestations: write steps: @@ -54,6 +57,57 @@ jobs: uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 with: subject-path: dist/*.tar.gz + - name: The image's context, and its tags + id: image + env: + TAG: ${{ github.ref_name }} + run: | + # //urbex, as the Containerfile's release stage reads it. + jq -r '.[] | select(.type == "Binary" and .goos == "linux") | "\(.goarch) \(.path)"' dist/artifacts.json | + while read -r arch path; do + mkdir -p "dist/image/linux/$arch" + cp "$path" "dist/image/linux/$arch/urbex" + done + cp mise.toml dist/image/ + version=${TAG#v} + tags="ghcr.io/urbforge/urbex-op:$version" + case "$TAG" in *-*) ;; *) tags="$tags,ghcr.io/urbforge/urbex-op:latest" ;; esac + echo "version=$version" >>"$GITHUB_OUTPUT" + echo "tags=$tags" >>"$GITHUB_OUTPUT" + echo "mise=$(sed -n 's/^min_version = "\(.*\)"$/\1/p' mise.toml)" >>"$GITHUB_OUTPUT" + - uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4.4.0 + - uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + - uses: docker/login-action@81c6f93d4bc991e510b342a00b419b3bb4efc5c6 # v4.7.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push the operator image + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: dist/image + file: tools/operator/Containerfile + target: release + platforms: linux/amd64,linux/arm64 + build-args: MISE_VERSION=${{ steps.image.outputs.mise }} + tags: ${{ steps.image.outputs.tags }} + labels: | + org.opencontainers.image.source=https://github.com/${{ github.repository }} + org.opencontainers.image.version=${{ steps.image.outputs.version }} + org.opencontainers.image.description=urbex and the tools it runs, for urbex-op + org.opencontainers.image.licenses=Apache-2.0 + push: true + provenance: mode=max + - name: Attach urbex-op, set to this release's image + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + TAG: ${{ github.ref_name }} + VERSION: ${{ steps.image.outputs.version }} + run: | + sed "s/^URBEX_OP_VERSION=dev\$/URBEX_OP_VERSION=$VERSION/" tools/operator/urbex-op >dist/urbex-op + grep -qx "URBEX_OP_VERSION=$VERSION" dist/urbex-op + chmod +x dist/urbex-op + gh release upload "$TAG" dist/urbex-op --repo "$GITHUB_REPOSITORY" --clobber - name: Publish the release env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index 3c0ea7c..f635554 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,11 @@ credentials, the keys, the GitOps repo and your projects. Only podman or docker is needed, on Linux or on macOS (docker with colima, or podman machine). See [`tools/operator/README.md`](tools/operator/README.md). +Each release attaches `urbex-op` and publishes its image, +`ghcr.io/urbforge/urbex-op:`, which `urbex-op` pulls the first +time: download the one file and run it. From a checkout, build the image +first: + ```sh tools/operator/urbex-op build && tools/operator/urbex-op -w ~/urbex-work init export URBEX_PROXMOX_TOKEN=... diff --git a/tools/operator/Containerfile b/tools/operator/Containerfile index 671a407..83b279f 100644 --- a/tools/operator/Containerfile +++ b/tools/operator/Containerfile @@ -1,17 +1,24 @@ -# The operator image: urbex, built from this checkout, and everything it -# shells out to - terraform, ansible-playbook, sops, age, git, ssh - at the -# versions pinned in mise.toml. Build it with `tools/operator/urbex-op -# build` (or `mise run operator-image`), which passes mise.toml's Go and -# mise versions as GO_VERSION and MISE_VERSION. -ARG GO_VERSION +# The operator image: urbex and everything it shells out to - terraform, +# ansible-playbook, sops, age, git, ssh - at the versions pinned in +# mise.toml. Two ways to build it, both passing mise.toml's Go and mise +# versions as GO_VERSION and MISE_VERSION: +# +# - from this checkout (the last stage, the default): `tools/operator/urbex-op +# build`, or `mise run operator-image`; +# - from a release's binaries (--target release): the release workflow, +# with a context holding mise.toml and //urbex for each +# platform - published as ghcr.io/urbforge/urbex-op. +ARG GO_VERSION=1 FROM docker.io/library/golang:${GO_VERSION} AS build WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 go build -o /out/urbex ./cmd/urbex +# The commit urbex version reports: the context has no .git for Go to read. +ARG URBEX_COMMIT= +RUN CGO_ENABLED=0 go build -ldflags "-X github.com/urbforge/urbex-cli/internal/version.Commit=${URBEX_COMMIT}" -o /out/urbex ./cmd/urbex -FROM docker.io/library/debian:13-slim +FROM docker.io/library/debian:13-slim AS tools ARG MISE_VERSION # The tools, from mise.toml, linked into /usr/local/bin; mise itself and # uv are only needed to install them, and are removed in the same layer. @@ -36,4 +43,12 @@ RUN apt-get update \ && chmod +x /usr/local/bin/urbex-python \ && rm -rf /tmp/mise /tmp/mise-cache "$MISE_DATA_DIR/installs/uv" "$MISE_DATA_DIR/shims" WORKDIR / + +# A release: the binary GoReleaser built for this platform. +FROM tools AS release +ARG TARGETPLATFORM +COPY ${TARGETPLATFORM}/urbex /usr/local/bin/urbex + +# From this checkout. +FROM tools COPY --from=build /out/urbex /usr/local/bin/urbex diff --git a/tools/operator/README.md b/tools/operator/README.md index c4c24bc..3ef41a6 100644 --- a/tools/operator/README.md +++ b/tools/operator/README.md @@ -20,6 +20,21 @@ script expands its arrays the way macOS's bash 3.2 accepts. What it needs: ## Quick start +From a release, nothing to clone: download `urbex-op` from the +[release](https://github.com/urbforge/urbex-cli/releases) (one file). It +runs that release's image, `ghcr.io/urbforge/urbex-op:`, and +pulls it the first time: + +```sh +curl -fsSLo ~/bin/urbex-op https://github.com/urbforge/urbex-cli/releases/download/v0.1.0/urbex-op +chmod +x ~/bin/urbex-op +urbex-op version # urbex-op 0.1.0, image ghcr.io/urbforge/urbex-op:0.1.0 +urbex-op -w ~/urbex-work init # the workspace: SSH key, age key +``` + +From a checkout of this repository (to work on urbex), build the image +from it first: + ```sh tools/operator/urbex-op build # the image, from this checkout tools/operator/urbex-op -w ~/urbex-work init # the workspace: SSH key, age key @@ -115,9 +130,14 @@ See *Find every endpoint* in the cookbook | Variable | Default | Meaning | |---|---|---| | `URBEX_WORKSPACE` | `~/urbex-workspace` | The workspace, if `-w` isn't given. | -| `URBEX_OPERATOR_IMAGE` | `urbex-operator` | The image `build` creates and runs use. | +| `URBEX_OPERATOR_IMAGE` | a release's `urbex-op`: `ghcr.io/urbforge/urbex-op:`; from a checkout: `urbex-operator` | The image runs use (and `build` creates). | | `URBEX_PROXMOX_TOKEN`, `URBEX_CLOUDFLARE_TOKEN`, `URBEX_AGE_KEY` | - | Passed to the container when set; otherwise the credentials file is used. | -Rebuild the image (`urbex-op build`, or `mise run operator-image`) after -updating this checkout: the image contains the `urbex` binary built from +From a checkout, rebuild the image (`urbex-op build`, or `mise run +operator-image`) after updating it: the image contains the `urbex` binary built from it, and the tools at the versions `mise.toml` pins. + +A release builds the same Containerfile with `--target release`, from +the binaries GoReleaser made, for linux/amd64 and linux/arm64, and pushes +it to `ghcr.io/urbforge/urbex-op` (`:`, and `:latest` unless the +tag has a suffix such as `-rc.1`). diff --git a/tools/operator/urbex-op b/tools/operator/urbex-op index 05e529e..e6daa2a 100755 --- a/tools/operator/urbex-op +++ b/tools/operator/urbex-op @@ -3,6 +3,7 @@ # holds everything a platform needs on the operator's side. See README.md. # # urbex-op build build the operator image from this checkout +# urbex-op version this script's version and the image it runs # urbex-op [-w DIR] init create the workspace: keys, folders # urbex-op [-w DIR] set KEY store a credential in the workspace (asks for it) # urbex-op [-w DIR] shell a shell in the container, ready to run urbex @@ -11,25 +12,52 @@ # DIR defaults to $URBEX_WORKSPACE, then ~/urbex-workspace. Tokens come from # the environment if set (URBEX_PROXMOX_TOKEN, URBEX_CLOUDFLARE_TOKEN), else # from the workspace's credentials file - 'set proxmoxToken' puts them there. +# +# A release's urbex-op runs that release's image, ghcr.io/urbforge/urbex-op, +# pulled the first time; from a checkout, the image 'build' makes. +# URBEX_OPERATOR_IMAGE overrides either. set -euo pipefail +# The release workflow sets this in the copy attached to a release. +URBEX_OP_VERSION=dev + WS=${URBEX_WORKSPACE:-$HOME/urbex-workspace} if [ "${1:-}" = "-w" ]; then WS=$2; shift 2; fi -IMAGE=${URBEX_OPERATOR_IMAGE:-urbex-operator} +if [ "$URBEX_OP_VERSION" = dev ]; then + IMAGE=${URBEX_OPERATOR_IMAGE:-urbex-operator} +else + IMAGE=${URBEX_OPERATOR_IMAGE:-ghcr.io/urbforge/urbex-op:$URBEX_OP_VERSION} +fi REPO=$(cd "$(dirname "$0")/../.." && pwd) ENGINE=$(command -v podman || command -v docker || { echo "urbex-op: podman or docker is needed" >&2; exit 1; }) # Files the container writes stay owned by you: podman maps your user in. # macOS bash 3.2 calls an empty "${a[@]}" unbound under set -u: expand arrays as ${a[@]+"${a[@]}"}. USERNS=(); [[ "$ENGINE" == *podman ]] && USERNS=(--userns keep-id) +# The image, pulled the first time when it is a published one. +need_image() { + "$ENGINE" image inspect "$IMAGE" >/dev/null 2>&1 && return + case "$IMAGE" in + */*) echo "urbex-op: pulling $IMAGE" >&2; "$ENGINE" pull "$IMAGE" >&2 ;; + *) echo "urbex-op: no image $IMAGE - run 'urbex-op build' in a checkout of urbforge/urbex-cli" >&2; exit 1 ;; + esac +} + case "${1:-}" in ""|-h|--help) - sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//' + sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//' + exit 0 ;; + version|--version) + echo "urbex-op $URBEX_OP_VERSION, image $IMAGE" exit 0 ;; build) + [ -f "$REPO/tools/operator/Containerfile" ] || { + echo "urbex-op: build needs a checkout of urbforge/urbex-cli; a release's urbex-op pulls its image" >&2; exit 1; } # Go's and mise's versions come from mise.toml, like every other tool's. pinned() { sed -n "s/^$1 = \"\(.*\)\"\$/\1/p" "$REPO/mise.toml"; } + COMMIT=$(git -C "$REPO" rev-parse HEAD 2>/dev/null || true) exec "$ENGINE" build -t "$IMAGE" --build-arg "GO_VERSION=$(pinned go)" --build-arg "MISE_VERSION=$(pinned min_version)" \ + --build-arg "URBEX_COMMIT=$COMMIT" \ -f "$REPO/tools/operator/Containerfile" "$REPO" ;; set) KEY=${2:?usage: urbex-op set KEY - e.g. proxmoxToken, cloudflareToken, ageKey} @@ -37,6 +65,7 @@ case "${1:-}" in VALUE= if [ -t 0 ]; then read -rsp "$KEY: " VALUE || true; echo; else IFS= read -r VALUE || true; fi [ -n "$VALUE" ] || { echo "urbex-op: empty value, nothing stored" >&2; exit 1; } + need_image # Written with the image's Python (Ansible's, which has PyYAML), so the file stays valid YAML and # keeps every other credential; mode 0600. printf '%s' "$VALUE" | "$ENGINE" run --rm -i ${USERNS[@]+"${USERNS[@]}"} --user "$(id -u):$(id -g)" \ @@ -59,6 +88,7 @@ os.chmod(path, 0o600) chmod 700 "$WS/home/.ssh" "$WS/home/.urbex" [ -f "$WS/home/.ssh/urbex" ] || ssh-keygen -q -t ed25519 -N "" -C urbex -f "$WS/home/.ssh/urbex" if [ ! -f "$WS/home/age.key" ]; then + need_image "$ENGINE" run --rm -v "$WS/home:/h:z" "$IMAGE" age-keygen -o /h/age.key 2>/dev/null chmod 600 "$WS/home/age.key" fi @@ -76,6 +106,7 @@ esac [ -d "$WS/home" ] || { echo "urbex-op: no workspace at $WS - run 'urbex-op -w $WS init' first" >&2; exit 1; } CMD=$1 [ "$CMD" = shell ] && CMD="exec bash" +need_image TTY=(); [ -t 0 ] && [ -t 1 ] && TTY=(-it) # OpenSSH (Ansible's transport) refuses to run for a uid with no passwd entry. Podman's keep-id adds