name: Self-host images # Images for the self-host stack, published to ghcr.io/cap-go/. Capgo Cloud does not use # them, so this workflow is separate from build_and_deploy.yml and can never block a deploy. # For now only capgo-setup (selfhost/capgo-setup/Dockerfile): it applies the migrations. on: pull_request: paths: - .github/workflows/selfhost_images.yml - selfhost/capgo-setup/** - scripts/selfhost-setup.ts - scripts/local-db.ts - scripts/supabase-worktree-config.ts - supabase/bootstrap.sql - supabase/migrations/** push: tags: - "capgo-[0-9]*" concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} permissions: {} env: IMAGE: ghcr.io/cap-go/capgo-setup # Same database image as supabase/local-db.compose.yml (Docker Hub: no pull limit on hosted runners). POSTGRES_IMAGE: supabase/postgres:17.6.1.143 jobs: # Proves the image applies every migration to an empty database and that a second run is a no-op. test_setup: if: github.event_name == 'pull_request' name: capgo-setup applies migrations (amd64) runs-on: ubuntu-latest timeout-minutes: 15 permissions: contents: read steps: - name: Checkout repository uses: actions/checkout@v6 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Build image uses: docker/build-push-action@v6 with: context: . file: selfhost/capgo-setup/Dockerfile load: true tags: capgo-setup:test - name: Start an empty database run: | docker network create setup-test docker run -d --name db --network setup-test -e POSTGRES_PASSWORD=postgres "$POSTGRES_IMAGE" # TCP check: the init server only listens on the socket, so this waits for the real start. for _ in $(seq 1 90); do docker exec db pg_isready -U postgres -h 127.0.0.1 && exit 0 sleep 2 done docker logs db exit 1 - name: Run setup, then run it again (must apply nothing) run: | setup() { docker run --rm --network setup-test \ -e DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres \ -e SUPABASE_ADMIN_URL=postgresql://supabase_admin:postgres@db:5432/postgres \ capgo-setup:test } setup second="$(setup)" echo "$second" case "$second" in "Migrations: 0 applied,"*) ;; *) echo "::error::The second run applied migrations again: setup is not idempotent."; exit 1 ;; esac # Native build per architecture. On a capgo-* tag each one is pushed by digest; `publish` tags them. build: name: Build capgo-setup (${{ matrix.arch }}) runs-on: ${{ matrix.runner }} timeout-minutes: 20 permissions: contents: read packages: write strategy: fail-fast: false matrix: include: - arch: amd64 runner: ubuntu-24.04 - arch: arm64 runner: ubuntu-24.04-arm env: PUSH: ${{ github.event_name == 'push' }} steps: - name: Checkout repository uses: actions/checkout@v6 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GHCR if: env.PUSH == 'true' uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build (and push by digest on tags) id: build uses: docker/build-push-action@v6 with: context: . file: selfhost/capgo-setup/Dockerfile platforms: linux/${{ matrix.arch }} outputs: type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true,push=${{ env.PUSH }} - name: Save digest if: env.PUSH == 'true' env: DIGEST: ${{ steps.build.outputs.digest }} run: | mkdir -p digests touch "digests/${DIGEST#sha256:}" - name: Upload digest if: env.PUSH == 'true' uses: actions/upload-artifact@v6 with: name: capgo-setup-digest-${{ matrix.arch }} path: digests/* if-no-files-found: error retention-days: 1 # capgo-1.2.3 -> 1.2.3, 1, latest. capgo-1.2.3-alpha.4 -> alpha only. publish: if: github.event_name == 'push' needs: build name: Publish capgo-setup runs-on: ubuntu-latest timeout-minutes: 10 permissions: packages: write steps: - name: Download digests uses: actions/download-artifact@v6 with: pattern: capgo-setup-digest-* path: digests merge-multiple: true - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to GHCR uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Tag the multi-arch image env: TAG: ${{ github.ref_name }} run: | version="${TAG#capgo-}" if [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+-alpha\.[0-9]+$ ]]; then tags=(alpha) elif [[ "$version" =~ ^([0-9]+)\.[0-9]+\.[0-9]+$ ]]; then tags=("$version" "${BASH_REMATCH[1]}" latest) else echo "::error::Unexpected release tag: $TAG" exit 1 fi tag_args=() for tag in "${tags[@]}"; do tag_args+=(-t "$IMAGE:$tag"); done digests=() for file in digests/*; do digests+=("$IMAGE@sha256:$(basename "$file")"); done docker buildx imagetools create "${tag_args[@]}" "${digests[@]}" docker buildx imagetools inspect "$IMAGE:${tags[0]}"