diff --git a/.github/workflows/docker.build.yaml b/.github/workflows/docker.build.yaml index 271d998..264d621 100644 --- a/.github/workflows/docker.build.yaml +++ b/.github/workflows/docker.build.yaml @@ -9,12 +9,16 @@ on: tags: - 'v*.*.*' +env: + IMAGE_DOCKER_HUB: flownative/php + IMAGE_HARBOR: harbor.flownative.io/docker/php + jobs: - build: - strategy: - matrix: - php: [ 8.2.35, 8.3.36, 8.4.27, 8.5.12 ] - runs-on: ubuntu-latest + # The images are always built from the latest release, also on schedule. + prepare: + runs-on: ubuntu-26.04 + outputs: + tag: ${{ steps.latest_version.outputs.tag }} steps: - uses: actions/checkout@v7 with: @@ -25,39 +29,41 @@ jobs: id: latest_version uses: flownative/action-git-latest-release@v1 - - run: | - sudo chmod -R ugo+rwX . && shopt -s dotglob && rm -rf * - + # Every platform is built natively on a runner of its own, because + # emulating arm64 with QEMU took about 90 minutes per PHP version. + # The images are pushed by digest only and tagged in the merge job. + build: + needs: prepare + strategy: + matrix: + php: [ 8.2.35, 8.3.36, 8.4.27, 8.5.12 ] + platform: [ linux/amd64, linux/arm64 ] + include: + - platform: linux/amd64 + runner: ubuntu-26.04 + - platform: linux/arm64 + runner: ubuntu-26.04-arm + runs-on: ${{ matrix.runner }} + steps: - uses: actions/checkout@v7 with: - ref: ${{ steps.latest_version.outputs.tag }} - fetch-depth: 100 + ref: ${{ needs.prepare.outputs.tag }} - name: Docker meta id: meta uses: docker/metadata-action@v6 with: - flavor: | - latest=false images: | - flownative/php - harbor.flownative.io/docker/php + ${{ env.IMAGE_DOCKER_HUB }} + ${{ env.IMAGE_HARBOR }} labels: | org.opencontainers.image.title=PHP org.opencontainers.image.description=Docker image providing PHP CLI and PHP-FPM with various extensions pre-installed org.opencontainers.image.licenses=MIT org.opencontainers.image.vendor=Flownative GmbH org.opencontainers.image.version=${{ matrix.php }} - tags: | - type=semver,pattern={{major}}.{{minor}},value=${{ matrix.php }} - type=semver,pattern={{version}},value=${{ matrix.php }} - - - name: Set up QEMU - id: qemu - uses: docker/setup-qemu-action@v4 - name: Set up Docker Buildx - id: buildx uses: docker/setup-buildx-action@v4 - name: Login to Docker Hub @@ -73,34 +79,115 @@ jobs: username: ${{ secrets.HARBOR_DOCKER_USERNAME }} password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} - # One cache per minor version, so a new patch release overwrites the - # cache of the previous one instead of leaving it behind in the registry. - - name: Determine build cache reference - id: cache + # One cache per minor version and platform, so a new patch release + # overwrites the cache of the previous one instead of leaving it behind + # in the registry. + - name: Determine names + id: names env: PHP_VERSION: ${{ matrix.php }} - run: echo "ref=harbor.flownative.io/docker/php-build-cache:${PHP_VERSION%.*}" >> "${GITHUB_OUTPUT}" + PLATFORM: ${{ matrix.platform }} + run: | + echo "cache=harbor.flownative.io/docker/php-build-cache:${PHP_VERSION%.*}-${PLATFORM#linux/}" >> "${GITHUB_OUTPUT}" + echo "artifact=digest-${PHP_VERSION}-${PLATFORM#linux/}" >> "${GITHUB_OUTPUT}" # The cache lives in the registry rather than in the GitHub Actions # cache: the latter evicts layer blobs while the cache index still # references them, and BuildKit then fails the build instead of treating # it as a cache miss. - name: Build Docker image + id: build uses: docker/build-push-action@v7 with: context: . - platforms: linux/amd64,linux/arm64 - push: true + platforms: ${{ matrix.platform }} + pull: true build-args: | PHP_VERSION=${{ matrix.php }} - tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - cache-from: type=registry,ref=${{ steps.cache.outputs.ref }} - cache-to: type=registry,ref=${{ steps.cache.outputs.ref }},mode=max,image-manifest=true,oci-mediatypes=true,ignore-error=true + outputs: type=image,"name=${{ env.IMAGE_DOCKER_HUB }},${{ env.IMAGE_HARBOR }}",push-by-digest=true,name-canonical=true,push=true + cache-from: type=registry,ref=${{ steps.names.outputs.cache }} + cache-to: type=registry,ref=${{ steps.names.outputs.cache }},mode=max,image-manifest=true,oci-mediatypes=true,ignore-error=true - announce: - runs-on: ubuntu-latest + - name: Export digest + env: + DIGEST: ${{ steps.build.outputs.digest }} + run: | + mkdir -p "${RUNNER_TEMP}/digests" + touch "${RUNNER_TEMP}/digests/${DIGEST#sha256:}" + + - name: Upload digest + uses: actions/upload-artifact@v7 + with: + name: ${{ steps.names.outputs.artifact }} + path: ${{ runner.temp }}/digests/* + if-no-files-found: error + retention-days: 1 + + # Combines the images of all platforms into one manifest list per PHP + # version and tags it in both registries. The PHP versions are taken from + # the names of the digest artifacts, so they are listed only once above. + merge: needs: build + runs-on: ubuntu-26.04 + steps: + - name: Download digests + uses: actions/download-artifact@v8 + with: + path: ${{ runner.temp }}/digests + pattern: digest-* + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v4 + + - name: Login to Docker Hub + uses: docker/login-action@v4 + with: + username: ${{ secrets.DOCKER_IO_REGISTRY_USER }} + password: ${{ secrets.DOCKER_IO_REGISTRY_PASSWORD }} + + - name: Login to Harbor + uses: docker/login-action@v4 + with: + registry: harbor.flownative.io + username: ${{ secrets.HARBOR_DOCKER_USERNAME }} + password: ${{ secrets.HARBOR_DOCKER_PASSWORD }} + + # Each registry gets its manifest list from the images pushed to that + # same registry, so no blobs have to be copied between registries. + - name: Create manifest lists + working-directory: ${{ runner.temp }}/digests + run: | + set -euo pipefail + versions="$(find . -mindepth 1 -maxdepth 1 -type d -name 'digest-*' | sed -n 's|^\./digest-\([0-9.]*\)-[a-z0-9]*$|\1|p' | sort -u)" + [ -n "${versions}" ] || { echo "::error::No digests found"; exit 1; } + for version in ${versions}; do + # Each artifact is a directory holding one empty file named + # after the digest. + digests="$(find . -mindepth 2 -path "./digest-${version}-*/*" -type f -exec basename {} \;)" + count="$(echo "${digests}" | wc -w | tr -d ' ')" + if [ "${count}" -ne 2 ]; then + echo "::error::PHP ${version}: expected 2 digests, found ${count}" + exit 1 + fi + for image in "${IMAGE_DOCKER_HUB}" "${IMAGE_HARBOR}"; do + sources=() + for digest in ${digests}; do + sources+=("${image}@sha256:${digest}") + done + echo "::group::${image}:${version}" + docker buildx imagetools create \ + --tag "${image}:${version%.*}" \ + --tag "${image}:${version}" \ + "${sources[@]}" + docker buildx imagetools inspect "${image}:${version}" + echo "::endgroup::" + done + done + + announce: + runs-on: ubuntu-26.04 + needs: merge steps: - name: Dispatch to beach-php uses: peter-evans/repository-dispatch@v4