Skip to content

Commit bf0bb96

Browse files
CI: Workflow to calculate build size diff before & after PR/commit. (#262)
1 parent 0812b03 commit bf0bb96

File tree

2 files changed

+143
-9
lines changed

2 files changed

+143
-9
lines changed

.github/workflows/build.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,26 @@ jobs:
4343
repository: 'lancaster-university/microbit-v2-samples'
4444
# We need to use the checkout action (instead of build.py cloning the
4545
# repository) so that on PRs we can get the commit from the PR merge
46-
- name: Clone the this repository in the libraries folder
46+
- name: Clone this repository in the libraries folder
4747
uses: actions/checkout@v3
4848
with:
4949
path: libraries/codal-microbit-v2
50-
fetch-depth: '0'
5150
# Changing the commit SHA might be unnecessary, as we've already cloned this
5251
# repo, but could be useful to ensure codal.json points to the same commit
5352
- name: Modify codal.json files to use this codal-microbit-v2 commit
5453
shell: bash
5554
run: |
5655
echo "codal.json before:"
5756
cat codal.json
58-
python -c "import pathlib; \
59-
f1 = pathlib.Path('codal.json'); \
60-
f1.write_text(f1.read_text().replace(',\n \"dev\": true', '')); \
61-
f1.write_text(f1.read_text().replace('master', '${GITHUB_SHA}')); \
62-
f2 = pathlib.Path('codal.ble.json'); \
63-
f2.write_text(f2.read_text().replace('master', '${GITHUB_SHA}')); \
64-
f2.write_text(f2.read_text().replace(',\n \"dev\": true', ''))"
57+
python - << EOF
58+
import pathlib
59+
for codal_file in ['codal.json', 'codal.ble.json']:
60+
f = pathlib.Path(codal_file)
61+
f.write_text(f.read_text() \
62+
.replace('lancaster-university/codal-microbit-v2', '${GITHUB_REPOSITORY}') \
63+
.replace('master', '${GITHUB_SHA}') \
64+
.replace(',\n \"dev\": true', ''))
65+
EOF
6566
echo "codal.json after:"
6667
cat codal.json
6768
- name: Build default (non-ble) project using build.py

.github/workflows/size-diff.yml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
name: Bloaty size diff
2+
3+
on:
4+
pull_request:
5+
branches: '*'
6+
push:
7+
branches: '*'
8+
9+
jobs:
10+
size-diff:
11+
name: Run Bloaty
12+
runs-on: ubuntu-latest
13+
steps:
14+
#########################
15+
# Install the toolchain #
16+
#########################
17+
- name: Setup Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: '3.8'
21+
- name: Setup arm-none-eabi-gcc 10.3
22+
uses: carlosperate/arm-none-eabi-gcc-action@v1
23+
with:
24+
release: 10.3-2021.10
25+
- name: Install Ninja 1.11 & CMake 3.25 via PyPI
26+
run: python -m pip install ninja==1.11.1 cmake==3.25.0
27+
28+
#########################################
29+
# Set up the CODAL project and build it #
30+
#########################################
31+
- name: Clone the microbit-v2-samples repository
32+
uses: actions/checkout@v3
33+
with:
34+
repository: 'lancaster-university/microbit-v2-samples'
35+
# We need to use the checkout action (instead of build.py cloning the
36+
# repository) so that on PRs we can get the commit from the PR merge
37+
- name: Clone this repository in the libraries folder
38+
uses: actions/checkout@v3
39+
with:
40+
path: libraries/codal-microbit-v2
41+
fetch-depth: '0'
42+
# Changing the commit SHA might be unnecessary, as we've already cloned this
43+
# repo, but could be useful to ensure codal.json points to the same commit
44+
- name: Modify files to use BLE & this codal-microbit-v2 commit
45+
shell: bash
46+
run: |
47+
echo "coda.json before:"
48+
cat codal.json
49+
mv codal.ble.json codal.json
50+
python - << EOF
51+
import pathlib;
52+
f = pathlib.Path('codal.json');
53+
f.write_text(f.read_text() \
54+
.replace('lancaster-university/codal-microbit-v2', '${GITHUB_REPOSITORY}') \
55+
.replace('master', '${GITHUB_SHA}') \
56+
.replace(',\n \"dev\": true', ''))
57+
f = pathlib.Path('source/main.cpp')
58+
f.write_text(f.read_text().replace('out_of_box_experience()', 'ble_test()'))
59+
EOF
60+
echo "coda.json after:"
61+
cat codal.json
62+
- name: Build using build.py
63+
run: python build.py
64+
- name: Save ELF file in a different directory
65+
run: |
66+
mkdir original-build
67+
mv build/MICROBIT original-build/MICROBIT.elf
68+
# Manually clean the project, but keep the codal-microbit-v2 library
69+
# If the codal-microbit-v2 target adds more libs this step will need to include them as well
70+
- name: Clean project
71+
run: rm -rf build/ libraries/codal-core libraries/codal-microbit-nrf5sdk libraries/codal-nrf52
72+
73+
####################################################################
74+
# Set up codal-microbit-v2 to a parent/base commit and build again #
75+
####################################################################
76+
- name: "PR only: Get base commit SHA"
77+
if: ${{ github.event.pull_request.base.sha }}
78+
run: |
79+
echo "${{ github.event.pull_request.base.sha }}"
80+
echo "GIT_BASE_SHA=${{ github.event.pull_request.base.sha }}" >> $GITHUB_ENV
81+
echo "# Bloaty comparison with PR base commit" >> $GITHUB_STEP_SUMMARY
82+
echo "Base commit: ${{ github.event.pull_request.base.sha }}" >> $GITHUB_STEP_SUMMARY
83+
- name: "Commit only: Get parent commit SHA"
84+
if: ${{ ! github.event.pull_request.base.sha }}
85+
run: |
86+
cd libraries/codal-microbit-v2
87+
echo "$(git log --pretty=%P -n 1 HEAD^0)"
88+
echo "GIT_BASE_SHA=$(git log --pretty=%P -n 1 HEAD^0)" >> $GITHUB_ENV
89+
echo "# Bloaty comparison with parent commit" >> $GITHUB_STEP_SUMMARY
90+
echo "Parent commit: $(git log --pretty=%P -n 1 HEAD^0)" >> $GITHUB_STEP_SUMMARY
91+
# We don't need to update codal.json because we've kept the
92+
# codal-microbit-v2 repo and we manually check out the right base commit
93+
- name: Checkout parent/base commit of codal-microbit-v2
94+
run: |
95+
cd libraries/codal-microbit-v2
96+
git checkout ${GIT_BASE_SHA}
97+
- name: Build 'base' project using build.py
98+
run: python build.py --clean
99+
100+
#######################################
101+
# Run the Bloaty McBloatface analysis #
102+
#######################################
103+
# 1st run the bloaty diff so that it's added to the top of the summary page
104+
- name: Run Bloaty to compare before and after ELF files
105+
id: bloaty-comparison
106+
uses: carlosperate/bloaty-action@v1
107+
with:
108+
bloaty-args: -s vm original-build/MICROBIT.elf -- build/MICROBIT
109+
output-to-summary: true
110+
# Then show memory consumption of top 30 components for this build
111+
- name: Run Bloaty to view ELF file full info
112+
uses: carlosperate/bloaty-action@v1
113+
with:
114+
bloaty-args: original-build/MICROBIT.elf -d compileunits -n 30 -s vm
115+
output-to-summary: true
116+
- name: Add comment to PR with the bloaty diff
117+
if: ${{ github.event.pull_request }}
118+
continue-on-error: true
119+
uses: actions/github-script@v6
120+
with:
121+
script: |
122+
let prComment = '## Build diff\n' +
123+
'Base commit: [${{ env.GIT_BASE_SHA }}](${{ github.server_url }}/${{ github.repository }}/commit/${{ env.GIT_BASE_SHA }})\n' +
124+
'Action run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\n' +
125+
'```\n' +
126+
'${{ steps.bloaty-comparison.outputs.bloaty-output-encoded }}' +
127+
'```\n'
128+
github.rest.issues.createComment({
129+
issue_number: context.issue.number,
130+
owner: context.repo.owner,
131+
repo: context.repo.repo,
132+
body: prComment
133+
})

0 commit comments

Comments
 (0)