# Build and push an image

> Build the repository's Dockerfile, push the image to the branchd registry, and run a job in the image you built.
> **Available** in repositories branchd reads pipeline files for. It is switching repositories on one at a time: until yours is, it keeps running its `.forge-ci.yaml` or `.xeonr-ci.yaml`. See [What is available](https://docs.branchd.run/branchd/pipelines#what-is-available).

```yaml title=".branchd/pipelines/ci.yaml"
# .branchd/pipelines/ci.yaml
# Test, then build the repository's Dockerfile and push the image to the
# branchd registry on pushes to main. The build job passes the image's
# reference on as an output.
name: CI
on:
  push:
    branches: [main]
  pull_request:
jobs:
  test:
    image: golang:1.26
    steps:
      - run: go test ./...
  build:
    needs: [test]
    if: branch == 'main' && event == 'push'
    outputs:
      image: ${{ steps.image.outputs.image }}
    steps:
      - id: image
        # Also written branchd/steps/images/image-build@v1, the step's address.
        uses: ci/image-build@1
        with:
          image: $CI_REGISTRY_IMAGE
          tag: $CI_PIPELINE_IID
          dockerfile: Dockerfile
  smoke:
    # build runs only on pushes to main, so smoke has the same if:. A need
    # on a job that does not run is refused, unless it is optional: true.
    if: branch == 'main' && event == 'push'
    needs: [build]
    # The job runs in the image the build job pushed.
    image: ${{ needs.build.outputs.image }}
    steps:
      - run: /app/server --version
```

- **`uses: branchd/steps/images/image-build@v1`** is a typed step: one program
  that runs in its own image, published in branchd's shared step repository,
  `branchd/steps`. It builds `dockerfile` from the checkout and pushes
  `image:tag`. Its output `image` is the pushed reference,
  `<image>:<tag>@<digest>`.
- **`$CI_REGISTRY_IMAGE`** is this repository's path in the branchd registry,
  and **`$CI_PIPELINE_IID`** the run's number. `$NAME` of a variable known
  when the run is compiled expands in `with:`.
- **The job's `outputs:`** pass the step's output on. A job that needs
  `build` reads it as `${{ needs.build.outputs.image }}`, here as the
  `image:` of the job that runs next.
- **The same `if:`** on `build` and `smoke`: `smoke` needs `build`, and a need
  on a job that does not run is refused. On a pull request neither runs.

As the job's **only** step, a typed step needs nothing more: the job is the
step, so it has no `image:` and no `run:` steps, and its `if:` goes on the
job. Among `run:` steps it must be marked `isolated: true`; see
[Isolate a step from the job](https://docs.branchd.run/branchd/pipelines/isolated-steps). A
job of its own, as here, is the simpler choice when the next step needs
only its output.

`branchd/steps/images/image-build@v1` takes `image`, `tag` and `dockerfile` (all
required), and `build_args` (a map of `--build-arg` values), `push_latest`
and `build_mode`. `@v1` is the newest published `v1.x.y` tag of
`branchd/steps/images`; `@v1.0.2` is exact. The old name, `ci/image-build@1`,
still resolves, to `branchd/steps/images/image-build` at a release branchd moves;
it is kept for existing files, and new files use the address.

**Go modules in the build.** The step also passes a `GOPROXY` build argument,
`<branchd>/-/go/,direct`, unless `build_args` names `GOPROXY` itself, with
the job's modules token as `CI_JOB_TOKEN`. A Dockerfile opts in with
`ARG GOPROXY` before its `go` commands; it then fetches Go modules,
your team's private ones included, through branchd's module proxy. A
Dockerfile without `ARG GOPROXY` is built as before.

**Lantern releases and source maps in the build.** A Vite build that uses
`@lntrn/web`'s plugin (0.5.3 or later) registers its release with Lantern and
uploads its source maps when it has a Lantern workload token as
`LANTERN_WORKLOAD_TOKEN`. Give the step `lantern_workload: true`. The
`branchd/presets/service` preset sets it on every image build of a publishing
run, so a repository on that preset needs no pipeline change. The step
exchanges a token only when the Dockerfile names `LANTERN_WORKLOAD_TOKEN`, so
any other Dockerfile builds as before. Lantern takes the token only from a
protected run, for a client project that grants the repository (its CI grants)
or that lists the repository among its CI projects. Getting the token is best
effort: on a pull request's run, or when Auth refuses, the build log says so,
the build runs without it, and the plugin uploads nothing.

Declare the token where `vite build` runs, in a build stage that the final
image copies from. Never use `ENV`, and never declare it in the final stage:
that keeps it out of the pushed image's layers and history.

- **`image-build`** (kaniko, the default) passes it as a build argument:

  ```dockerfile
  FROM node:22 AS build
  WORKDIR /app
  COPY . .
  RUN pnpm install --frozen-lockfile
  ARG VERSION
  ARG LANTERN_WORKLOAD_TOKEN
  RUN pnpm build

  FROM nginx:1.27
  COPY --from=build /app/dist /usr/share/nginx/html
  ```

  Put the `ARG` just before the build `RUN`, after the install. The token
  changes every run, so every step after the `ARG` misses the layer cache.

- **`image-build-multiarch`** (`build.platforms` in the preset) mounts it as a
  BuildKit secret with the id `LANTERN_WORKLOAD_TOKEN`, which is in no layer
  and no build record:

  ```dockerfile
  RUN --mount=type=secret,id=LANTERN_WORKLOAD_TOKEN \
      LANTERN_WORKLOAD_TOKEN="$(cat /run/secrets/LANTERN_WORKLOAD_TOKEN 2>/dev/null)" pnpm build
  ```

  A Dockerfile written with the `ARG` form also builds here: the step passes
  the build argument too when the Dockerfile declares
  `ARG LANTERN_WORKLOAD_TOKEN`.

Source: https://docs.branchd.run/branchd/pipelines/build-and-push-an-image
