新增工作流
Some checks are pending
Build and Push Docker Images / build-and-push (push) Waiting to run

This commit is contained in:
2025-08-14 19:23:51 +08:00
parent 4b3ed9dbaf
commit 6ceea97200
2 changed files with 62 additions and 1 deletions

View File

@@ -2,7 +2,7 @@ run-name: ${{ gitea.actor }} 构建镜像并推送不含JAR依赖上传
on: on:
push: push:
branches: branches:
- master - main
jobs: jobs:
build-parent-pom: build-parent-pom:

View File

@@ -0,0 +1,61 @@
name: Build and Push Docker Images
# 监听master分支的push事件
on:
push:
branches: [ master ]
jobs:
build-and-push:
# 使用当前运行器已安装Java、Maven和Docker
runs-on: self-hosted
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