# docker-build-push (Gitea Actions) A composite Gitea Action that computes the next version via [`get-next-version`](../get-next-version/action.yml:1), builds a Docker image (optionally for a specific multi-stage `target`), logs in to the target registry, pushes the image, and (by default) tags the commit with the version that was just built. ## How it works 1. **Get next version** — calls the sibling [`get-next-version`](../get-next-version/action.yml:1) action via `self:captbrick/gitea-actions/get-next-version@master`, forwarding `main_branch`, `major`, `minor`, `image_registry`, `image_repository`, and `target` as its `image_name` input. When `image_registry` is empty, `get-next-version` falls back to its own default (`gitea.livingeach.day`) internally, so this action doesn't need to repeat that default. The image repository is lowercased by `get-next-version`, because Docker repository names must be lowercase. When `target` is set, `get-next-version` appends it to the image repository and computes a distinct `version_tag` (see [Multiple images from one Dockerfile](#multiple-images-from-one-dockerfile) below). `get-next-version` exports `VERSION`, `VERSION_TAG`, `IMAGE_REGISTRY` and `IMAGE_TAG` into `$GITEA_ENV`, which the remaining steps consume directly, and also re-exposes `version` / `version_tag` / `image_tag` as this action's own outputs. 2. **Prepare cache refs** — when `cache` is enabled (default), computes two registry-backed BuildKit cache refs below the final lowercased image repository's dedicated `cache` repository: one for the base branch and one for the current/head branch. Branch names are lowercased and sanitized so refs like `feature/foo` become valid Docker tags such as `feature-foo`. For merge commits, the action also tries to detect the merged source branch from the commit message and adds that branch cache as an extra cache source. 3. **Docker login** — runs when either `push` or `cache` is enabled, because registry cache import/export needs registry authentication too. 4. **Set up QEMU** — always run via `docker/setup-qemu-action`, so the runner can emulate non-native architectures and has a Buildx builder with full feature support (registry cache export, multi-platform output), regardless of how many entries `platforms` has. 5. **Build and push image** — always runs `docker buildx build`, passing `--build-arg APP_VERSION=$VERSION`, `--tag $IMAGE_TAG`, `-f `, optional `--target `, and `--platform ` (default `linux/amd64`). When `cache` is enabled, adds `--cache-from` for the base, detected merge-source, and branch cache refs and `--cache-to` for the branch cache ref. Pushing is consolidated into this single step: when `push: 'true'`, `--push` is added so the result (single- or multi-platform) is published straight from Buildx. With `push: 'false'`, no output flag is added at all — the build still runs and still updates the registry cache (when `cache: 'true'`), but produces no local image; it only warms the cache. See [Building for multiple platforms](#building-for-multiple-platforms) below. 6. **Create version tag** — gated behind the `push` input (default on); commits a Git tag named after `$VERSION_TAG` using the `gitea-actions[bot]` identity and pushes it. Setting `push: 'false'` skips the image push and Git tag creation/push; if `cache` is still enabled, the build can still log in and update the branch cache. ## Usage ```yaml jobs: build: name: Docker Build & Push runs-on: ubuntu-latest permissions: contents: write steps: - name: Checkout code uses: actions/checkout@v7 with: fetch-depth: 0 - uses: self:captbrick/gitea-actions/docker-build-push@master with: main_branch: master major: 1 minor: 1 registry_username: ${{ secrets.REGISTRY_USERNAME }} registry_password: ${{ secrets.REGISTRY_PASSWORD }} ``` > The workflow needs `permissions: contents: write` so the action can push > the Git tag it creates. The checkout step needs `fetch-depth: 0` so the > full tag history is available to `get-next-version`. Set `push: 'false'` > to build without pushing the final image or creating a Git tag — the build > runs but produces no accessible image (just warms the cache, if enabled). > With the default `cache: 'true'`, the action still logs in and updates the > branch BuildKit cache; set `cache: 'false'` as well to skip registry > interaction entirely. ### Multiple images from one Dockerfile Use `target` when a single multi-stage Dockerfile produces more than one image (e.g. `frontend` and `backend`). `target` is forwarded to `get-next-version` as its `image_name` input, which: - appends `/` to the image repository, so each target is pushed to its own repository path (e.g. `gitea.livingeach.day/owner/repo/frontend:1.0.6` and `gitea.livingeach.day/owner/repo/backend:1.0.6`), while `version` itself (`1.0.6`) stays the same for every target built from the same commit; - prepends `-` to `version_tag`, so the Git tag created for each target mirrors the image repository naming (e.g. `frontend-1.0.6`, `backend-1.0.6`) and pushing one doesn't collide with the others. A common pattern is to first warm up the shared layer cache with a non-tagged, non-pushed `build-all` target, then build and push each real target: ```yaml - name: Warm up build cache uses: self:captbrick/gitea-actions/docker-build-push@master with: main_branch: master target: build-all push: 'false' registry_username: ${{ secrets.REGISTRY_USERNAME }} registry_password: ${{ secrets.REGISTRY_PASSWORD }} - name: Build & push frontend uses: self:captbrick/gitea-actions/docker-build-push@master with: main_branch: master target: frontend registry_username: ${{ secrets.REGISTRY_USERNAME }} registry_password: ${{ secrets.REGISTRY_PASSWORD }} - name: Build & push backend uses: self:captbrick/gitea-actions/docker-build-push@master with: main_branch: master target: backend registry_username: ${{ secrets.REGISTRY_USERNAME }} registry_password: ${{ secrets.REGISTRY_PASSWORD }} ``` > `push: 'false'` on the warm-up step means it skips Docker login, push, > and tagging entirely, so its `target: build-all` value never produces a > pushed image or a Git tag — it only primes Docker's layer cache for the > `frontend` and `backend` builds that follow. ### Registry build cache Registry-backed BuildKit cache is enabled by default with `cache: 'true'`. The action prepares cache refs using a dedicated `cache` repository below the computed `IMAGE_REPOSITORY` and `cache_registry` when set, otherwise the computed `IMAGE_REGISTRY`: - `/cache:` is used as a stable fallback cache source; - `/cache:` is used as an additional cache source on merge commits when the source branch can be detected; - `/cache:` is used as both a cache source and the cache destination (`mode=max`). For pull requests, the base ref comes from `GITHUB_BASE_REF` and the branch ref comes from `GITHUB_HEAD_REF`. For non-PR builds, missing refs fall back to `main_branch` / `GITHUB_REF_NAME`. Ref names are lowercased and sanitized for Docker tag compatibility. On merge commits, the action parses common merge commit subjects such as `Merge pull request #123 from owner/feature` and `Merge branch 'feature'` to add the source branch cache as an extra `--cache-from` ref. This lets a post-merge build on `master` reuse layers from the merged feature branch cache before writing the updated `master` cache. Because the cache is stored in the registry, `cache: 'true'` requires Docker login even when `push: 'false'`. Disable it with `cache: 'false'` when you want an unauthenticated local-only build. Set `cache_registry` when cache layers should be stored in a different registry than the final image. For example, the final image can still be pushed to `gitea.livingeach.day/owner/repo`, while cache refs are stored under `zot-registry.zot-registry.svc.cluster.local:5000/owner/repo/cache:`. The action logs in to the cache registry when it differs from `IMAGE_REGISTRY`. ### Building for multiple platforms By default `platforms` is `linux/amd64`, matching a single-platform `docker buildx build`. To also publish, for example, an `arm64` variant (needed to run the image on Apple Silicon or ARM hosts — without it you'll hit `no match for platform in manifest` when pulling), set a single foreign platform or a comma-separated list: ```yaml - uses: self:captbrick/gitea-actions/docker-build-push@master with: main_branch: master platforms: linux/amd64,linux/arm64 registry_username: ${{ secrets.REGISTRY_USERNAME }} registry_password: ${{ secrets.REGISTRY_PASSWORD }} ``` `docker/setup-qemu-action` always runs before the build, so the runner can emulate non-native architecture(s) (needed even for a single foreign platform like `platforms: linux/arm64`, not just comma-separated lists) and has a full-featured Buildx builder available (registry cache export, multi-platform output). Push is consolidated into the build step itself (`docker buildx build --push`): with `push: 'true'`, this works the same way whether `platforms` has one or several entries, pushing the result (single image or multi-arch manifest list) straight to the registry. With `push: 'false'`, no output flag is added regardless of platform count — the build runs and still updates the registry cache (when `cache: 'true'`), but produces no accessible image; use `push: 'false'` only to warm the cache, not to inspect the image locally. (Buildx also can't `--load` a multi-platform result into the local Docker daemon even if that were attempted.) ## Inputs | Name | Description | Required | Default | | -------------------- | ------------------------------------------------------------------------------------- | -------- | ---------------------- | | `main_branch` | Name of the main branch (e.g., `master` or `main`); forwarded to `get-next-version` | yes | | | `major` | Major version number; forwarded to `get-next-version` | no | `'1'` | | `minor` | Minor version number; forwarded to `get-next-version` | no | `'0'` | | `image_registry` | Container registry used to build the image tag and to log in to; forwarded to `get-next-version` when set, otherwise its default (`gitea.livingeach.day`) is used | no | `''` | | `image_repository` | Image repository used to build the image tag; forwarded to `get-next-version` when set, otherwise its default (full `owner/repo` path) is used. It is always lowercased before building `IMAGE_TAG`, because Docker repository names must be lowercase | no | `''` | | `target` | Dockerfile build stage/target (`docker build --target`); also forwarded to `get-next-version` as `image_name` — see [Multiple images from one Dockerfile](#multiple-images-from-one-dockerfile) | no | `''` | | `registry_username` | Username (or service principal app ID) used to log in to the registry | yes | | | `registry_password` | Password (or service principal secret) used to log in to the registry | yes | | | `dockerfile` | Path to the Dockerfile to build | no | `Dockerfile` | | `context` | Docker build context path | no | `.` | | `push` | Whether to push the built image and create/push a Git tag matching `version_tag`; Docker login also runs when `cache` is enabled | no | `'true'` | | `cache` | Whether to use registry-backed Docker BuildKit cache with branch/base cache refs and detected merge-source cache refs | no | `'true'` | | `cache_registry` | Optional registry used for BuildKit cache refs; defaults to the computed `IMAGE_REGISTRY` when empty | no | `''` | | `platforms` | Comma-separated `docker buildx build --platform` list, e.g. `linux/amd64,linux/arm64` — see [Building for multiple platforms](#building-for-multiple-platforms) | no | `'linux/amd64'` | ## Outputs | Name | Description | | ------------- | ------------------------------------------------------------------- | | `version` | Next application version computed by `get-next-version` (e.g., `1.0.6` or `1.0.6-feature.1`) | | `version_tag` | Version string used for the Git tag (equals `version`, or `target-version` when `target` is set) | | `image_tag` | Full image tag that was built and pushed (e.g., `gitea.livingeach.day/owner/repo:1.0.6`, or `gitea.livingeach.day/owner/repo/frontend:1.0.6` when `target` is set) |