chore(forgejo): try something new with the build workflow

This commit is contained in:
Tommy 2025-01-03 07:19:43 +01:00
parent c3c21547ea
commit 566e74674f
Signed by: tommy
SSH key fingerprint: SHA256:1LWgQT3QPHIT29plS8jjXc3S1FcE/4oGvsx3Efxs6Uc

View file

@ -5,25 +5,38 @@ on:
paths:
- 'apps/*/Dockerfile'
workflow_dispatch:
inputs:
dockerfile:
description: "Optional Dockerfile path to build. Example: 'apps/ci-os/Dockerfile'"
required: false
image:
description: "Optional container image name. Example: 'my-org/my-image:latest'"
required: false
jobs:
# 1) Find all Dockerfiles under apps/* and output them as JSON
# 1) Find all Dockerfiles under apps/* OR use the user-specified Dockerfile
discover-dockerfiles:
runs-on: ci-os
outputs:
dockerfiles: ${{ steps.set-matrix.outputs.dockerfiles }}
dockerfiles: ${{ steps.set-dockerfiles.outputs.dockerfiles }}
steps:
- name: Checkout
uses: actions/checkout@v4
- id: set-matrix
name: Find Dockerfiles
- id: set-dockerfiles
name: Resolve Dockerfiles
run: |
# Find all Dockerfiles in apps/* subdirectories
# If 'dockerfile' was provided via workflow_dispatch, use it directly:
if [ -n "${{ github.event.inputs.dockerfile }}" ]; then
echo "Single Dockerfile specified: ${{ github.event.inputs.dockerfile }}"
echo "dockerfiles=[\"${{ github.event.inputs.dockerfile }}\"]" >> "$GITHUB_OUTPUT"
exit 0
fi
# Otherwise, discover all Dockerfiles in apps/*:
files=$(find apps -mindepth 2 -maxdepth 2 -type f -name Dockerfile)
# If no Dockerfiles found, output an empty array to avoid parsing errors
# If no Dockerfiles found, output an empty array
if [ -z "$files" ]; then
echo 'dockerfiles=[]' >> "$GITHUB_OUTPUT"
exit 0
@ -34,10 +47,8 @@ jobs:
for f in $files; do
json="${json}\"$f\","
done
# Remove trailing comma and close array
json="${json%,}]"
# Write to job output
echo "dockerfiles=$json" >> "$GITHUB_OUTPUT"
# 2) For each Dockerfile discovered, build & push with Kaniko
@ -50,21 +61,28 @@ jobs:
dockerfile: ${{ fromJSON(needs.discover-dockerfiles.outputs.dockerfiles) }}
steps:
- name: Checkout Repository
- name: Checkout
uses: actions/checkout@v4
- id: set-image
name: Determine Image Name
run: |
# If the user provided a container image name, use it
if [ -n "${{ github.event.inputs.image }}" ]; then
echo "image=${{ github.event.inputs.image }}" >> "$GITHUB_OUTPUT"
else
# Otherwise parse from the Dockerfile path
# e.g. "apps/ci-os/Dockerfile" => "ci-os"
image=$(echo "${{ matrix.dockerfile }}" | sed 's|apps/||g' | sed 's|/Dockerfile||g')
echo "image=$image" >> "$GITHUB_OUTPUT"
fi
- name: Build and Push with Kaniko
uses: https://code.252.no/pub/kaniko-action@latest
with:
context: ./
dockerfile: ${{ matrix.dockerfile }}
destinations: >
code.252.no/${{ github.repository }}/${ { matrix.dockerfile
// remove `apps/`
// remove `/Dockerfile`
// e.g. "apps/ci-os/Dockerfile" => "ci-os"
// There's no built-in function to do this inline, so consider a real approach:
}}:latest
destinations: "code.252.no/${{ github.repository }}/${{ steps.set-image.outputs.image }}:latest"
credentials: "code.252.no=tommy:${{ secrets.REGISTRY_TOKEN }}"
push: "true"
cache: "false"