Build and Publish Docker Image #16
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 Publish Docker Image | |
| on: | |
| workflow_run: | |
| workflows: ["Build and Upload Artifacts"] | |
| types: | |
| - completed | |
| branches: | |
| - main | |
| env: | |
| DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
| DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} | |
| IMAGE_NAME: mecoblock/sanitizetelebot | |
| jobs: | |
| build-and-push: | |
| # Only run if the Go build workflow succeeded | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| platform: [linux/amd64, linux/arm64] | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up QEMU | |
| uses: docker/setup-qemu-action@v2 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v2 | |
| - name: Get architecture | |
| id: platform | |
| run: | | |
| # Extract architecture from platform string | |
| echo "arch=${platform#*/}" >> $GITHUB_OUTPUT | |
| env: | |
| platform: ${{ matrix.platform }} | |
| - name: Download Binary | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: sanitizetelebot-linux-${{ steps.platform.outputs.arch }} | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| path: bin | |
| - name: List downloaded files | |
| run: ls -la bin/ | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ env.DOCKERHUB_USERNAME }} | |
| password: ${{ env.DOCKERHUB_TOKEN }} | |
| - name: Build and push | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| platforms: ${{ matrix.platform }} | |
| push: true | |
| tags: ${{ env.IMAGE_NAME }}:${{ steps.platform.outputs.arch }}-latest | |
| build-args: | | |
| APP_BINARY=bin/sanitizetelebot-linux-${{ steps.platform.outputs.arch }} | |
| create-manifest: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ env.DOCKERHUB_USERNAME }} | |
| password: ${{ env.DOCKERHUB_TOKEN }} | |
| - name: Create and push manifest | |
| run: | | |
| # Enable Docker experimental features | |
| export DOCKER_CLI_EXPERIMENTAL=enabled | |
| # Create and push the manifest list | |
| docker manifest create ${{ env.IMAGE_NAME }}:latest \ | |
| --amend ${{ env.IMAGE_NAME }}:amd64-latest \ | |
| --amend ${{ env.IMAGE_NAME }}:arm64-latest | |
| # Annotate the manifest with platform-specific information | |
| docker manifest annotate ${{ env.IMAGE_NAME }}:latest \ | |
| ${{ env.IMAGE_NAME }}:amd64-latest --os linux --arch amd64 | |
| docker manifest annotate ${{ env.IMAGE_NAME }}:latest \ | |
| ${{ env.IMAGE_NAME }}:arm64-latest --os linux --arch arm64 | |
| # Push the manifest | |
| docker manifest push --purge ${{ env.IMAGE_NAME }}:latest |