swap matrix approach for loop
This commit is contained in:
151
.github/workflows/build.yml
vendored
151
.github/workflows/build.yml
vendored
@@ -36,94 +36,89 @@ jobs:
|
|||||||
# Filter to only directories that contain a Dockerfile (real lanes)
|
# Filter to only directories that contain a Dockerfile (real lanes)
|
||||||
FILTERED_LANES=()
|
FILTERED_LANES=()
|
||||||
SKIPPED_LANES=()
|
SKIPPED_LANES=()
|
||||||
for _d in "${ALL_LANES[@]}"; do
|
build:
|
||||||
if [[ -f "${_d}/Dockerfile" ]]; then
|
needs: plan
|
||||||
FILTERED_LANES+=("${_d}")
|
if: needs.plan.outputs.should_build == 'true'
|
||||||
else
|
runs-on: ubuntu-latest
|
||||||
SKIPPED_LANES+=("${_d}")
|
steps:
|
||||||
fi
|
- uses: actions/checkout@v4
|
||||||
done
|
|
||||||
ALL_LANES=("${FILTERED_LANES[@]}")
|
|
||||||
|
|
||||||
# Also capture lane basenames (e.g. '7.4', 'nginx') for stable naming
|
- uses: docker/setup-buildx-action@v3
|
||||||
ALL_NAMES=()
|
|
||||||
for _d in "${ALL_LANES[@]}"; do
|
|
||||||
ALL_NAMES+=("$(basename "${_d}")")
|
|
||||||
done
|
|
||||||
if [[ ${#ALL_LANES[@]} -eq 0 ]]; then
|
|
||||||
echo "No lanes found under docker/. Nothing to do."
|
|
||||||
echo "should_build=false" >> $GITHUB_OUTPUT
|
|
||||||
echo 'matrix={"dir":[]}' >> $GITHUB_OUTPUT
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Determine changed files (PR vs push vs manual dispatch)
|
- name: DEBUG registry username source
|
||||||
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
|
run: |
|
||||||
BASE_SHA="${{ github.event.pull_request.base.sha }}"
|
echo "Selected registry username source: $SOURCE"
|
||||||
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
|
env:
|
||||||
elif [[ "${{ github.event_name }}" == "push" ]]; then
|
SOURCE: ${{ inputs.REGISTRY_USER != '' && 'inputs' || secrets.REGISTRY_USER != '' && 'secrets' || vars.REGISTRY_USER != '' && 'vars' || 'actor' }}
|
||||||
# For push events try to get the previous commit; fall back to empty
|
|
||||||
BASE_SHA="$(git rev-parse HEAD~1 2>/dev/null || true)"
|
|
||||||
HEAD_SHA="$(git rev-parse HEAD 2>/dev/null || true)"
|
|
||||||
else
|
|
||||||
# For workflow_dispatch and other events treat as "all files changed"
|
|
||||||
BASE_SHA=""
|
|
||||||
HEAD_SHA="$(git rev-parse HEAD 2>/dev/null || true)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "$BASE_SHA" ]]; then
|
- uses: docker/login-action@v3
|
||||||
CHANGED=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA")
|
with:
|
||||||
else
|
registry: gitea.auvem.com
|
||||||
# First commit or shallow: treat everything as changed
|
username: ${{ inputs.REGISTRY_USER != '' && inputs.REGISTRY_USER || secrets.REGISTRY_USER != '' && secrets.REGISTRY_USER || vars.REGISTRY_USER != '' && vars.REGISTRY_USER || github.actor }}
|
||||||
CHANGED=$(git ls-files)
|
password: ${{ secrets.REGISTRY_TOKEN }}
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Changed files:"
|
- name: Show plan matrix
|
||||||
echo "$CHANGED"
|
run: |
|
||||||
|
echo "Plan matrix: $MATRIX_JSON"
|
||||||
|
jq -C . dir <<< "$MATRIX_JSON" 2>/dev/null || true
|
||||||
|
env:
|
||||||
|
MATRIX_JSON: ${{ needs.plan.outputs.matrix }}
|
||||||
|
|
||||||
# If workflow changed, rebuild all lanes (use basenames)
|
- name: Loop: build and push per-lane
|
||||||
if grep -qx ".github/workflows/build.yml" <<< "$CHANGED"; then
|
shell: bash
|
||||||
echo "Workflow changed; rebuilding all lanes."
|
env:
|
||||||
# Use full lane paths (e.g. 'docker/7.4') so downstream steps get deterministic dirs
|
MATRIX_JSON: ${{ needs.plan.outputs.matrix }}
|
||||||
TARGET_DIRS=("${ALL_LANES[@]}")
|
GIT_SHA_SHORT: ${GITHUB_SHA::7}
|
||||||
else
|
run: |
|
||||||
# Build only lanes with changes under their directories
|
set -euo pipefail
|
||||||
TARGET_DIRS=()
|
|
||||||
for idx in "${!ALL_LANES[@]}"; do
|
|
||||||
lane_path="${ALL_LANES[$idx]}"
|
|
||||||
lane_name="${ALL_NAMES[$idx]}"
|
|
||||||
# Any change directly under lane dir counts; include Dockerfile or subpaths
|
|
||||||
if grep -q "^${lane_path}/" <<< "$CHANGED"; then
|
|
||||||
# Append the full lane path (e.g. 'docker/7.4')
|
|
||||||
TARGET_DIRS+=("${lane_path}")
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
# De-duplicate
|
if [[ -z "$MATRIX_JSON" ]]; then
|
||||||
mapfile -t TARGET_DIRS < <(printf "%s\n" "${TARGET_DIRS[@]}" | awk 'NF && !x[$0]++')
|
echo "ERROR: plan matrix is empty. Aborting." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Diagnostics: show what we will include in the matrix
|
# Iterate lanes
|
||||||
echo "ALL_LANES (discovered):"
|
lanes=$(jq -r '.dir[]' <<< "$MATRIX_JSON")
|
||||||
for i in "${!ALL_LANES[@]}"; do
|
echo "Lanes to build:"
|
||||||
printf " [%d] '%s'\n" "$i" "${ALL_LANES[$i]}"
|
echo "$lanes"
|
||||||
done
|
|
||||||
|
|
||||||
echo "TARGET_DIRS (after selection & dedupe):"
|
for lane in $lanes; do
|
||||||
for i in "${!TARGET_DIRS[@]}"; do
|
echo "\n--- Building lane: $lane ---"
|
||||||
# Show non-printable/empty clearly
|
if [[ ! -f "$lane/Dockerfile" ]]; then
|
||||||
printf " [%d] '%s' (len=%d)\n" "$i" "${TARGET_DIRS[$i]}" "${#TARGET_DIRS[$i]}"
|
echo "ERROR: No Dockerfile at $lane/Dockerfile" >&2
|
||||||
done
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Fail if any empty entries sneaked in; that would cause ambiguous matrix entries
|
NAME=$(basename "$lane")
|
||||||
for val in "${TARGET_DIRS[@]}"; do
|
|
||||||
if [[ -z "${val}" ]]; then
|
|
||||||
echo "ERROR: Computed target dirs contains an empty entry. Aborting to avoid ambiguous matrix." >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [[ ${#TARGET_DIRS[@]} -eq 0 ]]; then
|
if [[ "$NAME" == "nginx" ]]; then
|
||||||
|
IMAGE="gitea.auvem.com/auvem/wordpress-docker/nginx"
|
||||||
|
TAG="stable"
|
||||||
|
else
|
||||||
|
IMAGE="gitea.auvem.com/auvem/wordpress-docker/php-fpm"
|
||||||
|
if [[ "$NAME" =~ ^([0-9]+\.[0-9]+)$ ]]; then
|
||||||
|
VERSION="${BASH_REMATCH[1]}"
|
||||||
|
TAG="${VERSION}-stable"
|
||||||
|
elif [[ "$NAME" =~ ^([0-9]+)$ ]]; then
|
||||||
|
VERSION="${BASH_REMATCH[1]}"
|
||||||
|
TAG="${VERSION}-stable"
|
||||||
|
else
|
||||||
|
echo "ERROR: Cannot deterministically derive a version tag from lane name '$NAME'." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Computed: lane='$lane' name='$NAME' image='$IMAGE' tags='$IMAGE:${TAG},$IMAGE:git-${GIT_SHA_SHORT}'"
|
||||||
|
|
||||||
|
# Buildx build and push
|
||||||
|
docker buildx build \
|
||||||
|
--push \
|
||||||
|
--platform linux/amd64 \
|
||||||
|
--tag "$IMAGE:${TAG}" \
|
||||||
|
--tag "$IMAGE:git-${GIT_SHA_SHORT}" \
|
||||||
|
--file "$lane/Dockerfile" \
|
||||||
|
.
|
||||||
|
done
|
||||||
echo "No lane directories changed. Skipping build."
|
echo "No lane directories changed. Skipping build."
|
||||||
echo "should_build=false" >> $GITHUB_OUTPUT
|
echo "should_build=false" >> $GITHUB_OUTPUT
|
||||||
echo 'matrix={"dir":[]}' >> $GITHUB_OUTPUT
|
echo 'matrix={"dir":[]}' >> $GITHUB_OUTPUT
|
||||||
|
|||||||
Reference in New Issue
Block a user