chore(build): Optimize Docker build configuration and process #79
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: Build and Push Docker Image | |
| on: | |
| push: | |
| branches: | |
| - main | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.ref }} | |
| cancel-in-progress: false | |
| jobs: | |
| build-images: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| include: | |
| - service_name: ui | |
| file: frontend/Dockerfile | |
| context: ./frontend | |
| platform: linux/amd64,linux/arm64 | |
| - service_name: app | |
| file: docker/Dockerfile.app | |
| context: . | |
| platform: linux/amd64,linux/arm64 | |
| - service_name: docreader | |
| file: docker/Dockerfile.docreader | |
| context: . | |
| platform: linux/amd64,linux/arm64 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v3 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
| - name: Prepare version info | |
| id: version | |
| run: | | |
| # 使用统一的版本管理脚本 | |
| eval "$(./scripts/get_version.sh env)" | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "commit_id=$COMMIT_ID" >> $GITHUB_OUTPUT | |
| echo "build_time=$BUILD_TIME" >> $GITHUB_OUTPUT | |
| echo "go_version=$GO_VERSION" >> $GITHUB_OUTPUT | |
| # 显示版本信息 | |
| ./scripts/get_version.sh info | |
| - name: Build Cache for Docker | |
| uses: actions/cache@v4 | |
| id: cache | |
| with: | |
| path: go-build-cache | |
| key: ${{ runner.os }}-go-build-cache-${{ hashFiles('**/go.sum') }} | |
| - name: Inject go-build-cache | |
| uses: reproducible-containers/buildkit-cache-dance@v3 | |
| with: | |
| cache-source: go-build-cache | |
| cache-map: | | |
| { | |
| "go-pkg-mod": "/go/pkg/mod" | |
| } | |
| skip-extraction: ${{ steps.cache.outputs.cache-hit }} | |
| - name: Build ${{ matrix.service_name }} Image | |
| uses: docker/build-push-action@v3 | |
| with: | |
| push: true | |
| platforms: ${{ matrix.platform }} | |
| file: ${{ matrix.file }} | |
| context: ${{ matrix.context }} | |
| build-args: | | |
| ${{ matrix.service_name == 'app' && format('VERSION_ARG={0}', steps.version.outputs.version) || '' }} | |
| ${{ matrix.service_name == 'app' && format('COMMIT_ID_ARG={0}', steps.version.outputs.commit_id) || '' }} | |
| ${{ matrix.service_name == 'app' && format('BUILD_TIME_ARG={0}', steps.version.outputs.build_time) || '' }} | |
| ${{ matrix.service_name == 'app' && format('GO_VERSION_ARG={0}', steps.version.outputs.go_version) || '' }} | |
| tags: | | |
| ${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:latest | |
| ${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:${{ steps.version.outputs.version }} | |
| cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:cache | |
| cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/weknora-${{ matrix.service_name }}:cache,mode=max | |