123 lines
3.6 KiB
YAML
123 lines
3.6 KiB
YAML
name: Build and Push Docker Images
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- master
|
|
tags:
|
|
- 'v*'
|
|
pull_request:
|
|
branches:
|
|
- master
|
|
workflow_dispatch:
|
|
|
|
env:
|
|
REGISTRY: crpi-c807itn6exy37e7d.cn-hangzhou.personal.cr.aliyuncs.com
|
|
NAMESPACE: ${{ secrets.ALIYUN_NAMESPACE }}
|
|
BACKEND_IMAGE: idc-backend
|
|
FRONTEND_IMAGE: idc-frontend
|
|
|
|
jobs:
|
|
build-backend:
|
|
name: Build Backend Image
|
|
runs-on: ubuntu-latest
|
|
if: github.event_name != 'pull_request'
|
|
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 登录阿里云 Docker Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ secrets.ALIYUN_USERNAME }}
|
|
password: ${{ secrets.ALIYUN_PASSWORD }}
|
|
|
|
- name: 提取版本号
|
|
id: meta
|
|
run: |
|
|
if [[ "${{ github.ref_type }}" == "tag" ]]; then
|
|
VERSION=${GITHUB_REF#refs/tags/}
|
|
else
|
|
VERSION=latest
|
|
fi
|
|
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
|
|
echo "COMMIT_SHA=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
|
|
|
|
- name: 设置 Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: 构建并推送后端镜像
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: ./backend
|
|
file: ./backend/Dockerfile
|
|
push: true
|
|
tags: |
|
|
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.BACKEND_IMAGE }}:${{ steps.meta.outputs.VERSION }}
|
|
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.BACKEND_IMAGE }}:${{ steps.meta.outputs.COMMIT_SHA }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|
|
build-frontend:
|
|
name: Build Frontend Image
|
|
runs-on: ubuntu-latest
|
|
if: github.event_name != 'pull_request'
|
|
|
|
steps:
|
|
- name: 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 登录阿里云 Docker Registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ secrets.ALIYUN_USERNAME }}
|
|
password: ${{ secrets.ALIYUN_PASSWORD }}
|
|
|
|
- name: 提取版本号
|
|
id: meta
|
|
run: |
|
|
if [[ "${{ github.ref_type }}" == "tag" ]]; then
|
|
VERSION=${GITHUB_REF#refs/tags/}
|
|
else
|
|
VERSION=latest
|
|
fi
|
|
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
|
|
echo "COMMIT_SHA=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
|
|
|
|
- name: 设置 Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
|
|
- name: 构建并推送前端镜像
|
|
uses: docker/build-push-action@v5
|
|
with:
|
|
context: ./frontend
|
|
file: ./frontend/Dockerfile
|
|
push: true
|
|
tags: |
|
|
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.FRONTEND_IMAGE }}:${{ steps.meta.outputs.VERSION }}
|
|
${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.FRONTEND_IMAGE }}:${{ steps.meta.outputs.COMMIT_SHA }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|
|
notify:
|
|
name: Notify Build Complete
|
|
runs-on: ubuntu-latest
|
|
needs: [build-backend, build-frontend]
|
|
if: always() && github.event_name != 'pull_request'
|
|
|
|
steps:
|
|
- name: 输出构建结果
|
|
run: |
|
|
echo "=== 构建结果 ==="
|
|
echo "后端镜像: ${{ needs.build-backend.result }}"
|
|
echo "前端镜像: ${{ needs.build-frontend.result }}"
|
|
if [[ "${{ needs.build-backend.result }}" == "success" && "${{ needs.build-frontend.result }}" == "success" ]]; then
|
|
echo "所有镜像构建成功"
|
|
else
|
|
echo "部分镜像构建失败"
|
|
exit 1
|
|
fi
|