Files
caddy-custom/.gitea/workflows/build.yml
T

129 lines
5.9 KiB
YAML

# Builds git.thesatelliteoflove.com/phil/caddy (Caddy + CrowdSec bouncer/AppSec
# + rate limiting) and keeps it current: a daily run fingerprints the upstream
# versions and only builds when something changed. dockcheck on the server
# then rolls the new :2 out (after its DaysOld wait).
#
# The runner has no Docker daemon; images are built by the rootless BuildKit
# container next to it, reached through its socket (same as StarPunk).
name: Build custom Caddy
on:
schedule:
- cron: '30 4 * * *' # daily, before dockcheck at 08:00
push:
branches: [main]
workflow_dispatch:
inputs:
force:
description: Rebuild even if this fingerprint was already built
type: boolean
default: false
env:
REGISTRY: git.thesatelliteoflove.com
IMAGE: git.thesatelliteoflove.com/phil/caddy
BUILDKIT: unix:///run/buildkit/buildkit/buildkitd.sock
ANSIBLE_REPO: https://git.thesatelliteoflove.com/phil/ansible.git
GOTIFY_URL: http://gotify # the runner is on lava
jobs:
build:
runs-on: docker
steps:
- name: Install tools
run: |
apk add --no-cache nodejs git curl jq python3 py3-yaml py3-jinja2 docker-cli docker-cli-buildx
curl -fsSL -o /usr/local/bin/regctl https://github.com/regclient/regclient/releases/latest/download/regctl-linux-amd64
chmod +x /usr/local/bin/regctl
- name: Checkout
uses: actions/checkout@v4
- name: Resolve upstream versions
id: v
run: |
set -euo pipefail
# Follow what Docker Hub has published, so -builder and -alpine exist
CADDY_VERSION=$(regctl image config caddy:2-alpine --platform linux/amd64 \
--format '{{range .Config.Env}}{{println .}}{{end}}' | sed -n 's/^CADDY_VERSION=v//p')
test -n "$CADDY_VERSION"
regctl image digest "caddy:${CADDY_VERSION}-builder" >/dev/null
BASE_DIGEST=$(regctl image digest "caddy:${CADDY_VERSION}-alpine")
BOUNCER=$(curl -fsSL https://api.github.com/repos/hslatman/caddy-crowdsec-bouncer/releases/latest | jq -r .tag_name)
RL_SHA=$(curl -fsSL https://api.github.com/repos/mholt/caddy-ratelimit/commits/master | jq -r .sha)
test -n "$BOUNCER" -a "$BOUNCER" != null -a -n "$RL_SHA" -a "$RL_SHA" != null
FP=$(printf '%s\n' "$CADDY_VERSION" "$BASE_DIGEST" "$BOUNCER" "$RL_SHA" "${{ github.sha }}" | sha256sum | cut -c1-12)
{
echo "caddy=$CADDY_VERSION"; echo "bouncer=$BOUNCER"; echo "rl=$RL_SHA"; echo "fp=$FP"
} >> "$GITHUB_OUTPUT"
echo "caddy=$CADDY_VERSION bouncer=$BOUNCER ratelimit=$RL_SHA fp=$FP"
# The automatic Actions token can't use the container registry, so this
# is a personal access token with only write:package (secret REGISTRY_TOKEN).
- name: Log in to the Gitea registry
run: |
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login "$REGISTRY" -u "${{ github.repository_owner }}" --password-stdin
echo "${{ secrets.REGISTRY_TOKEN }}" | regctl registry login "$REGISTRY" -u "${{ github.repository_owner }}" --pass-stdin
- name: Already built?
id: skip
run: |
if [ "${{ inputs.force }}" != "true" ] && regctl image digest "$IMAGE:fp-${{ steps.v.outputs.fp }}" >/dev/null 2>&1; then
echo "skip=true" >> "$GITHUB_OUTPUT"
echo "Up to date (fp-${{ steps.v.outputs.fp }})"
fi
# The real Caddyfile (no secrets in it) must validate with the new build,
# so a Caddy or plugin release that breaks it never reaches the server.
- name: Render production Caddyfile
if: steps.skip.outputs.skip != 'true'
run: |
git clone -q --depth 1 "$ANSIBLE_REPO" /tmp/ansible
mkdir -p rendered
python3 test/render_caddyfile.py /tmp/ansible > rendered/Caddyfile
- name: Use the BuildKit socket
if: steps.skip.outputs.skip != 'true'
run: docker buildx create --name ci --driver remote "$BUILDKIT" --use
- name: Test
if: steps.skip.outputs.skip != 'true'
run: |
docker buildx build --target test --progress plain \
--build-arg CADDY_VERSION=${{ steps.v.outputs.caddy }} \
--build-arg CROWDSEC_BOUNCER_VERSION=${{ steps.v.outputs.bouncer }} \
--build-arg RATELIMIT_VERSION=${{ steps.v.outputs.rl }} \
-f Containerfile .
- name: Build and push
if: steps.skip.outputs.skip != 'true'
run: |
v=${{ steps.v.outputs.caddy }}; fp=${{ steps.v.outputs.fp }}
docker buildx build --push --target final \
--build-arg CADDY_VERSION=$v \
--build-arg CROWDSEC_BOUNCER_VERSION=${{ steps.v.outputs.bouncer }} \
--build-arg RATELIMIT_VERSION=${{ steps.v.outputs.rl }} \
--build-arg FINGERPRINT=$fp \
-t "$IMAGE:$v-$fp" -t "$IMAGE:fp-$fp" -t "$IMAGE:$v" \
-t "$IMAGE:${v%.*}" -t "$IMAGE:2" -t "$IMAGE:latest" \
-f Containerfile .
- name: Notify success
if: success() && steps.skip.outputs.skip != 'true'
run: |
curl -fsS "$GOTIFY_URL/message?token=${{ secrets.GOTIFY_TOKEN }}" \
--form-string title="Caddy image built" --form-string priority=3 \
--form-string message="caddy ${{ steps.v.outputs.caddy }}, bouncer ${{ steps.v.outputs.bouncer }}, ratelimit ${{ steps.v.outputs.rl }} -> $IMAGE:2 (dockcheck applies it after 3 days)"
- name: Notify failure
if: failure()
run: |
curl -fsS "$GOTIFY_URL/message?token=${{ secrets.GOTIFY_TOKEN }}" \
--form-string title="Caddy image build FAILED" --form-string priority=8 \
--form-string message="Server keeps its current Caddy image. ${{ github.server_url }}/${{ github.repository }}/actions"
- name: Clean up
if: always()
run: |
docker buildx rm ci || true
docker logout "$REGISTRY" || true