2026-10-21 12:21:45 +08:00
|
|
|
|
# action.yml
|
|
|
|
|
|
name: "magic docker build and push"
|
2026-10-21 12:38:52 +08:00
|
|
|
|
description: "一键构建并推送 Docker 镜像:支持自定义 context / dockerfile,或使用内置 preset(vue3、vue3-hash、dotnet10)"
|
2026-10-21 12:21:45 +08:00
|
|
|
|
|
|
|
|
|
|
inputs:
|
|
|
|
|
|
registry:
|
|
|
|
|
|
description: "Docker registry URL"
|
|
|
|
|
|
required: true
|
|
|
|
|
|
default: "${{ vars.DOCKER_REGISTRY }}"
|
|
|
|
|
|
user:
|
|
|
|
|
|
description: "Docker registry username"
|
|
|
|
|
|
required: true
|
|
|
|
|
|
default: "${{ vars.DOCKER_USER }}"
|
|
|
|
|
|
pass:
|
|
|
|
|
|
description: "Docker registry password"
|
|
|
|
|
|
required: true
|
|
|
|
|
|
default: "${{ vars.DOCKER_PASS }}"
|
|
|
|
|
|
image:
|
2026-10-21 12:30:04 +08:00
|
|
|
|
description: "Docker image name,不含 registry(最终会拼成 <registry>/<image>:<tag>)"
|
2026-10-21 12:21:45 +08:00
|
|
|
|
required: true
|
|
|
|
|
|
default: "${{ github.repository }}"
|
|
|
|
|
|
tag:
|
|
|
|
|
|
description: "Docker image tag (default: github.ref_name)"
|
|
|
|
|
|
required: false
|
|
|
|
|
|
default: "${{ github.ref_name }}"
|
|
|
|
|
|
platform:
|
|
|
|
|
|
description: "Build platform (default: linux/amd64)"
|
|
|
|
|
|
required: false
|
|
|
|
|
|
default: "linux/amd64"
|
|
|
|
|
|
context:
|
|
|
|
|
|
description: "构建目录,相对仓库根目录(默认 .)"
|
|
|
|
|
|
required: false
|
|
|
|
|
|
default: "."
|
|
|
|
|
|
dockerfile:
|
|
|
|
|
|
description: "Dockerfile 的完整路径,相对仓库根目录或绝对路径。设置后忽略 preset(默认留空)"
|
|
|
|
|
|
required: false
|
|
|
|
|
|
default: ""
|
|
|
|
|
|
preset:
|
2026-10-21 12:38:52 +08:00
|
|
|
|
description: "内置方案:vue3 / vue3-hash / dotnet10。未设置 dockerfile 时生效(默认留空)"
|
2026-10-21 12:21:45 +08:00
|
|
|
|
required: false
|
|
|
|
|
|
default: ""
|
|
|
|
|
|
|
|
|
|
|
|
runs:
|
|
|
|
|
|
using: "composite"
|
|
|
|
|
|
steps:
|
|
|
|
|
|
# 编译、nginx 配置都在 preset 的 Dockerfile 里(多阶段构建)
|
|
|
|
|
|
# 优先级:dockerfile > preset > context 下的默认 Dockerfile
|
|
|
|
|
|
- name: build docker image and push
|
|
|
|
|
|
uses: docker://docker:git
|
|
|
|
|
|
env:
|
|
|
|
|
|
DOCKER_BUILDKIT: 1
|
|
|
|
|
|
REGISTRY: ${{ inputs.registry }}
|
|
|
|
|
|
REG_USER: ${{ inputs.user }}
|
|
|
|
|
|
REG_PASS: ${{ inputs.pass }}
|
|
|
|
|
|
IMAGE: ${{ inputs.image }}
|
|
|
|
|
|
TAG: ${{ inputs.tag }}
|
|
|
|
|
|
PLATFORM: ${{ inputs.platform }}
|
|
|
|
|
|
CONTEXT_DIR: ${{ inputs.context }}
|
|
|
|
|
|
DOCKERFILE: ${{ inputs.dockerfile }}
|
|
|
|
|
|
PRESET: ${{ inputs.preset }}
|
|
|
|
|
|
ACTION_PATH: ${{ github.action_path }}
|
|
|
|
|
|
with:
|
|
|
|
|
|
entrypoint: "/bin/sh"
|
|
|
|
|
|
args: |
|
|
|
|
|
|
-c '
|
|
|
|
|
|
set -e
|
|
|
|
|
|
image="$REGISTRY/$IMAGE:$TAG"
|
|
|
|
|
|
dockerfile_path="${DOCKERFILE:-}"
|
|
|
|
|
|
preset="${PRESET:-}"
|
|
|
|
|
|
build_args=""
|
|
|
|
|
|
|
|
|
|
|
|
if [ -n "$dockerfile_path" ]; then
|
|
|
|
|
|
if [ -n "$preset" ]; then
|
|
|
|
|
|
echo "已指定 dockerfile($dockerfile_path),忽略 preset($preset)"
|
|
|
|
|
|
fi
|
|
|
|
|
|
elif [ -n "$preset" ]; then
|
|
|
|
|
|
dockerfile_path="$ACTION_PATH/$preset.Dockerfile"
|
|
|
|
|
|
if [ ! -f "$dockerfile_path" ]; then
|
2026-10-21 12:38:52 +08:00
|
|
|
|
echo "未知 preset:$preset(可选:vue3 / vue3-hash / dotnet10)"
|
2026-10-21 12:21:45 +08:00
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
|
|
|
|
|
# preset 的 Dockerfile 用 VERSION 覆盖 package.json 版本 / dotnet Version
|
|
|
|
|
|
build_args="VERSION=$TAG"
|
|
|
|
|
|
echo "使用 preset:$preset"
|
|
|
|
|
|
else
|
|
|
|
|
|
dockerfile_path="$CONTEXT_DIR/Dockerfile"
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
echo "构建目录:$CONTEXT_DIR"
|
|
|
|
|
|
echo "Dockerfile:$dockerfile_path"
|
|
|
|
|
|
echo "镜像:$image"
|
|
|
|
|
|
|
|
|
|
|
|
set -- --platform "$PLATFORM" -t "$image" -f "$dockerfile_path"
|
|
|
|
|
|
if [ -n "$build_args" ]; then
|
|
|
|
|
|
set -- --build-arg "$build_args" "$@"
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
docker ps && docker info && \
|
|
|
|
|
|
docker build "$@" "$CONTEXT_DIR" && \
|
|
|
|
|
|
echo "$image 已经构建完了" && \
|
|
|
|
|
|
docker login "$REGISTRY" -u "$REG_USER" -p "$REG_PASS" && \
|
|
|
|
|
|
docker push "$image" && \
|
|
|
|
|
|
echo "$image 推送成功"
|
|
|
|
|
|
'
|