-
Notifications
You must be signed in to change notification settings - Fork 0
206 lines (189 loc) · 10 KB
/
build.yml
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
# Nice article: https://piolabs.com/blog/insights/cicd-testing-coverage-versioning.html
name: Build and Release
on:
workflow_dispatch:
push:
branches: [ main, develop ]
jobs:
versioning:
name: Determine version, Build and Release
permissions: write-all
runs-on: ubuntu-latest
outputs:
branchname: ${{ steps.versioninfo.outputs.branchname }}
commithash: ${{ steps.versioninfo.outputs.commithash }}
buildtimestamp: ${{ steps.versioninfo.outputs.buildtimestamp }}
lastmajordigit: ${{ steps.versioninfo.outputs.lastmajordigit }}
lastminordigit: ${{ steps.versioninfo.outputs.lastminordigit }}
lastpatchdigit: ${{ steps.versioninfo.outputs.lastpatchdigit }}
lastversion: ${{ steps.versioninfo.outputs.lastversion }}
nextmajordigit: ${{ steps.selectversion.outputs.nextmajordigit }}
nextminordigit: ${{ steps.selectversion.outputs.nextminordigit }}
nextpatchdigit: ${{ steps.selectversion.outputs.nextpatchdigit }}
buildversion: ${{ steps.selectversion.outputs.buildversion }}
buildversionfilename: ${{ steps.selectversion.outputs.buildversionfilename }}
steps:
- name: Enable caching
uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get version data
id: versioninfo
run: |
echo "extract branch name from github_ref '${{ github.ref }}'"
declare branchname=$(echo "${{ github.ref }}" | cut -d'/' -f 3-)
echo "clean branch name = $branchname"
echo "extract commit short hash : $(git rev-parse --short HEAD)"
declare commithash=$(git rev-parse --short HEAD)
echo "extract build timestamp"
declare buildtimestamp=$(date "+%Y-%b-%d-%H:%M:%S")
echo "buildtimestamp = $buildtimestamp"
declare fulltag=$(git describe --tag $(git rev-parse --verify refs/remotes/origin/main))
echo "fulltag = [$fulltag]"
declare versiontag=$(echo $fulltag | cut -d'-' -f1)
echo "extract SemVer numbers from version tag [$versiontag]"
declare -i lastmajordigit=$(echo $versiontag | cut -c 2- | cut -d'.' -f1)
echo "lastmajordigit = $lastmajordigit"
declare -i lastminordigit=$(echo $versiontag | cut -c 1- | cut -d'.' -f2)
echo "lastminordigit = $lastminordigit"
declare -i lastpatchdigit=$(echo $versiontag | cut -c 1- | cut -d'.' -f3)
echo "lastpatchdigit = $lastpatchdigit"
declare lastversion="v$lastmajordigit.$lastminordigit.$lastpatchdigit"
echo "output variables to GitHub Actions"
echo "branchname=$branchname" >> $GITHUB_OUTPUT
echo "lastmajordigit=$lastmajordigit" >> $GITHUB_OUTPUT
echo "lastminordigit=$lastminordigit" >> $GITHUB_OUTPUT
echo "lastpatchdigit=$lastpatchdigit" >> $GITHUB_OUTPUT
echo "commithash=$commithash" >> $GITHUB_OUTPUT
echo "buildtimestamp=$buildtimestamp" >> $GITHUB_OUTPUT
echo "lastversion=$lastversion" >> $GITHUB_OUTPUT
- name: Determine which version to build
id: selectversion
run: |
echo "Triggered from Branch : ${{ steps.versioninfo.outputs.branchname }}"
echo "Commit hash : ${{ steps.versioninfo.outputs.commithash }}"
echo "Last version : ${{ steps.versioninfo.outputs.lastversion }}"
echo " Major : ${{ steps.versioninfo.outputs.lastmajordigit }}"
echo " Minor : ${{ steps.versioninfo.outputs.lastminordigit }}"
echo " Patch : ${{ steps.versioninfo.outputs.lastpatchdigit }}"
if [ "${{ steps.versioninfo.outputs.branchname }}" == "main" ]; then
echo "Triggered from merge on main branch with commit title : ${{ github.event.head_commit.message }}"
if [[ "${{ github.event.head_commit.message }}" == *"major"* ]]; then
echo "Incrementing Major versionDigit"
declare -i nextmajordigit=${{ steps.versioninfo.outputs.lastmajordigit }}+1
declare -i nextminordigit=0
declare -i nextpatchdigit=0
declare buildversion="v$nextmajordigit.$nextminordigit.$nextpatchdigit"
declare buildversionfilename=$(echo "SHIFTR-${buildversion}")
elif [[ "${{ github.event.head_commit.message }}" == *"minor"* ]]; then
echo "Incrementing Minor versionDigit"
declare -i nextmajordigit=${{ steps.versioninfo.outputs.lastmajordigit }}
declare -i nextminordigit=${{ steps.versioninfo.outputs.lastminordigit }}+1
declare -i nextpatchdigit=0
declare buildversion="v$nextmajordigit.$nextminordigit.$nextpatchdigit"
declare buildversionfilename=$(echo "SHIFTR-${buildversion}")
else
echo "Incrementing Patch versionDigit"
declare -i nextmajordigit=${{ steps.versioninfo.outputs.lastmajordigit }}
declare -i nextminordigit=${{ steps.versioninfo.outputs.lastminordigit }}
declare -i nextpatchdigit=${{ steps.versioninfo.outputs.lastpatchdigit }}+1
declare buildversion="v$nextmajordigit.$nextminordigit.$nextpatchdigit"
declare buildversionfilename=$(echo "SHIFTR-${buildversion}")
fi
else
echo "Not on main branch -> development version"
declare -i nextmajordigit=${{ steps.versioninfo.outputs.lastmajordigit }}
declare -i nextminordigit=${{ steps.versioninfo.outputs.lastminordigit }}
declare -i nextpatchdigit=${{ steps.versioninfo.outputs.lastpatchdigit }}
declare buildversion="v$nextmajordigit.$nextminordigit.$nextpatchdigit-${{ steps.versioninfo.outputs.commithash }}"
declare buildversionfilename=$(echo "SHIFTR-${buildversion}")
fi
echo "Building Version : $buildversion"
echo " Major : $nextmajordigit"
echo " Minor : $nextminordigit"
echo " Patch : $nextpatchdigit"
echo "Filename : $buildversionfilename"
echo "output variables to GitHub Actions"
echo "nextmajordigit=$nextmajordigit" >> $GITHUB_OUTPUT
echo "nextminordigit=$nextminordigit" >> $GITHUB_OUTPUT
echo "nextpatchdigit=$nextpatchdigit" >> $GITHUB_OUTPUT
echo "buildversion=$buildversion" >> $GITHUB_OUTPUT
echo "buildversionfilename=$buildversionfilename" >> $GITHUB_OUTPUT
- name: Show Build info
id: showbuildinfo
run: |
echo "Build Version : ${{ steps.selectversion.outputs.buildversion }}"
echo " Major : ${{ steps.selectversion.outputs.nextmajordigit }}"
echo " Minor : ${{ steps.selectversion.outputs.nextminordigit }}"
echo " Patch : ${{ steps.selectversion.outputs.nextpatchdigit }}"
echo "Build Filename : ${{ steps.selectversion.outputs.buildversionfilename }}.bin"
echo "Build Timestamp : ${{ steps.versioninfo.outputs.buildtimestamp }}"
- name: Save Build info
uses: "DamianReeves/write-file-action@master"
with:
path: include/Version.h
write-mode: overwrite
contents: |
// ##########################################################################
// ### This file is generated by build and Continuous Integration scripts ###
// ### .github/workflows/build.py for local development environment ###
// ### .github/workflows/build.yml for CI environment ###
// ### Changes will be overwritten on the next build! ###
// ##########################################################################
#ifndef VERSION_H
#define VERSION_H
#define VERSION "${{ steps.selectversion.outputs.buildversion }}"
#define VERSION_MAJOR ${{ steps.selectversion.outputs.nextmajordigit }}
#define VERSION_MINOR ${{ steps.selectversion.outputs.nextminordigit }}
#define VERSION_PATCH ${{ steps.selectversion.outputs.nextpatchdigit }}
#define VERSION_BUILD "${{ steps.versioninfo.outputs.commithash }}"
#define VERSION_TIMESTAMP "${{ steps.versioninfo.outputs.buildtimestamp }}"
#endif
- name: Verify Build info
run: |
cat include/Version.h
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.9"
- name: Install PlatformIO Core
run: pip install --upgrade platformio
- name: Build
run: |
pio run -e wt32-eth01
- name: Attach Binary to Workflow run
id: attachbinarytoworkflowrun
uses: actions/upload-artifact@v3
with:
name: ${{ steps.selectversion.outputs.buildversionfilename }}.bin
path: .pio/build/wt32-eth01/firmware.bin
if-no-files-found: error
- name: Release when on main branch
id: createrelease
uses: actions/create-release@v1
if: ${{ steps.versioninfo.outputs.branchname == 'main'}}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.selectversion.outputs.buildversion }}
release_name: Release ${{ steps.selectversion.outputs.buildversion }}
draft: false
prerelease: false
- name: Attach Binary to Release
id: attachbinarytorelease
uses: actions/upload-release-asset@v1
if: ${{ steps.versioninfo.outputs.branchname == 'main'}}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.createrelease.outputs.upload_url }}
asset_path: .pio/build/wt32-eth01/firmware.bin
asset_name: ${{ steps.selectversion.outputs.buildversionfilename }}.bin
asset_content_type: application/octet-stream