diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index ad7ef70..c9b5370 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -1,69 +1,82 @@ -name: Publish Docker image - -# To use this workflow: -# * Change the IMAGE_NAME to yours - use lowercase only -# * Enable automated build if you wish +name: Publish Multi-Architecture Docker image env: + # The name of your image. This will be combined with your username/org. IMAGE_NAME: pihole + # Target registry + REGISTRY: ghcr.io on: workflow_dispatch: - ## Enable automated build by uncommenting the following lines: push: - # Publish `master` as Docker `latest` image. + # Trigger on pushes to the main branch (tags it as 'latest') branches: - main - # Publish `v1.2.3` tags as release `1.2.3`. + # Trigger on version tags like v1.0.1 tags: - v* jobs: - # Test docker build before publishing - # test: - # runs-on: ubuntu-latest - - # steps: - # - uses: actions/checkout@v2 - - # - name: Run tests - # run: | - # if [ -f docker-compose.test.yml ]; then - # docker-compose --file docker-compose.test.yml build - # # docker-compose --file docker-compose.test.yml run sut - # else - # docker build . --file Dockerfile - # fi - - # Push image to GitHub Packages. push: - # Ensure test job passes before pushing image. - # needs: test - runs-on: ubuntu-latest - permissions: write-all + permissions: + contents: read + packages: write + steps: - - uses: actions/checkout@v2 + # 1. Checkout the source code from the repository + - name: Checkout repository + uses: actions/checkout@v4 - - name: Build image - run: docker build . --file Dockerfile --tag $IMAGE_NAME + # 2. Set up QEMU (Quick Emulator) + # This allows building for different architectures (like ARM64) on an AMD64 runner + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + with: + platforms: all + # 3. Set up Docker Buildx + # Buildx is the modern Docker CLI plugin that enables multi-platform builds + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + # 4. Login to GitHub Container Registry (GHCR) + # Uses the automatic GITHUB_TOKEN for authentication - name: Log into GitHub Container Registry - run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # 5. Extract metadata (Tags & Labels) + # Automatically handles: + # - Lowercasing the image name + # - Creating 'latest' for the main branch + # - Creating semver tags (1.2.3) from git tags (v1.2.3) + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=semver,pattern={{version}} + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} - - name: Push image to GitHub Container Registry - - run: | - IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME - # Change all uppercase to lowercase - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') - # Strip git ref prefix from version - VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') - # Strip "v" prefix from tag name - [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') - # Use Docker `latest` tag convention - [ "$VERSION" == "main" ] && VERSION=latest - echo IMAGE_ID=$IMAGE_ID - echo VERSION=$VERSION - docker tag $IMAGE_NAME $IMAGE_ID:$VERSION - docker push $IMAGE_ID:$VERSION + # 6. Build and Push the Multi-Arch Image + # This builds the image for all specified platforms and pushes it to GHCR + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + # Pushes the image only on push events (not on PRs by default) + push: true + # List the architectures you want to support here: + platforms: linux/amd64,linux/arm64,linux/arm/v7 + # Uses the tags and labels generated in step 5 + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + # Enable caching to speed up subsequent builds + cache-from: type=gha + cache-to: type=gha,mode=max