Add docker images size change check #11
Workflow file for this run
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
| # Copyright (C) 2024 Intel Corporation | |
| # SPDX-License-Identifier: Apache-2.0 | |
| name: Check Docker Image Size Change | |
| permissions: | |
| contents: read | |
| on: | |
| pull_request: | |
| branches: [main] | |
| types: [opened, reopened, ready_for_review, synchronize] | |
| paths: | |
| - '**/Dockerfile' | |
| # If there is a new commit, the previous jobs will be canceled | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| check-image-size: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| pull-requests: write | |
| steps: | |
| - name: Checkout PR branch | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Get changed Dockerfiles | |
| id: changed-dockerfiles | |
| run: | | |
| git fetch origin ${{ github.base_ref }} | |
| merged_commit=$(git log -1 --format='%H') | |
| files=$(git diff --name-status --diff-filter=ARM ${{ github.event.pull_request.base.sha }} ${merged_commit} | awk '{print $2}' | grep -E 'Dockerfile$' || true) | |
| echo "files<<EOF" >> $GITHUB_OUTPUT | |
| echo "$files" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| - name: Build and check image sizes | |
| if: steps.changed-dockerfiles.outputs.files != '' | |
| run: | | |
| cd ${{github.workspace}} | |
| merged_commit=$(git log -1 --format='%H') | |
| set -e | |
| while read -r dockerfile; do | |
| [ -z "$dockerfile" ] && continue | |
| dir=$(dirname "$dockerfile") | |
| file=$(basename "$dockerfile") | |
| image_base="pr-image-size-base:$(echo $dir | tr '/' '-')" | |
| image_pr="pr-image-size-pr:$(echo $dir | tr '/' '-')" | |
| cd $dir | |
| echo "Building base image for $dockerfile" | |
| git checkout origin/${{ github.base_ref }} | |
| docker build -f "$file" -t "$image_base" --no-cache . | |
| cat $file | |
| size_base=$(docker image inspect "$image_base" | jq '.[0].Size / (1024 * 1024) | round') | |
| echo "Building PR image for $dockerfile" | |
| git checkout $merged_commit | |
| docker build -f "$file" -t "$image_pr" --no-cache . | |
| cat $file | |
| size_pr=$(docker image inspect "$image_pr" | jq '.[0].Size / (1024 * 1024) | round') | |
| diff=$((size_pr - size_base)) | |
| echo "::notice file=$dockerfile::Image size change: $size_base -> $size_pr MB}' (diff: $diff MB)" | |
| docker rmi "$image_base" "$image_pr" | |
| done <<< "${{ steps.changed-dockerfiles.outputs.files }}" |