chore: extract pipeline flag resolution

This commit is contained in:
Haitao Pan 2026-04-10 20:35:20 +08:00
parent ddf0fd827e
commit 7b7d045cd9
2 changed files with 95 additions and 76 deletions

View File

@ -63,82 +63,25 @@ jobs:
- name: Resolve Pipeline Flags
id: flags
run: |
set -euo pipefail
base_images_exists=false
run_base_images=false
push_base_images=false
push_image=true
push_latest=false
image_tag=""
base_image_registry="ghcr.io"
base_image_org="${{ vars.IMAGE_REPO_OWNER || github.repository_owner }}"
dockerhub_namespace="${{ vars.DOCKERHUB_NAMESPACE || 'cloudneutral' }}"
target_host="${DEFAULT_TARGET_HOST}"
run_apply=true
if [[ -d deploy/base-images ]] && find deploy/base-images -type f | grep -q .; then
base_images_exists=true
fi
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
target_host="${{ inputs.target_host }}"
[[ "${{ inputs.run_apply }}" == "true" ]] && run_apply=true || run_apply=false
image_tag="${{ inputs.image_tag }}"
[[ "${{ inputs.push_image }}" == "true" ]] && push_image=true || push_image=false
[[ "${{ inputs.push_latest }}" == "true" ]] && push_latest=true || push_latest=false
[[ "${{ inputs.run_base_images }}" == "true" ]] && run_base_images=true || run_base_images=false
[[ "${{ inputs.push_base_images }}" == "true" ]] && push_base_images=true || push_base_images=false
base_image_registry="${{ inputs.base_image_registry }}"
base_image_org="${{ inputs.base_image_org }}"
dockerhub_namespace="${{ inputs.dockerhub_namespace }}"
if [[ "${base_images_exists}" != "true" ]]; then
run_base_images=false
push_base_images=false
fi
else
if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
push_image=false
fi
if [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then
push_latest=true
fi
if [[ "${base_images_exists}" == "true" ]]; then
if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
base_ref="${{ github.event.pull_request.base.sha }}"
head_ref="${{ github.event.pull_request.head.sha }}"
else
base_ref="${{ github.event.before }}"
head_ref="${{ github.sha }}"
fi
if [[ -n "${base_ref}" && "${base_ref}" != "0000000000000000000000000000000000000000" ]]; then
if git diff --name-only "${base_ref}" "${head_ref}" | grep -q '^deploy/base-images/'; then
run_base_images=true
if [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then
push_base_images=true
fi
fi
fi
fi
fi
{
echo "base_images_exists=${base_images_exists}"
echo "run_base_images=${run_base_images}"
echo "push_base_images=${push_base_images}"
echo "base_image_registry=${base_image_registry}"
echo "base_image_org=${base_image_org}"
echo "dockerhub_namespace=${dockerhub_namespace}"
echo "target_host=${target_host}"
echo "run_apply=${run_apply}"
echo "image_tag=${image_tag}"
echo "push_image=${push_image}"
echo "push_latest=${push_latest}"
} >> "$GITHUB_OUTPUT"
env:
IMAGE_REPO_OWNER: ${{ vars.IMAGE_REPO_OWNER || github.repository_owner }}
DEFAULT_TARGET_HOST: ${{ env.DEFAULT_TARGET_HOST }}
DOCKERHUB_NAMESPACE: ${{ vars.DOCKERHUB_NAMESPACE || 'cloudneutral' }}
INPUT_TARGET_HOST: ${{ inputs.target_host }}
INPUT_RUN_APPLY: ${{ inputs.run_apply }}
INPUT_IMAGE_TAG: ${{ inputs.image_tag }}
INPUT_PUSH_IMAGE: ${{ inputs.push_image }}
INPUT_PUSH_LATEST: ${{ inputs.push_latest }}
INPUT_RUN_BASE_IMAGES: ${{ inputs.run_base_images }}
INPUT_PUSH_BASE_IMAGES: ${{ inputs.push_base_images }}
INPUT_BASE_IMAGE_REGISTRY: ${{ inputs.base_image_registry }}
INPUT_BASE_IMAGE_ORG: ${{ inputs.base_image_org }}
INPUT_DOCKERHUB_NAMESPACE: ${{ inputs.dockerhub_namespace }}
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
GITHUB_BEFORE: ${{ github.event.before }}
GITHUB_SHA: ${{ github.sha }}
run: bash ./scripts/github-actions/resolve-pipeline-flags.sh >> "$GITHUB_OUTPUT"
build:
name: Build

View File

@ -0,0 +1,76 @@
#!/usr/bin/env bash
set -euo pipefail
BASE_IMAGES_EXISTS=false
RUN_BASE_IMAGES=false
PUSH_BASE_IMAGES=false
PUSH_IMAGE=true
PUSH_LATEST=false
IMAGE_TAG=""
BASE_IMAGE_REGISTRY="ghcr.io"
BASE_IMAGE_ORG="${IMAGE_REPO_OWNER:-${GITHUB_REPOSITORY_OWNER:-}}"
DOCKERHUB_NAMESPACE="${DOCKERHUB_NAMESPACE:-cloudneutral}"
TARGET_HOST="${DEFAULT_TARGET_HOST:?DEFAULT_TARGET_HOST is required}"
RUN_APPLY=true
if [[ -d deploy/base-images ]] && find deploy/base-images -type f | grep -q .; then
BASE_IMAGES_EXISTS=true
fi
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
TARGET_HOST="${INPUT_TARGET_HOST:-${TARGET_HOST}}"
[[ "${INPUT_RUN_APPLY:-true}" == "true" ]] && RUN_APPLY=true || RUN_APPLY=false
IMAGE_TAG="${INPUT_IMAGE_TAG:-}"
[[ "${INPUT_PUSH_IMAGE:-true}" == "true" ]] && PUSH_IMAGE=true || PUSH_IMAGE=false
[[ "${INPUT_PUSH_LATEST:-false}" == "true" ]] && PUSH_LATEST=true || PUSH_LATEST=false
[[ "${INPUT_RUN_BASE_IMAGES:-false}" == "true" ]] && RUN_BASE_IMAGES=true || RUN_BASE_IMAGES=false
[[ "${INPUT_PUSH_BASE_IMAGES:-true}" == "true" ]] && PUSH_BASE_IMAGES=true || PUSH_BASE_IMAGES=false
BASE_IMAGE_REGISTRY="${INPUT_BASE_IMAGE_REGISTRY:-${BASE_IMAGE_REGISTRY}}"
BASE_IMAGE_ORG="${INPUT_BASE_IMAGE_ORG:-${BASE_IMAGE_ORG}}"
DOCKERHUB_NAMESPACE="${INPUT_DOCKERHUB_NAMESPACE:-${DOCKERHUB_NAMESPACE}}"
if [[ "${BASE_IMAGES_EXISTS}" != "true" ]]; then
RUN_BASE_IMAGES=false
PUSH_BASE_IMAGES=false
fi
else
if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
PUSH_IMAGE=false
fi
if [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then
PUSH_LATEST=true
fi
if [[ "${BASE_IMAGES_EXISTS}" == "true" ]]; then
if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
base_ref="${PR_BASE_SHA:-}"
head_ref="${PR_HEAD_SHA:-}"
else
base_ref="${GITHUB_BEFORE:-}"
head_ref="${GITHUB_SHA:-}"
fi
if [[ -n "${base_ref}" && "${base_ref}" != "0000000000000000000000000000000000000000" ]]; then
if git diff --name-only "${base_ref}" "${head_ref}" | grep -q '^deploy/base-images/'; then
RUN_BASE_IMAGES=true
if [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then
PUSH_BASE_IMAGES=true
fi
fi
fi
fi
fi
cat <<EOF
base_images_exists=${BASE_IMAGES_EXISTS}
run_base_images=${RUN_BASE_IMAGES}
push_base_images=${PUSH_BASE_IMAGES}
base_image_registry=${BASE_IMAGE_REGISTRY}
base_image_org=${BASE_IMAGE_ORG}
dockerhub_namespace=${DOCKERHUB_NAMESPACE}
target_host=${TARGET_HOST}
run_apply=${RUN_APPLY}
image_tag=${IMAGE_TAG}
push_image=${PUSH_IMAGE}
push_latest=${PUSH_LATEST}
EOF