SmartParks/.gitea/workflows/master.yml
mocheng 46735c04ba
Some checks failed
Build and Push Docker Images / build-and-push (push) Failing after 0s
新增工作流
2025-08-14 19:25:24 +08:00

62 lines
2.2 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 Docker Images
# 监听master分支的push事件
on:
push:
branches: [ master ]
jobs:
build-and-push:
# 使用当前运行器已安装Java、Maven和Docker
runs-on: ubuntu
steps:
# 拉取代码
- name: Checkout code
uses: actions/checkout@v4
# 使用服务器上的Maven构建项目
- name: Build with Maven
run: |
echo "开始使用Maven构建项目..."
mvn clean package -DskipTests
echo "Maven构建完成"
# 查找所有Dockerfile并构建推送镜像
- name: Build and push Docker images
env:
# 私有仓库地址(请替换为实际地址)
DOCKER_REGISTRY: your-private-registry.example.com
# 私有仓库认证信息在Gitea仓库设置中配置secrets
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: |
echo "登录到私有Docker仓库..."
echo "$DOCKER_PASSWORD" | docker login $DOCKER_REGISTRY -u $DOCKER_USERNAME --password-stdin
echo "查找项目中的所有Dockerfile..."
# 查找所有Dockerfile排除.git目录
find . -type f -name "Dockerfile" ! -path "./.git/*" | while read -r dockerfile; do
echo "处理Dockerfile: $dockerfile"
# 获取Dockerfile所在目录
docker_context=$(dirname "$dockerfile")
# 生成镜像名称(基于目录结构)
image_name=$(echo "$docker_context" | sed 's|./||g' | tr '/' '-')
full_image_name="$DOCKER_REGISTRY/$image_name:${{ github.sha }}"
echo "构建镜像: $full_image_name"
docker build -t "$full_image_name" -f "$dockerfile" "$docker_context"
echo "推送镜像: $full_image_name"
docker push "$full_image_name"
# 可选添加latest标签并推送
docker tag "$full_image_name" "$DOCKER_REGISTRY/$image_name:latest"
docker push "$DOCKER_REGISTRY/$image_name:latest"
done
echo "所有镜像构建和推送完成"
docker logout $DOCKER_REGISTRY