SmartParks/.gitea/workflows/master.yml
mocheng 9d12fc7fdd
Some checks failed
Build and Push to Docker Registry / 构建并推送镜像到Docker Registry (push) Failing after 1m54s
新增工作流3
2025-08-14 21:17:29 +08:00

91 lines
3.5 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

name: Build and Push to Docker Registry
on:
push:
branches: [ master ]
jobs:
build-and-deploy:
name: 构建并推送镜像到Docker Registry
runs-on: ubuntu
timeout-minutes: 30
steps:
- name: 拉取代码
uses: http://git.missmoc.top/mocheng/checkout@v4
with:
fetch-depth: 0
- name: 使用Maven构建项目
run: |
echo "===== 开始Maven构建 ====="
/maven/apache-maven-3.9.11/bin/mvn clean package -DskipTests
echo "===== Maven构建完成 ====="
- name: 构建并推送Docker镜像
env:
# 目标仓库地址(你的新建仓库)
TARGET_REGISTRY: 127.0.0.1:5000
# 目标仓库中的项目名称
TARGET_PROJECT: smartparks
# 基础镜像所在仓库地址(需要认证的仓库)
BASE_REGISTRY: 172.100.10.45:3000
# 基础镜像仓库的登录凭证在Gitea secrets中配置
BASE_REGISTRY_USERNAME: ${{ secrets.BASE_REGISTRY_USERNAME }}
BASE_REGISTRY_PASSWORD: ${{ secrets.BASE_REGISTRY_PASSWORD }}
run: |
echo "===== 环境信息 ====="
echo "目标仓库: $TARGET_REGISTRY/$TARGET_PROJECT"
echo "基础镜像仓库: $BASE_REGISTRY"
# 关键步骤先登录到基础镜像仓库解决401认证问题
echo "===== 登录基础镜像仓库 ====="
if ! echo "$BASE_REGISTRY_PASSWORD" | docker login $BASE_REGISTRY -u $BASE_REGISTRY_USERNAME --password-stdin; then
echo "错误:登录基础镜像仓库 $BASE_REGISTRY 失败,请检查账户密码"
exit 1
fi
echo "===== 查找项目中的Dockerfile ====="
dockerfiles=$(find . -type f -name "Dockerfile" ! -path "./.git/*")
if [ -z "$dockerfiles" ]; then
echo "警告未找到任何Dockerfile"
exit 0
fi
# 处理每个Dockerfile
echo "$dockerfiles" | while read -r dockerfile; do
echo "===== 处理Dockerfile: $dockerfile ====="
docker_context=$(dirname "$dockerfile")
image_tag=$(echo "$docker_context" | sed 's|./||g' | tr '/' '-' | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9_-]//g')
full_image_name="$TARGET_REGISTRY/$TARGET_PROJECT:$image_tag-${{ github.sha }}"
echo "镜像名称: $full_image_name"
echo "===== 构建镜像 ====="
if ! docker build -t "$full_image_name" -f "$dockerfile" "$docker_context"; then
echo "错误:构建镜像失败"
exit 1
fi
echo "===== 推送镜像到目标仓库 ====="
# 目标仓库无认证,直接推送
if ! docker push "$full_image_name"; then
echo "错误:推送镜像到目标仓库失败,请检查仓库是否可访问"
exit 1
fi
# 推送latest标签
latest_image="$TARGET_REGISTRY/$TARGET_PROJECT:$image_tag-latest"
echo "===== 推送最新标签: $latest_image ====="
docker tag "$full_image_name" "$latest_image"
if ! docker push "$latest_image"; then
echo "错误推送latest标签失败"
exit 1
fi
done
echo "===== 清理操作 ====="
docker logout $BASE_REGISTRY # 退出基础镜像仓库登录
docker system prune -f
echo "===== 所有操作完成 ====="