2024-12-30 20:50:06 +00:00
|
|
|
name: "Build and Push Images with Kaniko"
|
2024-11-03 20:28:20 +00:00
|
|
|
|
|
|
|
on:
|
|
|
|
push:
|
|
|
|
paths:
|
|
|
|
- 'apps/*/Dockerfile'
|
|
|
|
workflow_dispatch:
|
2025-01-03 06:19:43 +00:00
|
|
|
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
|
2024-12-30 20:50:06 +00:00
|
|
|
|
2024-11-03 20:28:20 +00:00
|
|
|
jobs:
|
2025-01-03 06:19:43 +00:00
|
|
|
# 1) Find all Dockerfiles under apps/* OR use the user-specified Dockerfile
|
2024-12-30 20:50:06 +00:00
|
|
|
discover-dockerfiles:
|
|
|
|
runs-on: ci-os
|
|
|
|
outputs:
|
2025-01-03 06:19:43 +00:00
|
|
|
dockerfiles: ${{ steps.set-dockerfiles.outputs.dockerfiles }}
|
2024-11-03 20:28:20 +00:00
|
|
|
steps:
|
2024-12-30 20:50:06 +00:00
|
|
|
- name: Checkout
|
2024-11-03 20:28:20 +00:00
|
|
|
uses: actions/checkout@v4
|
|
|
|
|
2025-01-03 06:19:43 +00:00
|
|
|
- id: set-dockerfiles
|
|
|
|
name: Resolve Dockerfiles
|
2024-11-03 20:28:20 +00:00
|
|
|
run: |
|
2025-01-03 06:19:43 +00:00
|
|
|
# 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/*:
|
2024-12-30 20:50:06 +00:00
|
|
|
files=$(find apps -mindepth 2 -maxdepth 2 -type f -name Dockerfile)
|
2024-11-03 20:28:20 +00:00
|
|
|
|
2025-01-03 06:19:43 +00:00
|
|
|
# If no Dockerfiles found, output an empty array
|
2024-12-30 20:50:06 +00:00
|
|
|
if [ -z "$files" ]; then
|
|
|
|
echo 'dockerfiles=[]' >> "$GITHUB_OUTPUT"
|
|
|
|
exit 0
|
|
|
|
fi
|
2024-11-03 20:28:20 +00:00
|
|
|
|
2024-12-30 20:50:06 +00:00
|
|
|
# Build a JSON array of Dockerfile paths
|
|
|
|
json="["
|
|
|
|
for f in $files; do
|
|
|
|
json="${json}\"$f\","
|
2024-11-03 20:28:20 +00:00
|
|
|
done
|
2024-12-30 20:50:06 +00:00
|
|
|
json="${json%,}]"
|
|
|
|
|
|
|
|
echo "dockerfiles=$json" >> "$GITHUB_OUTPUT"
|
|
|
|
|
|
|
|
# 2) For each Dockerfile discovered, build & push with Kaniko
|
|
|
|
build-and-push:
|
|
|
|
runs-on: ci-os
|
|
|
|
needs: discover-dockerfiles
|
|
|
|
strategy:
|
|
|
|
fail-fast: false
|
|
|
|
matrix:
|
|
|
|
dockerfile: ${{ fromJSON(needs.discover-dockerfiles.outputs.dockerfiles) }}
|
|
|
|
|
|
|
|
steps:
|
2025-01-03 06:19:43 +00:00
|
|
|
- name: Checkout
|
2024-12-30 20:50:06 +00:00
|
|
|
uses: actions/checkout@v4
|
|
|
|
|
2025-01-03 06:19:43 +00:00
|
|
|
- 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
|
|
|
|
|
2024-12-30 20:50:06 +00:00
|
|
|
- name: Build and Push with Kaniko
|
|
|
|
uses: https://code.252.no/pub/kaniko-action@latest
|
|
|
|
with:
|
|
|
|
context: ./
|
|
|
|
dockerfile: ${{ matrix.dockerfile }}
|
2025-01-03 06:19:43 +00:00
|
|
|
destinations: "code.252.no/${{ github.repository }}/${{ steps.set-image.outputs.image }}:latest"
|
2024-12-30 20:50:06 +00:00
|
|
|
credentials: "code.252.no=tommy:${{ secrets.REGISTRY_TOKEN }}"
|
|
|
|
push: "true"
|
|
|
|
cache: "false"
|
|
|
|
# cache_repo: "code.252.no/${{ github.repository }}/cache"
|