Refactor GitHub Actions workflow for self-hosted runner

This commit is contained in:
Joakim Hellsén 2026-03-07 21:27:24 +01:00
commit 1cce89c637
Signed by: Joakim Hellsén
SSH key fingerprint: SHA256:/9h/CsExpFp+PRhsfA0xznFx2CGfTT5R/kpuFfUgEQk

View file

@ -1,4 +1,15 @@
--- ---
# Required setup for self-hosted runner:
# 1. Install dependencies:
# sudo pacman -S qemu-user-static qemu-user-static-binfmt docker
# 2. Add runner to docker group:
# sudo usermod -aG docker forgejo-runner
# 3. Restart runner service to apply group membership:
# sudo systemctl restart forgejo-runner
# 4. Install uv and ruff for the runner user
# 5. Login to GitHub Container Registry:
# echo "ghp_YOUR_TOKEN_HERE" | sudo -u forgejo-runner docker login ghcr.io -u TheLovinator1 --password-stdin
name: Test and build Docker image name: Test and build Docker image
on: on:
push: push:
@ -7,41 +18,34 @@ on:
pull_request: pull_request:
workflow_dispatch: workflow_dispatch:
schedule: schedule:
- cron: "0 15 * * 0" - cron: "0 0 1 * *"
env: env:
TEST_WEBHOOK_URL: ${{ secrets.TEST_WEBHOOK_URL }} TEST_WEBHOOK_URL: ${{ secrets.TEST_WEBHOOK_URL }}
jobs: jobs:
docker: docker:
runs-on: ubuntu-latest runs-on: self-hosted
steps: steps:
# GitHub Container Registry
- uses: docker/login-action@v4
if: github.event_name != 'pull_request'
with:
registry: ghcr.io
username: thelovinator1
password: ${{ secrets.GITHUB_TOKEN }}
# Download the latest commit from the master branch # Download the latest commit from the master branch
- uses: actions/checkout@v6 - uses: actions/checkout@v6
# Set up QEMU # Verify local tools are available on the self-hosted runner
- id: qemu - name: Check local toolchain
uses: docker/setup-qemu-action@v4 run: |
with: python --version
image: tonistiigi/binfmt:master uv --version
platforms: linux/amd64,linux/arm64 ruff --version
cache-image: false docker version
# Set up Buildx so we can build multi-arch images # Bootstrap a local Buildx builder for multi-arch builds
- uses: docker/setup-buildx-action@v4 # (requires qemu-user-static and qemu-user-static-binfmt installed via pacman)
- name: Configure local buildx for multi-arch
# Install the latest version of ruff run: |
- uses: astral-sh/ruff-action@v3 docker buildx inspect local-multiarch-builder >/dev/null 2>&1 || \
with: docker buildx create --name local-multiarch-builder --driver docker-container
version: "latest" docker buildx use local-multiarch-builder
docker buildx inspect --bootstrap
# Lint the Python code using ruff # Lint the Python code using ruff
- run: ruff check --exit-non-zero-on-fix --verbose - run: ruff check --exit-non-zero-on-fix --verbose
@ -52,38 +56,37 @@ jobs:
# Lint Dockerfile # Lint Dockerfile
- run: docker build --check . - run: docker build --check .
# Set up Python 3.13
- uses: actions/setup-python@v6
with:
python-version: 3.14
# Install dependencies # Install dependencies
- uses: astral-sh/setup-uv@v7
with:
version: "latest"
- run: uv sync --all-extras --all-groups - run: uv sync --all-extras --all-groups
# Run tests # Run tests
- run: uv run pytest - run: uv run pytest
# Extract metadata (tags, labels) from Git reference and GitHub events for Docker # Compute image tags
- id: meta - id: tags
uses: docker/metadata-action@v6 name: Compute image tags
env: run: |
DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index IMAGE="ghcr.io/thelovinator1/discord-rss-bot"
with: if [ "${FORGEJO_REF}" = "refs/heads/master" ]; then
images: | echo "tags=${IMAGE}:latest,${IMAGE}:master" >> "$FORGEJO_OUTPUT"
ghcr.io/thelovinator1/discord-rss-bot else
tags: | SHORT_SHA="$(echo "$FORGEJO_SHA" | cut -c1-12)"
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }} echo "tags=${IMAGE}:sha-${SHORT_SHA}" >> "$FORGEJO_OUTPUT"
type=raw,value=master,enable=${{ github.ref == format('refs/heads/{0}', 'master') }} fi
# Build and push the Docker image # Build (and optionally push) Docker image
- uses: docker/build-push-action@v7 - name: Build and push Docker image
with: env:
context: . TAGS: ${{ steps.tags.outputs.tags }}
platforms: linux/amd64,linux/arm64 run: |
push: ${{ github.event_name != 'pull_request' }} IFS=',' read -r -a tag_array <<< "$TAGS"
labels: ${{ steps.meta.outputs.labels }} tag_args=()
tags: ${{ steps.meta.outputs.tags }} for tag in "${tag_array[@]}"; do
annotations: ${{ steps.meta.outputs.annotations }} tag_args+=( -t "$tag" )
done
if [ "${{ github.event_name }}" = "pull_request" ]; then
docker buildx build --platform linux/amd64,linux/arm64 "${tag_args[@]}" --load .
else
docker buildx build --platform linux/amd64,linux/arm64 "${tag_args[@]}" --push .
fi