Skip to content

Commit 7d74b6d

Browse files
Add docker images size change check (opea-project#1843)
* Add docker images size change check Signed-off-by: ZePan110 <ze.pan@intel.com> * Add docker images size change check Signed-off-by: ZePan110 <ze.pan@intel.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * test Signed-off-by: ZePan110 <ze.pan@intel.com> * Fix path issue Signed-off-by: ZePan110 <ze.pan@intel.com> * Fix Signed-off-by: ZePan110 <ze.pan@intel.com> * Remove test code Signed-off-by: ZePan110 <ze.pan@intel.com> * Enhance and test Signed-off-by: ZePan110 <ze.pan@intel.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix issue Signed-off-by: ZePan110 <ze.pan@intel.com> * debug Signed-off-by: ZePan110 <ze.pan@intel.com> * test Signed-off-by: ZePan110 <ze.pan@intel.com> * Remove test code Signed-off-by: ZePan110 <ze.pan@intel.com> * Set fail-fast: false Signed-off-by: ZePan110 <ze.pan@intel.com> * Enhance and test Signed-off-by: ZePan110 <ze.pan@intel.com> * Remove test code and debug outputs Signed-off-by: ZePan110 <ze.pan@intel.com> * Add Info tag Signed-off-by: ZePan110 <ze.pan@intel.com> --------- Signed-off-by: ZePan110 <ze.pan@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8f850b4 commit 7d74b6d

1 file changed

Lines changed: 182 additions & 0 deletions

File tree

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
name: Check Docker Image Size Change
4+
permissions:
5+
contents: read
6+
7+
on:
8+
pull_request:
9+
branches: [main]
10+
types: [opened, reopened, ready_for_review, synchronize]
11+
paths:
12+
- '**/Dockerfile'
13+
14+
# If there is a new commit, the previous jobs will be canceled
15+
concurrency:
16+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
17+
cancel-in-progress: true
18+
19+
jobs:
20+
get-check-list:
21+
runs-on: ubuntu-latest
22+
outputs:
23+
files: ${{ steps.changed-dockerfiles.outputs.files }}
24+
steps:
25+
- name: Checkout PR branch
26+
uses: actions/checkout@v4
27+
with:
28+
fetch-depth: 0
29+
30+
- name: Get changed Dockerfiles
31+
id: changed-dockerfiles
32+
run: |
33+
merged_commit=$(git log -1 --format='%H')
34+
files=$(git diff --name-status --diff-filter=ARM ${{ github.event.pull_request.base.sha }} ${merged_commit} | awk '{print $2}' | grep -E 'Dockerfile$' | jq -R . | jq -sc .)
35+
echo "files=$files"
36+
echo "files=$files" >> $GITHUB_OUTPUT
37+
38+
build-and-check:
39+
needs: get-check-list
40+
runs-on: ubuntu-latest
41+
permissions:
42+
contents: read
43+
pull-requests: write
44+
if: needs.get-check-list.outputs.files != ''
45+
strategy:
46+
matrix:
47+
dockerfile: ${{ fromJson(needs.get-check-list.outputs.files) }}
48+
fail-fast: false
49+
steps:
50+
- name: Checkout PR branch
51+
uses: actions/checkout@v4
52+
with:
53+
fetch-depth: 0
54+
55+
- name: Set up Docker Buildx
56+
uses: docker/setup-buildx-action@v3
57+
58+
- name: Build and check image sizes
59+
env:
60+
dockerfile: ${{ matrix.dockerfile }}
61+
run: |
62+
set -e
63+
cd ${{github.workspace}}
64+
merged_commit=$(git log -1 --format='%H')
65+
66+
[ -z "$dockerfile" ] && continue
67+
dir=$(dirname "$dockerfile")
68+
image_base="pr-image-size-base:$(echo $dir | tr '/' '-')"
69+
image_pr="pr-image-size-pr:$(echo $dir | tr '/' '-')"
70+
71+
echo "Building base image for $dockerfile"
72+
echo "Base: ${{ github.event.pull_request.base.sha }}"
73+
git checkout ${{ github.event.pull_request.base.sha }}
74+
echo "::group::Build image_base"
75+
docker build -f "$dockerfile" -t "$image_base" --no-cache .
76+
echo "::endgroup::"
77+
78+
size_base=$(docker image inspect "$image_base" | jq '.[0].Size / (1024 * 1024) | round')
79+
80+
echo "Building PR image for $dockerfile"
81+
git checkout $merged_commit
82+
echo "PR: $merged_commit"
83+
echo "::group::Build image_pr"
84+
docker build -f "$dockerfile" -t "$image_pr" --no-cache .
85+
echo "::endgroup::"
86+
87+
size_pr=$(docker image inspect "$image_pr" | jq '.[0].Size / (1024 * 1024) | round')
88+
89+
diff=$((size_pr - size_base))
90+
# echo "::warning::Image size change: $size_base -> $size_pr MB' (diff: $diff MB)"
91+
echo "comment to ${{ github.event.pull_request.number }}"
92+
if [ "$diff" -gt 50 ]; then
93+
comment_message="⚠️ WARNING\nFile $dockerfile resulted in a change in the image size from $size_base -> $size_pr MB (diff: $diff MB)"
94+
else
95+
comment_message="ℹ️ INFO\nFile $dockerfile resulted in a change in the image size from $size_base -> $size_pr MB (diff: $diff MB)"
96+
fi
97+
98+
# echo "::warning::File $dockerfile resulted in a change in the image size from $size_base -> $size_pr MB" >> $GITHUB_STEP_SUMMARY
99+
docker rmi "$image_base" "$image_pr"
100+
101+
echo $comment_message >> $GITHUB_STEP_SUMMARY
102+
image_name=$(echo $dir | tr '/' '-')
103+
cp $GITHUB_STEP_SUMMARY ${{github.workspace}}/build-$image_name.md
104+
echo "summary_path=${{github.workspace}}/build-$image_name.md" >> $GITHUB_ENV
105+
106+
- name: Download origin artifact log
107+
uses: actions/download-artifact@v4
108+
with:
109+
name: build-comments
110+
path: merged-files
111+
continue-on-error: true
112+
113+
- name: Merge logs
114+
run: |
115+
mkdir -p merged-files
116+
ls merged-files/
117+
cp ${{ env.summary_path }} merged-files/
118+
119+
- name: Save Summary as Artifact
120+
uses: actions/upload-artifact@v4
121+
with:
122+
name: build-comments
123+
path: merged-files/
124+
overwrite: true
125+
126+
collect-comments:
127+
needs: build-and-check
128+
permissions:
129+
actions: read
130+
if: always()
131+
runs-on: ubuntu-latest
132+
outputs:
133+
all_comments: ${{ steps.summary.outputs.all_comments }}
134+
steps:
135+
- name: Download Summary
136+
uses: actions/download-artifact@v4
137+
with:
138+
name: build-comments
139+
path: downloaded-files
140+
141+
- name: Read Summary
142+
id: summary
143+
run: |
144+
all_comments=$(cat downloaded-files/*.md | jq -Rs .)
145+
echo "all_comments=$all_comments"
146+
echo "all_comments=$all_comments" >> $GITHUB_OUTPUT
147+
148+
Post-comment-on-PR:
149+
needs: collect-comments
150+
runs-on: ubuntu-latest
151+
permissions:
152+
contents: read
153+
pull-requests: write
154+
if: always() && needs.collect-comments.outputs.all_comments != ''
155+
steps:
156+
- name: Post comment on PR
157+
env:
158+
all_comments: ${{ needs.collect-comments.outputs.all_comments }}
159+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
160+
run: |
161+
all_comments=$(echo $all_comments | jq -r . | sed 's|\\n|\n|g')
162+
json_body=$(jq -n --arg msg "$all_comments" '{"body": $msg}')
163+
164+
comment_id=$(curl -s \
165+
-H "Authorization: token ${GITHUB_TOKEN}" \
166+
-H "Accept: application/vnd.github.v3+json" \
167+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
168+
| jq '.[] | select(.user.login=="github-actions[bot]") | .id' | tail -n1)
169+
170+
if [ -n "$comment_id" ]; then
171+
curl -X PATCH \
172+
-H "Authorization: token ${GITHUB_TOKEN}" \
173+
-H "Accept: application/vnd.github.v3+json" \
174+
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id" \
175+
-d "$json_body"
176+
else
177+
curl -X POST \
178+
-H "Authorization: token ${GITHUB_TOKEN}" \
179+
-H "Accept: application/vnd.github.v3+json" \
180+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
181+
-d "$json_body"
182+
fi

0 commit comments

Comments
 (0)