towerops/.forgejo/workflows/production.yaml
Graham McIntire e6f408aa33 fix: use legacy docker build without BuildKit for unprivileged runner (#220)
BuildKit (used by build-push-action and buildx) requires privileged mount
operations that fail on unprivileged runners with vfs storage driver.

Solution: Replace build-push-action with plain 'docker build' and 'docker push'
commands with DOCKER_BUILDKIT=0 to use the legacy builder.

Trade-offs:
- Works on unprivileged runners without privileged access
- Slower builds (no BuildKit caching/parallelism)
- No build attestations/provenance (already disabled)

Reviewed-on: graham/towerops-web#220
2026-03-29 12:04:54 -05:00

180 lines
5.7 KiB
YAML

name: Production Deployment
on:
push:
branches:
- main
env:
REGISTRY: git.mcintire.me
IMAGE_NAME: graham/towerops-web
jobs:
test-exunit:
name: Run ExUnit Tests
runs-on: ubuntu-22.04
env:
MIX_ENV: test
DATABASE_URL: ecto://postgres:postgres@postgres/towerops_test
services:
postgres:
image: timescale/timescaledb:latest-pg17
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: towerops_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: https://github.com/actions/checkout@v4
- name: Set up Elixir
uses: https://github.com/erlef/setup-beam@v1
with:
version-type: strict
elixir-version: '1.18.1'
otp-version: '27.2'
- name: Cache Mix
uses: https://github.com/actions/cache@v4
with:
path: ~/.mix
key: ${{ runner.os }}-mix-1.18.1-27.2
restore-keys: ${{ runner.os }}-mix-
- name: Cache deps
uses: https://github.com/actions/cache@v4
with:
path: deps
key: ${{ runner.os }}-deps-${{ hashFiles('mix.lock') }}
restore-keys: ${{ runner.os }}-deps-
- name: Cache _build
uses: https://github.com/actions/cache@v4
with:
path: _build
key: ${{ runner.os }}-build-v2-${{ hashFiles('lib/**/*.ex') }}-${{ hashFiles('mix.lock') }}
restore-keys: ${{ runner.os }}-build-v2-
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y libssl-dev libsnmp-dev snmp-mibs-downloader postgresql-client
- name: Install dependencies
run: mix deps.get
- name: Compile C NIF
run: make -C c_src
- name: Compile (warnings as errors)
run: mix compile --warnings-as-errors
- name: Run tests
run: mix test
build-and-deploy:
name: Build and Deploy to Production
runs-on: ubuntu-22.04
needs: [test-exunit]
steps:
- name: Checkout code
uses: https://github.com/actions/checkout@v4
with:
fetch-depth: 0 # Need full history for git operations
- name: Start Docker daemon
run: |
# Start Docker daemon to run kaniko container
if ! docker info > /dev/null 2>&1; then
sudo dockerd \
--iptables=false \
--ip6tables=false \
--bridge=none \
--storage-driver=vfs &
# Wait for Docker to be ready
timeout 30 bash -c 'until docker info > /dev/null 2>&1; do sleep 1; done'
fi
- name: Generate image tag
id: tag
run: |
BRANCH=${GITHUB_REF#refs/heads/}
TIMESTAMP=$(date +%s)
SHORT_SHA=$(git rev-parse --short=7 HEAD)
TAG="${BRANCH}-${TIMESTAMP}-${SHORT_SHA}"
echo "tag=${TAG}" >> $GITHUB_OUTPUT
echo "Full image tag: ${TAG}"
- name: Log in to Container Registry
uses: https://github.com/docker/login-action@v3
with:
registry: ${{ secrets.REGISTRY_URL }}
username: ${{ secrets.REGISTRY_USER }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Build and push Docker image
env:
DOCKER_BUILDKIT: 0
run: |
IMAGE_TAG="${{ steps.tag.outputs.tag }}"
# Build image without BuildKit (required for unprivileged runner)
docker build \
--build-arg MIX_ENV=prod \
--file k8s/Dockerfile \
--tag "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}" \
--tag "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:production" \
.
# Push both tags
docker push "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${IMAGE_TAG}"
docker push "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:production"
- name: Update deployment manifest
run: |
IMAGE_TAG="${{ steps.tag.outputs.tag }}"
# Configure git
git config user.name "Forgejo Actions"
git config user.email "actions@git.mcintire.me"
# Fetch and checkout production branch
git fetch origin production
git checkout production
git pull origin production --rebase
# Merge main branch changes
git merge origin/main --no-edit
# Update deployment manifest
sed -i "s|image: ${REGISTRY}/${IMAGE_NAME}:.*|image: ${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}|g" k8s/deployment.yaml
# Commit and push if there are changes
git add k8s/deployment.yaml
if git diff --staged --quiet; then
echo "No changes to deployment.yaml"
else
git commit -m "chore: update production image to ${IMAGE_TAG} [skip ci]"
git push origin production
fi
- name: Deployment summary
run: |
echo "### ✅ Production Deployment Complete" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Image:** \`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tag }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "FluxCD will automatically apply the changes to the cluster." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Monitor deployment:**" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
echo "kubectl rollout status deployment/towerops -n towerops" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY