All checks were successful
CICD Start / Sanity and Base Decision (pull_request) Successful in 1m0s
Signed-off-by: copilotcoder <copilotcoder@darkhelm.org>
235 lines
8.5 KiB
YAML
235 lines
8.5 KiB
YAML
name: Docker Build Main
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
head_sha:
|
|
description: Commit SHA to process
|
|
required: false
|
|
base_hash:
|
|
description: Immutable base hash to use for CICD base image
|
|
required: false
|
|
source_workflow:
|
|
description: Upstream workflow name
|
|
required: false
|
|
base_needed:
|
|
description: Whether base rebuild was required
|
|
required: false
|
|
|
|
env:
|
|
GITEA_SSH_HOST: kankali.darkhelm.lan
|
|
GITEA_SSH_PORT: "2222"
|
|
GITEA_REPO_SSH_URL: ssh://git@kankali.darkhelm.lan:2222/DarkHelm.org/plex-playlist.git
|
|
GITEA_REGISTRY: kankali.darkhelm.lan:3001
|
|
GITEA_REGISTRY_IP: 10.18.75.2
|
|
GITEA_REGISTRY_HOST: kankali.darkhelm.lan
|
|
|
|
concurrency:
|
|
group: main-build-${{ github.sha }}
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
build:
|
|
name: Build and Push CICD Complete Image
|
|
runs-on: ubuntu-act-8gb
|
|
outputs:
|
|
head_sha: ${{ steps.meta.outputs.head_sha }}
|
|
|
|
steps:
|
|
- name: Audit trigger context
|
|
env:
|
|
EVENT_NAME: ${{ github.event_name }}
|
|
SOURCE_WORKFLOW: ${{ github.event.inputs.source_workflow }}
|
|
BASE_NEEDED: ${{ github.event.inputs.base_needed }}
|
|
HEAD_SHA: ${{ github.sha }}
|
|
BASE_HASH_INPUT: ${{ github.event.inputs.base_hash }}
|
|
REF: ${{ github.ref }}
|
|
REF_NAME: ${{ github.ref_name }}
|
|
HEAD_REF: ${{ github.head_ref }}
|
|
run: |
|
|
echo "=== Dispatch Audit: CICD Main Build ==="
|
|
echo "event_name=${EVENT_NAME}"
|
|
echo "source_workflow=${SOURCE_WORKFLOW}"
|
|
echo "base_needed=${BASE_NEEDED}"
|
|
echo "head_sha=${HEAD_SHA}"
|
|
echo "base_hash_input=${BASE_HASH_INPUT}"
|
|
echo "ref=${REF}"
|
|
echo "ref_name=${REF_NAME}"
|
|
echo "head_ref=${HEAD_REF}"
|
|
|
|
- name: Resolve head SHA
|
|
id: meta
|
|
env:
|
|
HEAD_SHA: ${{ github.sha }}
|
|
run: |
|
|
echo "head_sha=${HEAD_SHA}" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Minimal checkout for build inputs
|
|
env:
|
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
HEAD_SHA: ${{ steps.meta.outputs.head_sha }}
|
|
run: |
|
|
set -e
|
|
umask 077
|
|
trap 'rm -f ~/.ssh/id_rsa' EXIT
|
|
|
|
if ! grep -q "${GITEA_REGISTRY_HOST}" /etc/hosts; then
|
|
echo "${GITEA_REGISTRY_IP} ${GITEA_REGISTRY_HOST}" >> /etc/hosts
|
|
fi
|
|
|
|
mkdir -p ~/.ssh
|
|
echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa
|
|
chmod 600 ~/.ssh/id_rsa
|
|
ssh-keyscan -p "${GITEA_SSH_PORT}" "${GITEA_SSH_HOST}" >> ~/.ssh/known_hosts 2>/dev/null
|
|
|
|
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa -o IdentitiesOnly=yes -o StrictHostKeyChecking=no" \
|
|
git clone --depth 1 --no-checkout "${GITEA_REPO_SSH_URL}" .
|
|
|
|
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa -o IdentitiesOnly=yes -o StrictHostKeyChecking=no" \
|
|
git fetch --depth 1 origin "${HEAD_SHA}" >/dev/null 2>&1 || true
|
|
|
|
git checkout FETCH_HEAD -- Dockerfile.cicd Dockerfile.cicd-base .dockerignore scripts/compute-cicd-base-hash.sh
|
|
chmod +x scripts/compute-cicd-base-hash.sh
|
|
|
|
- name: Build and push complete CICD image
|
|
env:
|
|
PACKAGE_ACCESS_TOKEN: ${{ secrets.PACKAGE_ACCESS_TOKEN }}
|
|
REGISTRY_USER: ${{ github.actor }}
|
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
HEAD_SHA: ${{ steps.meta.outputs.head_sha }}
|
|
BASE_HASH_INPUT: ${{ github.event.inputs.base_hash }}
|
|
run: |
|
|
set -e
|
|
umask 077
|
|
trap 'rm -f /tmp/ssh_key' EXIT
|
|
|
|
if ! grep -q "${GITEA_REGISTRY_HOST}" /etc/hosts; then
|
|
echo "${GITEA_REGISTRY_IP} ${GITEA_REGISTRY_HOST}" >> /etc/hosts
|
|
fi
|
|
|
|
echo "${PACKAGE_ACCESS_TOKEN}" | docker login "http://${GITEA_REGISTRY}" -u "${REGISTRY_USER}" --password-stdin
|
|
|
|
if [ -n "${BASE_HASH_INPUT}" ]; then
|
|
BASE_HASH="${BASE_HASH_INPUT}"
|
|
echo "Using provided immutable base hash from upstream workflow dispatch: ${BASE_HASH}"
|
|
else
|
|
BASE_HASH=$(./scripts/compute-cicd-base-hash.sh)
|
|
echo "No base_hash input provided; computed base hash locally: ${BASE_HASH}"
|
|
fi
|
|
|
|
BASE_IMAGE="${GITEA_REGISTRY}/darkhelm.org/plex-playlist-cicd-base:${BASE_HASH}"
|
|
|
|
verify_base_image() {
|
|
# Fast path: base image already present locally.
|
|
if docker image inspect "${BASE_IMAGE}" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
|
|
# Manifest inspect unreliable on these runners; use pull as truth.
|
|
timeout 1800 docker pull "${BASE_IMAGE}" >/tmp/base-image-pull.log 2>&1
|
|
}
|
|
|
|
max_attempts=2
|
|
sleep_seconds=15
|
|
for i in $(seq 1 "${max_attempts}"); do
|
|
echo "Base availability check ${i}/${max_attempts}..."
|
|
if verify_base_image; then
|
|
echo "✓ Base image available and hash matched: ${BASE_IMAGE}"
|
|
break
|
|
else
|
|
pull_exit=$?
|
|
echo "Base pull attempt ${i} failed with exit code ${pull_exit}"
|
|
if [ "${pull_exit}" -eq 124 ]; then
|
|
echo "Pull timed out after 1800s while downloading base image"
|
|
fi
|
|
if [ -s /tmp/base-image-pull.log ]; then
|
|
tail -n 40 /tmp/base-image-pull.log || true
|
|
fi
|
|
|
|
if [ "${i}" -eq "${max_attempts}" ]; then
|
|
echo "❌ Required immutable base image is not available or mismatched: ${BASE_IMAGE}"
|
|
exit 1
|
|
fi
|
|
sleep "${sleep_seconds}"
|
|
fi
|
|
done
|
|
|
|
echo "✓ Base image ready: ${BASE_IMAGE}"
|
|
|
|
echo "${SSH_PRIVATE_KEY}" > /tmp/ssh_key
|
|
chmod 600 /tmp/ssh_key
|
|
export DOCKER_BUILDKIT=1
|
|
|
|
docker build -f Dockerfile.cicd \
|
|
--secret id=ssh_private_key,src=/tmp/ssh_key \
|
|
--build-arg GITHUB_SHA="${HEAD_SHA}" \
|
|
--build-arg CICD_BASE_IMAGE="${BASE_IMAGE}" \
|
|
-t cicd:latest .
|
|
|
|
docker tag cicd:latest "${GITEA_REGISTRY}/darkhelm.org/plex-playlist-cicd:latest"
|
|
docker tag cicd:latest "${GITEA_REGISTRY}/darkhelm.org/plex-playlist-cicd:${HEAD_SHA}"
|
|
|
|
docker push "${GITEA_REGISTRY}/darkhelm.org/plex-playlist-cicd:latest"
|
|
docker push "${GITEA_REGISTRY}/darkhelm.org/plex-playlist-cicd:${HEAD_SHA}"
|
|
|
|
dispatch-checks:
|
|
name: Dispatch CICD Checks
|
|
runs-on: ubuntu-act
|
|
needs: build
|
|
if: needs.build.result == 'success'
|
|
steps:
|
|
- name: Dispatch checks workflow
|
|
env:
|
|
ACTIONS_TRIGGER_TOKEN: ${{ secrets.ACTIONS_TRIGGER_TOKEN }}
|
|
PACKAGE_ACCESS_TOKEN: ${{ secrets.PACKAGE_ACCESS_TOKEN }}
|
|
HEAD_SHA: ${{ needs.build.outputs.head_sha }}
|
|
REPO_FULL: ${{ github.repository }}
|
|
HEAD_REF: ${{ github.head_ref }}
|
|
REF_NAME: ${{ github.ref_name }}
|
|
run: |
|
|
set -e
|
|
|
|
DISPATCH_TOKEN="${ACTIONS_TRIGGER_TOKEN:-${PACKAGE_ACCESS_TOKEN:-}}"
|
|
|
|
if [ -z "${DISPATCH_TOKEN}" ]; then
|
|
echo "❌ Missing dispatch token. Set ACTIONS_TRIGGER_TOKEN (repo write scope) or ensure PACKAGE_ACCESS_TOKEN has Actions workflow-dispatch permissions."
|
|
exit 1
|
|
fi
|
|
|
|
if ! grep -q "${GITEA_REGISTRY_HOST}" /etc/hosts; then
|
|
echo "${GITEA_REGISTRY_IP} ${GITEA_REGISTRY_HOST}" >> /etc/hosts
|
|
fi
|
|
|
|
API_BASE="http://${GITEA_REGISTRY_IP}:3001/api/v1"
|
|
REPO_OWNER="${REPO_FULL%/*}"
|
|
REPO_NAME="${REPO_FULL#*/}"
|
|
TARGET_REF="${HEAD_REF:-${REF_NAME}}"
|
|
|
|
payload=$(cat <<EOF
|
|
{
|
|
"ref": "${TARGET_REF}",
|
|
"inputs": {
|
|
"head_sha": "${HEAD_SHA}",
|
|
"source_workflow": "CICD Main Build"
|
|
}
|
|
}
|
|
EOF
|
|
)
|
|
|
|
DISPATCH_URL="${API_BASE}/repos/${REPO_OWNER}/${REPO_NAME}/actions/workflows/cicd-checks.yaml/dispatches"
|
|
RESPONSE_FILE="/tmp/dispatch-response.txt"
|
|
: > "${RESPONSE_FILE}"
|
|
HTTP_CODE=$(curl -sS --connect-timeout 5 --max-time 20 -o "${RESPONSE_FILE}" -w "%{http_code}" -X POST \
|
|
-H "Authorization: token ${DISPATCH_TOKEN}" \
|
|
-H "Content-Type: application/json" \
|
|
"${DISPATCH_URL}" \
|
|
-d "${payload}" || true)
|
|
|
|
if [ "${HTTP_CODE}" -lt 200 ] || [ "${HTTP_CODE}" -ge 300 ]; then
|
|
echo "❌ Dispatch failed with HTTP ${HTTP_CODE}"
|
|
echo "Dispatch URL: ${DISPATCH_URL}"
|
|
echo "Response body:"
|
|
cat "${RESPONSE_FILE}" || true
|
|
exit 1
|
|
fi
|