diff --git a/README.md b/README.md index f56c56a..0830d43 100644 --- a/README.md +++ b/README.md @@ -58,10 +58,31 @@ The runner decides, through environment variables, so the same workflow works on - `BUILDKIT_ENDPOINT` set (e.g. `tcp://buildkit:1234`): builds on that BuildKit with the `remote` driver, which keeps its own cache. Images built with `tag` are pushed uncompressed to `BUILDKIT_REGISTRY` (e.g. `buildkit:5000`), a registry the BuildKit and the runner's docker daemon can both reach, and pulled from there instead of being loaded. - `BUILDKIT_ENDPOINT` not set (e.g. on GitHub's runners): builds in a `docker-container` builder with the GitHub Actions cache (`type=gha`, `cache-mode` defaults to `max`) scoped to `image`. Pass `driver-opts: network=host` if the build pushes to a registry on the runner's `localhost`, e.g. a service container. -Set them in the runner's environment (e.g. its `.env` file, or the runner container's environment), not in workflows, so jobs that fall back to GitHub's runners don't get them. +Set them in the runner's environment, not in workflows, so jobs that fall back to GitHub's runners don't get them. See [Setting up a self-hosted runner](#setting-up-a-self-hosted-runner). Every build in a job reuses the builder of the first one, so a later build starts with the layers of earlier ones. +### Setting up a self-hosted runner + +1. Run a BuildKit the runner can reach, e.g. `moby/buildkit` started with `--addr tcp://0.0.0.0:1234`. It keeps the layer cache, so share one long-lived BuildKit between your runners. +2. Run a registry that both the BuildKit and the runner's docker daemon can reach, e.g. `registry:3` on port 5000. Images built with `tag` go through it. If it uses plain HTTP, allow that on both sides: + - in BuildKit's `buildkitd.toml`: + ```toml + [registry."buildkit:5000"] + http = true + ``` + - in the runner's `/etc/docker/daemon.json`: `{"insecure-registries": ["buildkit:5000"]}` +3. Set both variables in the runner's `.env` file, in the runner's directory next to `run.sh`. The runner reads it when it starts and passes it to every job: + ``` + BUILDKIT_ENDPOINT=tcp://buildkit:1234 + BUILDKIT_REGISTRY=buildkit:5000 + ``` + `BUILDKIT_REGISTRY` is required with `BUILDKIT_ENDPOINT` for builds with `tag`; those fail without it. + +With `push`, the BuildKit pushes straight to the target registry with the runner's `docker login` credentials, so it needs to reach that registry too. + +A self-hosted runner without `BUILDKIT_ENDPOINT` still works, with the GitHub Actions cache, and the action adds a notice to the run saying so. + ### Choosing `image` `image` is the cache scope, so builds that share it share their cache. A GitHub Actions cache scope only keeps what the last build wrote to it: diff --git a/action.yml b/action.yml index 4318d77..faf2ef6 100644 --- a/action.yml +++ b/action.yml @@ -58,12 +58,16 @@ runs: IMAGE: ${{ inputs.image }} TAG: ${{ inputs.tag }} PUSH: ${{ inputs.push }} + RUNNER_ENVIRONMENT: ${{ runner.environment }} run: | if [[ -n "$TAG" && -n "$PUSH" ]]; then echo "::error::Set tag or push, not both" exit 1 fi endpoint="${BUILDKIT_ENDPOINT:-}" + if [[ -z "$endpoint" && "$RUNNER_ENVIRONMENT" == 'self-hosted' && -z "${BUILD_IMAGE_BUILDER:-}" ]]; then + echo "::notice title=Building without a shared BuildKit::BUILDKIT_ENDPOINT is not set on this self-hosted runner, so images build with the GitHub Actions cache. Set it in the runner's environment to build on a shared BuildKit." + fi outputs='' if [[ -z "$TAG" && -z "$PUSH" ]]; then outputs='type=cacheonly' diff --git a/package.json b/package.json index 4204574..6d1b5cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kibalabs/github-action-build-image", - "version": "1.0.0", + "version": "1.0.1", "description": "GitHub Action to build a docker image with a shared BuildKit when the runner has one, or the GitHub Actions cache otherwise", "license": "MIT", "private": true,