forked from PrefectHQ/prefect
-
Notifications
You must be signed in to change notification settings - Fork 0
112 lines (96 loc) · 3.8 KB
/
time-docker-build.yaml
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
name: Docker Build Time Benchmark
on:
push:
branches:
- main
paths:
- "Dockerfile"
- ".dockerignore"
pull_request:
paths:
- "Dockerfile"
- ".dockerignore"
jobs:
benchmark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# For PRs, checkout the base branch to compare against
- name: Checkout base branch
if: github.base_ref
uses: actions/checkout@v4
with:
ref: ${{ github.base_ref }}
clean: true
- name: Clean Docker system
run: |
docker system prune -af
docker builder prune -af
- name: Build base branch image
if: github.base_ref
id: base_build_time
run: |
start_time=$(date +%s)
DOCKER_BUILDKIT=1 docker build . --no-cache --progress=plain
end_time=$(date +%s)
base_time=$((end_time - start_time))
echo "base_time=$base_time" >> $GITHUB_OUTPUT
# For PRs, checkout back to the PR's HEAD
- name: Checkout PR branch
if: github.base_ref
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
clean: true
- name: Clean Docker system again
run: |
docker system prune -af
docker builder prune -af
- name: Build and time Docker image
id: build_time
run: |
start_time=$(date +%s)
DOCKER_BUILDKIT=1 docker build . --no-cache --progress=plain
end_time=$(date +%s)
build_time=$((end_time - start_time))
echo "build_time=$build_time" >> $GITHUB_OUTPUT
- name: Compare build times
run: |
CURRENT_TIME=${{ steps.build_time.outputs.build_time }}
if [ "${{ github.base_ref }}" != "" ]; then
BASE_TIME=${{ steps.base_build_time.outputs.base_time }}
echo "### 🏗️ Docker Build Time Comparison" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Branch | Build Time | Difference |" >> $GITHUB_STEP_SUMMARY
echo "|--------|------------|------------|" >> $GITHUB_STEP_SUMMARY
echo "| base (${{ github.base_ref }}) | ${BASE_TIME}s | - |" >> $GITHUB_STEP_SUMMARY
DIFF=$((CURRENT_TIME - BASE_TIME))
PERCENT=$(echo "scale=2; ($CURRENT_TIME - $BASE_TIME) * 100 / $BASE_TIME" | bc)
if [ $DIFF -gt 0 ]; then
DIFF_TEXT="⬆️ +${DIFF}s (+${PERCENT}%)"
elif [ $DIFF -lt 0 ]; then
DIFF_TEXT="⬇️ ${DIFF}s (${PERCENT}%)"
else
DIFF_TEXT="✨ No change"
fi
echo "| current (${{ github.head_ref }}) | ${CURRENT_TIME}s | $DIFF_TEXT |" >> $GITHUB_STEP_SUMMARY
# Fail if build time increased by more than 5%
if (( $(echo "$PERCENT > 5" | bc -l) )); then
echo "" >> $GITHUB_STEP_SUMMARY
echo "❌ **Build time increased by more than 5%!**" >> $GITHUB_STEP_SUMMARY
echo "This change significantly increases the build time. Please review the Dockerfile changes." >> $GITHUB_STEP_SUMMARY
exit 1
elif (( $(echo "$PERCENT < 0" | bc -l) )); then
echo "" >> $GITHUB_STEP_SUMMARY
echo "✅ **Build time decreased!**" >> $GITHUB_STEP_SUMMARY
echo "Great job optimizing the build!" >> $GITHUB_STEP_SUMMARY
fi
else
echo "### 🏗️ Docker Build Time" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Build completed in ${CURRENT_TIME} seconds" >> $GITHUB_STEP_SUMMARY
fi