prop/.forgejo/workflows/build.yaml
Graham McIntire 2a6f14129a
ci: build against a dind sidecar instead of the host socket
The host docker socket wasn't mounted consistently across act_runners,
so which runner picked up the push was the difference between a clean
build and "unable to connect to docker." Switch to a dockerd service
pinned to the same bridge network as the job; the CLI now talks to
it over TCP via DOCKER_HOST=tcp://dockerd:2375. Every runner has a
daemon, every time — no more re-runs for the scheduling lottery.
2026-04-19 12:24:41 -05:00

141 lines
4.8 KiB
YAML

name: Build and Push
on:
push:
branches:
- main
env:
REGISTRY: git.mcintire.me
IMAGE_NAME: graham/prop
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build-and-push:
name: Build and Push Docker Image
runs-on: ubuntu-22.04
# Runs a dockerd sidecar on the same bridge network as the job
# container so the build doesn't depend on the host's docker socket
# being mounted. Some act_runners don't mount it and there was no
# way to tell at schedule time which runner would win — leading to
# random "unable to connect to docker" failures that went away on a
# re-run. With dind every runner has a daemon, every time.
services:
dockerd:
image: docker:27-dind
options: --privileged
env:
DOCKER_TLS_CERTDIR: ""
env:
DOCKER_HOST: tcp://dockerd:2375
steps:
- name: Checkout code
uses: https://github.com/actions/checkout@v4
- name: Install Docker CLI and buildx
run: |
# Dodge apt entirely — the runner image's bookworm repos have been
# failing GPG verification with "At least one invalid signature".
# The static Docker client binary talks to the dind sidecar via
# the DOCKER_HOST env var set at the job level. buildx is needed
# separately because the Dockerfile uses `--mount=type=cache`,
# which is a BuildKit-only feature.
curl -fsSL -o /tmp/docker.tgz \
https://download.docker.com/linux/static/stable/x86_64/docker-27.5.1.tgz
tar xzf /tmp/docker.tgz -C /tmp
install -m 0755 /tmp/docker/docker /usr/local/bin/docker
rm -rf /tmp/docker /tmp/docker.tgz
mkdir -p /usr/libexec/docker/cli-plugins
curl -fsSL -o /usr/libexec/docker/cli-plugins/docker-buildx \
https://github.com/docker/buildx/releases/download/v0.19.3/buildx-v0.19.3.linux-amd64
chmod +x /usr/libexec/docker/cli-plugins/docker-buildx
docker --version
docker buildx version
- name: Wait for dockerd sidecar
run: |
# dind takes ~5-15s to finish booting inside the service
# container; poll until the TCP endpoint answers.
echo "Using DOCKER_HOST=${DOCKER_HOST}"
for i in $(seq 1 60); do
if docker info >/dev/null 2>&1; then
echo "dockerd reachable after ${i}s"
exit 0
fi
echo "Waiting for dockerd... ($i/60)"
sleep 1
done
echo "dockerd never became reachable — diagnostics:" >&2
echo "--- env | grep -i docker ---" >&2
env | grep -i docker 2>&1 || true
echo "--- docker info (verbose) ---" >&2
docker info 2>&1 || true
exit 1
- name: Generate image tag
id: tag
run: |
TIMESTAMP=$(date +%s)
SHORT_SHA=$(git rev-parse --short=7 HEAD | cut -c1-7)
TAG="main-${TIMESTAMP}-${SHORT_SHA}"
echo "tag=${TAG}" >> $GITHUB_OUTPUT
- name: Log in to container registry
run: |
# Exponential backoff (2s, 4s, 8s) around docker login to
# ride out transient registry blips without failing the
# whole workflow.
attempt=1
max_attempts=3
while : ; do
if echo "${{ secrets.REGISTRY_PASSWORD }}" | \
docker login "${{ secrets.REGISTRY_URL }}" \
-u "${{ secrets.REGISTRY_USER }}" \
--password-stdin; then
exit 0
fi
if [ "$attempt" -ge "$max_attempts" ]; then
echo "docker login failed after $attempt attempts" >&2
exit 1
fi
delay=$((2 ** attempt))
echo "docker login attempt $attempt failed; retrying in ${delay}s"
sleep "$delay"
attempt=$((attempt + 1))
done
- name: Build and push Docker image
run: |
IMAGE="${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}"
TAG="${{ steps.tag.outputs.tag }}"
BUILD_TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
attempt=1
max_attempts=3
while : ; do
if docker buildx build \
--build-arg BUILD_TIMESTAMP="${BUILD_TIMESTAMP}" \
-t "${IMAGE}:${TAG}" -t "${IMAGE}:latest" \
--push \
.; then
exit 0
fi
if [ "$attempt" -ge "$max_attempts" ]; then
echo "docker buildx build failed after $attempt attempts" >&2
exit 1
fi
delay=$((2 ** attempt))
echo "docker buildx build attempt $attempt failed; retrying in ${delay}s"
sleep "$delay"
attempt=$((attempt + 1))
done