Build and Push Multi-arch Docker Images #36
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 Multi-arch Docker Images | |
| permissions: | |
| contents: read | |
| packages: write | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| services: | |
| description: "Comma-separated list of services to build (e.g., caddy,i2pd-tools). Leave empty to auto-detect." | |
| required: false | |
| type: string | |
| default: "" | |
| jobs: | |
| setup-services: | |
| runs-on: ubuntu-24.04 | |
| outputs: | |
| services: ${{ steps.set-services.outputs.services }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up matrix | |
| id: set-services | |
| run: | | |
| if [ -n "${{ github.event.inputs.services }}" ]; then | |
| echo "Using provided services: ${{ github.event.inputs.services }}" | |
| SERVICES=$(echo "${{ github.event.inputs.services }}" | tr ',' '\n' | xargs -I {} echo '"{}"' | jq -c -s '.') | |
| else | |
| echo "Auto-detecting projects..." | |
| SERVICES=$(jq -r '.projects | keys[]' projects.json | jq -R . | jq -c -s '.') | |
| fi | |
| echo "services=$SERVICES" >> $GITHUB_OUTPUT | |
| build: | |
| needs: setup-services | |
| runs-on: ${{ matrix.arch == 'arm64' && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }} | |
| strategy: | |
| matrix: | |
| service: ${{ fromJson(needs.setup-services.outputs.services) }} | |
| arch: [amd64, arm64] | |
| steps: | |
| - name: Checkout code (sparse for service) | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| sparse-checkout: | | |
| ${{ matrix.service }} | |
| projects.json | |
| sparse-checkout-cone-mode: true | |
| - name: Clone repository | |
| run: | | |
| REPO=$(jq -r ".projects.\"${{ matrix.service }}\".repo // empty" projects.json) | |
| if [ -n "$REPO" ] && [ "$REPO" != "null" ]; then | |
| REF=$(jq -r ".projects.\"${{ matrix.service }}\".ref" projects.json) | |
| echo "Cloning $REPO:$REF into ${{ matrix.service }}/src/" | |
| git clone --depth 1 --branch "$REF" --recursive "$REPO" "${{ matrix.service }}/src" | |
| fi | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Docker Buildx (required to use buildx imagetools) | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| driver: docker-container | |
| - name: Build and push native image (per-arch) | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: ${{ matrix.service }} | |
| file: ${{ matrix.service }}/Dockerfile | |
| platforms: linux/${{ matrix.arch }} | |
| push: true | |
| tags: | | |
| ghcr.io/${{ github.repository_owner }}/${{ matrix.service }}:${{ matrix.arch }}-latest | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| create-manifest: | |
| needs: [setup-services, build] | |
| runs-on: ubuntu-24.04 | |
| strategy: | |
| matrix: | |
| service: ${{ fromJson(needs.setup-services.outputs.services) }} | |
| steps: | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| with: | |
| driver: docker-container | |
| - name: Create and push multi-arch manifest (imagetools) | |
| run: | | |
| AMD64_TAG="ghcr.io/${{ github.repository_owner }}/${{ matrix.service }}:amd64-latest" | |
| ARM64_TAG="ghcr.io/${{ github.repository_owner }}/${{ matrix.service }}:arm64-latest" | |
| MULTI_TAG="ghcr.io/${{ github.repository_owner }}/${{ matrix.service }}:latest" | |
| docker buildx imagetools create --tag "$MULTI_TAG" "$AMD64_TAG" "$ARM64_TAG" |