-
Notifications
You must be signed in to change notification settings - Fork 337
208 lines (186 loc) · 7.68 KB
/
pr-image-size.yml
File metadata and controls
208 lines (186 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# 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:
get-check-list:
runs-on: ubuntu-latest
outputs:
files: ${{ steps.changed-dockerfiles.outputs.files }}
steps:
- name: Checkout PR branch
uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2
with:
fetch-depth: 0
- 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$' | jq -R . | jq -sc .)
echo "files=$files"
echo "files=$files" >> $GITHUB_OUTPUT
build-and-check:
needs: get-check-list
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
if: needs.get-check-list.outputs.files != ''
strategy:
matrix:
dockerfile: ${{ fromJson(needs.get-check-list.outputs.files) }}
fail-fast: false
outputs:
skip: ${{ steps.build-check.outputs.skip }}
steps:
- name: Checkout PR branch
uses: actions/checkout@1e31de5234b9f8995739874a8ce0492dc87873e2
with:
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb
- name: Build and check image sizes
id: build-check
env:
dockerfile: ${{ matrix.dockerfile }}
run: |
merged_commit=$(git log -1 --format='%H')
[ -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 '/' '-')"
slash_count=$(grep -o "/" <<< "$dockerfile" | wc -l)
if [ "$slash_count" -eq 1 ]; then
cd "${{github.workspace}}/$dir"
elif [ "$slash_count" -gt 1 ]; then
if [[ "$dockerfile" == *"ui/"* ]]; then
dir=$(echo "$dir" | sed 's|\(.*\)/ui.*|\1/ui|')
cd "${{github.workspace}}/$dir"
file=docker/$file
image_base="pr-image-size-base:$(echo $dir | tr '/' '-')"
image_pr="pr-image-size-pr:$(echo $dir | tr '/' '-')"
elif [[ "$dockerfile" == *"WorkflowExecAgent/tests"* ]]; then
echo "Skipping $dockerfile"
exit 0
else
echo "Error: Multiple '/' in dockerfile path but no 'ui/' found."
exit 1
fi
fi
echo "Building base image for $dockerfile"
git checkout ${{ github.event.pull_request.base.sha }}
echo "::group::Build image_base"
if ! docker build -f "$file" -t "$image_base" --no-cache .; then
echo "skip=true" >> $GITHUB_ENV
echo "skip=true" >> $GITHUB_OUTPUT
exit 0
fi
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 $file -t "$image_pr" --no-cache . || true
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
comment_message="⚠️ WARNING\nFile $dockerfile resulted in a change in the image size from $size_base -> $size_pr MB (diff: $diff MB)"
else
comment_message="ℹ️ INFO\nFile $dockerfile resulted in a change in the image size from $size_base -> $size_pr MB (diff: $diff MB)"
fi
# echo "comment_message=$comment_message" >> $GITHUB_OUTPUT
# 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"
echo $comment_message >> $GITHUB_STEP_SUMMARY
image_name=$(echo $dir | tr '/' '-')
cp $GITHUB_STEP_SUMMARY ${{github.workspace}}/build-$image_name.md
echo "summary_path=${{github.workspace}}/build-$image_name.md" >> $GITHUB_ENV
- name: Download origin artifact log
if: env.skip != 'true'
uses: actions/download-artifact@v4.1.3
with:
name: build-comments
path: merged-files
continue-on-error: true
- name: Merge logs
if: env.skip != 'true'
run: |
mkdir -p merged-files
ls merged-files/
cp ${{ env.summary_path }} merged-files/
- name: Save Summary as Artifact
if: env.skip != 'true'
uses: actions/upload-artifact@c7d193f32edcb7bfad88892161225aeda64e9392
with:
name: build-comments
path: merged-files/
overwrite: true
collect-comments:
needs: build-and-check
permissions:
actions: read
if: always() && needs.build-and-check.outputs.skip != 'true'
runs-on: ubuntu-latest
outputs:
all_comments: ${{ steps.summary.outputs.all_comments }}
steps:
- name: Download Summary
uses: actions/download-artifact@v4.1.3
with:
name: build-comments
path: downloaded-files
- name: Read Summary
id: summary
run: |
all_comments=$(cat downloaded-files/*.md | jq -Rs .)
echo "all_comments=$all_comments"
echo "all_comments=$all_comments" >> $GITHUB_OUTPUT
Post-comment-on-PR:
needs: collect-comments
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
if: always() && needs.collect-comments.outputs.all_comments != ''
steps:
- name: Post comment on PR
env:
all_comments: ${{ needs.collect-comments.outputs.all_comments }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
all_comments=$(echo $all_comments | jq -r . | sed 's|\\n|\n|g')
json_body=$(jq -n --arg msg "$all_comments" '{"body": $msg}')
comment_id=$(curl -s \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
| jq '.[] | select(.user.login=="github-actions[bot]") | .id' | tail -n1)
if [ -n "$comment_id" ]; then
curl -X PATCH \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id" \
-d "$json_body"
else
curl -X POST \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
-d "$json_body"
fi