1
+ name : Gas comparison
2
+
3
+ on :
4
+ pull_request :
5
+ branches : [main]
6
+ workflow_dispatch :
7
+
8
+ concurrency :
9
+ group : ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
10
+ cancel-in-progress : true
11
+
12
+ permissions :
13
+ contents : read
14
+ pull-requests : write
15
+
16
+ env :
17
+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
18
+
19
+ jobs :
20
+ files-changed :
21
+ runs-on : ubuntu-latest
22
+ outputs :
23
+ contracts : ${{ steps.changes.outputs.contracts }}
24
+ steps :
25
+ - name : checkout code
26
+ uses : actions/checkout@v4
27
+
28
+
29
+ id : changes
30
+ with :
31
+ filters : |
32
+ src:
33
+ - 'contracts/src/**'
34
+ - 'foundry.toml'
35
+
36
+ gas-comparison :
37
+ runs-on : [self-hosted, Linux, X64, prod, cpu]
38
+ needs : files-changed
39
+ if : needs.files-changed.outputs.contracts == 'true'
40
+ steps :
41
+ - name : checkout code
42
+ uses : actions/checkout@v4
43
+ with :
44
+ submodules : recursive
45
+
46
+ - name : install foundry
47
+ uses : foundry-rs/foundry-toolchain@v1
48
+
49
+ - name : Run gas report on PR branch
50
+ run : |
51
+ forge test --gas-report | grep "|" > pr-gas-report.txt
52
+
53
+ - name : Upload PR Gas Report
54
+ uses : actions/upload-artifact@v3
55
+ with :
56
+ name : pr-gas-report
57
+ path : pr-gas-report.txt
58
+
59
+ - name : checkout code
60
+ uses : actions/checkout@v4
61
+ with :
62
+ ref : main
63
+ submodules : recursive
64
+
65
+ - name : Run gas report on main branch
66
+ run : |
67
+ forge test --gas-report | grep "|" > main-gas-report.txt
68
+
69
+ - name : Download PR Gas Report
70
+ uses : actions/download-artifact@v3
71
+ with :
72
+ name : pr-gas-report
73
+ path : .
74
+
75
+ - name : Compare Gas Reports
76
+ run : |
77
+ echo "# Gas Cost Comparison for `${GITHUB_HEAD_REF}` vs `main`" > gas-comparison.md
78
+ echo "" >> gas-comparison.md
79
+ echo '```diff' >> gas-comparison.md
80
+
81
+ # Use diff to capture changes in gas costs
82
+ diff -u main-gas-report.txt pr-gas-report.txt >> gas-comparison.md || true
83
+
84
+ echo '```' >> gas-comparison.md
85
+ echo "" >> gas-comparison.md
86
+ cat gas-comparison.md
87
+
88
+ - name : Post Comment on Pull Request
89
+ run : |
90
+ COMMENT_BODY=$(cat gas-comparison.md)
91
+ COMMENT_PAYLOAD=$(jq -n --arg body "$COMMENT_BODY" '{body: $body}')
92
+ PR_NUMBER=${{ github.event.pull_request.number }}
93
+ curl -s -H "Authorization: token $GITHUB_TOKEN" \
94
+ -H "Content-Type: application/json" \
95
+ -X POST \
96
+ -d "$COMMENT_PAYLOAD" \
97
+ "https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments"
0 commit comments