From b2d6dfe7c847ba62205d783d8337a86b3957a17c Mon Sep 17 00:00:00 2001 From: Alexander Taepper Date: Tue, 28 Jan 2025 13:15:29 +0100 Subject: [PATCH] build: simplify ci by always building the arm image (#683) * build: simplify ci by always building the arm image --- .github/workflows/ci.yml | 117 ++++++++------------------------------- 1 file changed, 22 insertions(+), 95 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 84acba7c2..ce8c8f0c6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,92 +50,30 @@ jobs: DIR_HASH=$(echo -n ${{ hashFiles('conanfile.py', 'conanprofile.docker', './Dockerfile_dependencies') }}) echo "DIR_HASH=$DIR_HASH" >> $GITHUB_ENV - - name: Determine platform build necessity - run: | - # Default PLATFORM_BUILD to false - echo "PLATFORM_BUILD=false" >> $GITHUB_ENV - - # Check if architecture is amd64 - if [[ "${{ matrix.arch }}" == "amd64" ]]; then - echo "PLATFORM_BUILD=true" >> $GITHUB_ENV - exit 0 - fi - - # Check if branch is main - if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then - echo "PLATFORM_BUILD=true" >> $GITHUB_ENV - exit 0 - fi - - # Write the current labels to a file - cat < event.json - ${{ toJSON(github.event.pull_request.labels) }} - EOF - - # Check if the PR has the label 'arm-image' - if jq -e '.[] | select(.name == "arm-image")' event.json > /dev/null; then - echo "PLATFORM_BUILD=true" >> $GITHUB_ENV - exit 0 - fi - - - name: Check if image exists - if: env.PLATFORM_BUILD == 'true' + - name: Check if dependency image exists run: | EXISTS=$(docker manifest inspect \ ${{ env.DOCKER_DEPENDENCY_IMAGE_NAME }}:filehash-${{ env.DIR_HASH }}-${{ matrix.arch }} \ > /dev/null 2>&1 && echo "true" || echo "false") echo "CACHE_HIT=$EXISTS" >> $GITHUB_ENV - - name: Build and push image if input files changed - if: env.CACHE_HIT == 'false' && env.PLATFORM_BUILD == 'true' + - name: Build and push dependency image if input files changed + if: env.CACHE_HIT == 'false' uses: docker/build-push-action@v6 with: context: . file: Dockerfile_dependencies push: true tags: ${{ env.DOCKER_DEPENDENCY_IMAGE_NAME }}:filehash-${{ env.DIR_HASH }}-${{ matrix.arch }} - cache-from: type=gha,ref=builder-image-cache-${{ hashFiles('conanfile.py', 'Dockerfile') }} - cache-to: type=gha,mode=min,ref=builder-image-cache-${{ hashFiles('conanfile.py', 'Dockerfile') }} - platforms: "linux/${{ matrix.arch }}" + cache-from: type=gha,ref=dependency-image-cache-${{ matrix.arch }}-${{ hashFiles('conanfile.py', 'Dockerfile') }} + cache-to: type=gha,mode=min,ref=dependency-image-cache-${{ hashFiles('conanfile.py', 'Dockerfile') }} - name: Tag dependency image with commit hash - if: env.PLATFORM_BUILD == 'true' run: | docker buildx imagetools create \ --tag ${{ env.DOCKER_DEPENDENCY_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-${{ matrix.arch }} \ ${{ env.DOCKER_DEPENDENCY_IMAGE_NAME }}:filehash-${{ env.DIR_HASH }}-${{ matrix.arch }} - - name: Build and push production image - if: env.PLATFORM_BUILD == 'true' - uses: docker/build-push-action@v6 - with: - context: . - push: true - platforms: "linux/${{ matrix.arch }}" - cache-from: type=gha,ref=${{ github.ref_name }}-image-cache - cache-to: type=gha,mode=min,ref=${{ github.ref_name }}-image-cache - tags: ${{ env.DOCKER_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-${{ matrix.arch }} - build-args: | - DEPENDENCY_IMAGE=${{ env.DOCKER_DEPENDENCY_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-${{ matrix.arch }} - - unitTests: - name: Unit tests - needs: dockerImage - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Determine HEAD_SHA - run: | - if [[ "${{ github.event_name }}" == "pull_request" ]]; then - echo "HEAD_SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV - else - echo "HEAD_SHA=${{ github.sha }}" >> $GITHUB_ENV - fi - - name: Build unit test image uses: docker/build-push-action@v6 with: @@ -143,10 +81,10 @@ jobs: target: builder tags: builder load: true - cache-from: type=gha,ref=${{ github.ref_name }}-image-cache - cache-to: type=gha,mode=min,ref=${{ github.ref_name }}-image-cache + cache-from: type=gha,ref=image-cache-${{ github.ref_name }}-${{ matrix.arch }} + cache-to: type=gha,mode=min,ref=image-cache-${{ github.ref_name }}-${{ matrix.arch }} build-args: | - DEPENDENCY_IMAGE=${{ env.DOCKER_DEPENDENCY_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-amd64 + DEPENDENCY_IMAGE=${{ env.DOCKER_DEPENDENCY_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-${{ matrix.arch }} - name: Run unit tests run: | @@ -154,6 +92,18 @@ jobs: --entrypoint "./silo_test" \ builder + - name: Build and push production image + uses: docker/build-push-action@v6 + with: + context: . + push: true + cache-from: type=gha,ref=image-cache-${{ github.ref_name }}-${{ matrix.arch }} + cache-to: type=gha,mode=min,ref=image-cache-${{ github.ref_name }}-${{ matrix.arch }} + tags: ${{ env.DOCKER_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-${{ matrix.arch }} + build-args: | + DEPENDENCY_IMAGE=${{ env.DOCKER_DEPENDENCY_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-${{ matrix.arch }} + + multiPlatformImages: name: Create multi-platform images needs: dockerImage @@ -177,30 +127,6 @@ jobs: echo "HEAD_SHA=${{ github.sha }}" >> $GITHUB_ENV fi - - name: Detect all platforms with built images - run: | - PLATFORMS="" - - PLATFORMS_TO_CHECK=( - "${{ env.DOCKER_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-arm64" - "${{ env.DOCKER_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-amd64" - ) - - for PLATFORM in "${PLATFORMS_TO_CHECK[@]}"; do - echo "Checking existence of: $PLATFORM" - if docker manifest inspect "$PLATFORM" > /dev/null 2>&1; then - echo "Image for $PLATFORM exists" - PLATFORMS+="$PLATFORM " - else - echo "Image for $PLATFORM does not exist" - fi - done - - # Trim trailing space from PLATFORMS - PLATFORMS=$(echo "$PLATFORMS" | sed 's/[[:space:]]*$//') - - echo "PLATFORM_SPECIFIC_IMAGES=$PLATFORMS" >> $GITHUB_ENV - - name: Docker metadata id: dockerMetadataImage uses: docker/metadata-action@v5 @@ -216,7 +142,8 @@ jobs: TAGS=(${{ steps.dockerMetadataImage.outputs.tags }}) for TAG in "${TAGS[@]}"; do docker buildx imagetools create --tag $TAG \ - ${{ env.PLATFORM_SPECIFIC_IMAGES }} + "${{ env.DOCKER_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-arm64" \ + "${{ env.DOCKER_IMAGE_NAME }}:commit-${{ env.HEAD_SHA }}-amd64" done endToEndTests: