Add docker images size change check #4
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: | | |
| 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" | tr '\0' '\n' >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| - name: Build and check image sizes | |
| if: steps.changed-dockerfiles.outputs.files != '' | |
| run: | | |
| set -x | |
| cd ${{github.workspace}} | |
| merged_commit=$(git log -1 --format='%H') | |
| while IFS= read -r -d '' dockerfile; do | |
| [ -z "$dockerfile" ] && continue | |
| dir=$(dirname "$dockerfile") | |
| image_base="pr-image-size-base:$(echo $dir | tr '/' '-')" | |
| image_pr="pr-image-size-pr:$(echo $dir | tr '/' '-')" | |
| echo "Building base image for $dockerfile" | |
| echo "Base: ${{ github.event.pull_request.base.sha }}" | |
| git checkout ${{ github.event.pull_request.base.sha }} | |
| echo "::group::Build image_base" | |
| docker build -f "$dockerfile" -t "$image_base" --no-cache . | |
| echo "::endgroup::" | |
| size_base=$(docker image inspect "$image_base" | jq '.[0].Size / (1024 * 1024) | round') | |
| echo "Building PR image for $dockerfile" | |
| git checkout $merged_commit | |
| echo "PR: $merged_commit" | |
| echo "::group::Build image_pr" | |
| docker build -f "$dockerfile" -t "$image_pr" --no-cache . | |
| echo "::endgroup::" | |
| size_pr=$(docker image inspect "$image_pr" | jq '.[0].Size / (1024 * 1024) | round') | |
| diff=$((size_pr - size_base)) | |
| # echo "::warning::Image size change: $size_base -> $size_pr MB' (diff: $diff MB)" | |
| echo "comment to ${{ github.event.pull_request.number }}" | |
| if [ "$diff" -gt 50 ]; then | |
| curl -X POST \ | |
| -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| "https://api.github.com/repos/opea-project/GenAIComps/issues/${{ github.event.pull_request.number }}/comments" \ | |
| -d '{"body":"⚠️ File '"$dockerfile"' resulted in a change in the image size from '"$size_base"' -> '"$size_pr"' MB"}' | |
| else | |
| curl -X POST \ | |
| -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| "https://api.github.com/repos/opea-project/GenAIComps/issues/${{ github.event.pull_request.number }}/comments" \ | |
| -d '{"body":"File '"$dockerfile"' resulted in a change in the image size from '"$size_base"' -> '"$size_pr"' MB"}' | |
| fi | |
| # echo "File $dockerfile resulted in a change in the image size from $size_base -> $size_pr MB" >> $GITHUB_STEP_SUMMARY | |
| docker rmi "$image_base" "$image_pr" | |
| done < <(echo "${{ steps.changed-dockerfiles.outputs.files }}" | tr '\n' '\0') |