116 lines
3.9 KiB
YAML
116 lines
3.9 KiB
YAML
name: Build Any Repository Docker Image
|
||
|
||
on:
|
||
workflow_dispatch:
|
||
inputs:
|
||
repo_url:
|
||
description: '要构建的仓库地址(如:xc-workflows/engine-demo)'
|
||
required: true
|
||
type: string
|
||
ref:
|
||
description: '分支/tag/commit'
|
||
required: true
|
||
default: 'main'
|
||
type: string
|
||
dry_run:
|
||
description: '仅构建不推送'
|
||
required: false
|
||
default: 'true'
|
||
type: choice
|
||
options:
|
||
- 'true'
|
||
- 'false'
|
||
|
||
jobs:
|
||
build:
|
||
runs-on: amd64-ubuntu-24.04
|
||
|
||
steps:
|
||
- name: Clone target repository
|
||
run: |
|
||
# 构建完整的 clone URL
|
||
CLONE_URL="https://${{ gitea.server_url }}/${{ github.event.inputs.repo_url }}.git"
|
||
echo "正在克隆: $CLONE_URL"
|
||
|
||
git clone "$CLONE_URL" target_repo
|
||
cd target_repo
|
||
git checkout "${{ github.event.inputs.ref }}"
|
||
|
||
# 保存仓库信息供后续步骤使用
|
||
echo "TARGET_REPO_PATH=$(pwd)" >> "$GITEA_ENV"
|
||
echo "TARGET_REPO_NAME=$(basename ${{ github.event.inputs.repo_url }})" >> "$GITEA_ENV"
|
||
|
||
- name: Set image metadata
|
||
run: |
|
||
cd target_repo
|
||
|
||
# 生成镜像名称(将仓库路径中的 / 替换为 -)
|
||
IMAGE_NAME="$(echo "${{ github.event.inputs.repo_url }}" | tr '[:upper:]' '[:lower:]' | tr '/' '-')"
|
||
SAFE_TAG=$(echo "${{ github.event.inputs.ref }}" | sed 's/\//-/g')
|
||
IMAGE="${DOCKER_REGISTRY}/${DOCKER_USERNAME}/${IMAGE_NAME}:${SAFE_TAG}"
|
||
|
||
echo "IMAGE_NAME=${IMAGE_NAME}" >> "$GITEA_ENV"
|
||
echo "IMAGE=${IMAGE}" >> "$GITEA_ENV"
|
||
echo "构建目标镜像: ${IMAGE}"
|
||
|
||
- name: Login to Docker Registry
|
||
if: github.event.inputs.dry_run != 'true'
|
||
run: |
|
||
echo "$DOCKER_PASSWORD" | docker login "$DOCKER_REGISTRY" \
|
||
-u "$DOCKER_USERNAME" \
|
||
--password-stdin
|
||
|
||
- name: Build Docker Image
|
||
run: |
|
||
cd target_repo
|
||
echo "开始构建镜像: ${IMAGE}"
|
||
docker build -t "${IMAGE}" .
|
||
echo "镜像构建完成"
|
||
|
||
- name: Push Docker Image
|
||
if: github.event.inputs.dry_run != 'true'
|
||
run: |
|
||
for attempt in 1 2 3; do
|
||
echo "Starting docker push attempt ${attempt}/3 for ${IMAGE}"
|
||
docker push "${IMAGE}" &
|
||
PUSH_PID=$!
|
||
|
||
while kill -0 "$PUSH_PID" 2>/dev/null; do
|
||
echo "docker push is still running at $(date -u '+%Y-%m-%dT%H:%M:%SZ')"
|
||
sleep 60
|
||
done
|
||
|
||
if wait "$PUSH_PID"; then
|
||
echo "docker push completed successfully"
|
||
exit 0
|
||
fi
|
||
|
||
echo "docker push failed on attempt ${attempt}/3"
|
||
sleep 30
|
||
done
|
||
|
||
echo "docker push failed after 3 attempts"
|
||
exit 1
|
||
|
||
- name: Dry Run Summary
|
||
if: github.event.inputs.dry_run == 'true'
|
||
run: |
|
||
echo "=========================================="
|
||
echo "✅ 仅构建模式完成(未推送)"
|
||
echo "=========================================="
|
||
echo "源仓库: ${{ github.event.inputs.repo_url }}"
|
||
echo "分支/Tag: ${{ github.event.inputs.ref }}"
|
||
echo "镜像名称: ${IMAGE}"
|
||
echo "=========================================="
|
||
|
||
- name: Full Build Summary
|
||
if: github.event.inputs.dry_run != 'true'
|
||
run: |
|
||
echo "=========================================="
|
||
echo "✅ 完整构建完成(已推送)"
|
||
echo "=========================================="
|
||
echo "源仓库: ${{ github.event.inputs.repo_url }}"
|
||
echo "分支/Tag: ${{ github.event.inputs.ref }}"
|
||
echo "镜像名称: ${IMAGE}"
|
||
echo "=========================================="
|