Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build images on target platforms #579

Merged
merged 9 commits into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 77 additions & 14 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Build Docker

permissions:
id-token: write
contents: write

on:
push:
tags: ["v*"]
Expand All @@ -13,8 +17,8 @@ on:
workflow_dispatch:

jobs:
docker:
name: Build Docker
docker_build:
name: Build Docker image for Postgres ${{ matrix.postgres }} on ${{ matrix.runner }}
strategy:
matrix:
postgres: ["14", "15", "16", "17"]
Expand All @@ -25,6 +29,9 @@ jobs:
env:
BUILDKIT_PROGRESS: plain
POSTGRES_VERSION: ${{ matrix.postgres }}
outputs:
branch_tag: ${{ steps.params.outputs.branch_tag }}
target_repo: ${{ steps.params.outputs.target_repo }}
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
Expand All @@ -36,32 +43,88 @@ jobs:
uses: actions/checkout@v4
with:
submodules: "recursive"
- name: Compute platform
id: compute_platform

- name: Compute job parameters
id: params
run: |
# Tag is XX-YYYYY-<branch>-latest so 16 + branch name length
# since maximum docker tag is 128 characters, we need to truncate the branch name to 112
BRANCH=$(echo "${{ github.head_ref || github.ref_name }}" \
| sed 's/[^a-zA-Z0-9\-\.]/-/g' \
| cut -c 1-112 | tr '[:upper:]' '[:lower:]' \
| sed -e 's/-*$//')

# Set platform depending on which runner we're using
if [ "${{ matrix.runner }}" = "ubuntu-24.04" ]; then
echo "platform=amd64" >> "$GITHUB_OUTPUT"
PLATFORM=amd64
else
PLATFORM=arm64
fi

# If main or tag, then push to `pgduckdb/pgduckdb`
git fetch --tags
if [ "$BRANCH" = "main" ] || git rev-parse --verify $BRANCH^{tag} > /dev/null 2>&1; then
TARGET_REPO='pgduckdb/pgduckdb'
else
echo "platform=arm64" >> "$GITHUB_OUTPUT"
TARGET_REPO='pgduckdb/ci-builds'
fi

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
echo "platform=$PLATFORM" >> "$GITHUB_OUTPUT"
echo "branch_tag=$BRANCH" >> "$GITHUB_OUTPUT"
echo "target_repo=$TARGET_REPO" >> "$GITHUB_OUTPUT"
echo "latest_image=pgduckdb/ci-builds:${{ matrix.postgres }}-${PLATFORM}-${BRANCH}-latest" >> "$GITHUB_OUTPUT"

- name: Attempt to pull previous image
run: |
docker pull ${{ steps.params.outputs.latest_image }} || true
docker pull moby/buildkit:buildx-stable-1

- name: Set up Docker buildx
uses: docker/setup-buildx-action@v3
with:
platforms: linux/${{ steps.compute_platform.outputs.platform }}
platforms: linux/${{ steps.params.outputs.platform }}

- name: docker bake
uses: docker/bake-action@v5
with:
targets: pg_duckdb_${{ matrix.postgres }}
push: true
set: |
*.platform=linux/${{ steps.compute_platform.outputs.platform }}
*.cache-to=type=gha,mode=max
*.cache-from=type=gha
postgres.tags=pgduckdb/pgduckdb:${{ matrix.postgres }}-${{ steps.compute_platform.outputs.platform }}-${{ github.sha }}
${{ !contains(github.ref_name, '/') && format('postgres.tags=pgduckdb/pgduckdb:{0}-${1}-{2}', matrix.postgres, steps.compute_platform.outputs.platform, github.ref_name) || '' }}
*.platform=linux/${{ steps.params.outputs.platform }}
*.cache-from=type=registry,ref=${{ steps.params.outputs.latest_image }}
*.cache-from=type=gha,scope=${{ github.workflow }}
*.cache-to=type=gha,mode=max,scope=${{ github.workflow }}
postgres.tags=pgduckdb/ci-builds:${{ matrix.postgres }}-${{ steps.params.outputs.platform }}-${{ github.sha }}
postgres.tags=${{ steps.params.outputs.latest_image }}

docker_merge:
name: Merge Docker image for Postgres ${{ matrix.postgres }}
strategy:
matrix:
postgres: ["14", "15", "16", "17"]

runs-on: ubuntu-24.04
needs: docker_build
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: pgduckdb
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Merge images
run: |
docker pull --platform linux/amd64 pgduckdb/ci-builds:${{ matrix.postgres }}-amd64-${{ github.sha }}
docker pull --platform linux/arm64 pgduckdb/ci-builds:${{ matrix.postgres }}-arm64-${{ github.sha }}

BRANCH="${{ needs.docker_build.outputs.branch_tag }}"
TARGET_REPO="${{ needs.docker_build.outputs.target_repo }}"

echo "Will push merged image to '${TARGET_REPO}'."
docker buildx imagetools create \
--tag ${TARGET_REPO}:${{ matrix.postgres }}-${BRANCH} \
--tag pgduckdb/ci-builds:${{ matrix.postgres }}-${{ github.sha }} \
pgduckdb/ci-builds:${{ matrix.postgres }}-amd64-${{ github.sha }} \
pgduckdb/ci-builds:${{ matrix.postgres }}-arm64-${{ github.sha }}

docker buildx imagetools inspect pgduckdb/ci-builds:${{ matrix.postgres }}-${{ github.sha }}