Skip to content

Build and Push Docker Image to GHCR and ALI_DOCKER_REGISTRY #4

Build and Push Docker Image to GHCR and ALI_DOCKER_REGISTRY

Build and Push Docker Image to GHCR and ALI_DOCKER_REGISTRY #4

name: Build and Push Docker Image to GHCR
on:
workflow_dispatch: # 手动触发
permissions:
contents: read
packages: write
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Get short SHA
id: vars
run: echo "short_sha=${GITHUB_SHA::8}" >> $GITHUB_OUTPUT
- name: Log in to GHCR with GITHUB_TOKEN
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push frontend image
uses: docker/build-push-action@v5
with:
context: ./frontend
file: ./frontend/Dockerfile
push: true
tags: |
ghcr.io/pancrepal-xiaoyibao/kb-frontend:latest
ghcr.io/pancrepal-xiaoyibao/kb-frontend:${{ github.sha }}
ghcr.io/pancrepal-xiaoyibao/kb-frontend:${{ steps.vars.outputs.short_sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
- name: Build and push backend image
uses: docker/build-push-action@v5
with:
context: ./backend
file: ./backend/Dockerfile
push: true
tags: |
ghcr.io/pancrepal-xiaoyibao/kb-backend:latest
ghcr.io/pancrepal-xiaoyibao/kb-backend:${{ github.sha }}
ghcr.io/pancrepal-xiaoyibao/kb-backend:${{ steps.vars.outputs.short_sha }}
build-args: |
# 构建时传递环境变量
DATABASE_URL=${{ secrets.DATABASE_URL || 'sqlite:///./kb_upload.db' }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64
- name: Build summary
run: |
echo "✅ 容器构建完成!"
echo ""
echo "前端镜像标签:"
echo " - ghcr.io/pancrepal-xiaoyibao/kb-frontend:latest"
echo " - ghcr.io/pancrepal-xiaoyibao/kb-frontend:${{ steps.vars.outputs.short_sha }}"
echo ""
echo "后端镜像标签:"
echo " - ghcr.io/pancrepal-xiaoyibao/kb-backend:latest"
echo " - ghcr.io/pancrepal-xiaoyibao/kb-backend:${{ steps.vars.outputs.short_sha }}"