Docker 镜像发布 #3
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Docker 镜像发布 | |
| on: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| jobs: | |
| build-and-push: | |
| name: 构建并推送Docker镜像 | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - name: 检出代码 | |
| uses: actions/checkout@v4 | |
| - name: 设置 Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: 登录到 GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: 提取元数据 | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: | | |
| ghcr.io/${{ github.repository }}/backend | |
| ghcr.io/${{ github.repository }}/frontend | |
| tags: | | |
| type=semver,pattern={{version}} | |
| type=semver,pattern={{major}}.{{minor}} | |
| type=ref,event=branch | |
| type=sha,format=short | |
| - name: 构建并推送后端镜像 | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./backend | |
| push: true | |
| tags: ghcr.io/${{ github.repository }}/backend:${{ steps.meta.outputs.version }},ghcr.io/${{ github.repository }}/backend:latest | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: 构建并推送前端镜像 | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./blog-web | |
| push: true | |
| tags: ghcr.io/${{ github.repository }}/frontend:${{ steps.meta.outputs.version }},ghcr.io/${{ github.repository }}/frontend:latest | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| build-args: | | |
| PUBLIC_API_BASE_URL=http://localhost:8080/api | |
| INTERNAL_API_BASE_URL=http://backend:8080/api | |
| - name: 更新 Docker Compose 文件 | |
| run: | | |
| VERSION=${{ steps.meta.outputs.version }} | |
| sed -i "s|build:\s*context: ./backend|image: ghcr.io/${{ github.repository }}/backend:${VERSION}|g" docker-compose.yml | |
| sed -i "s|build:\s*context: ./blog-web|image: ghcr.io/${{ github.repository }}/frontend:${VERSION}|g" docker-compose.yml | |
| - name: 创建 Docker Compose 发布文件 | |
| run: | | |
| cp docker-compose.yml docker-compose.release.yml | |
| - name: 上传 Docker Compose 文件作为发布资产 | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| files: docker-compose.release.yml | |
| token: ${{ secrets.GITHUB_TOKEN }} |