-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
346 lines (325 loc) · 12.3 KB
/
action.yml
File metadata and controls
346 lines (325 loc) · 12.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
name: 'PyBuilder Action'
description: 'PyBuilder Composite GitHub Action'
branding:
icon: zap
color: blue
inputs:
checkout:
description: 'Do checkout first'
required: false
default: 'true'
install-python:
description: 'Whether to install Python'
required: false
default: 'true'
homebrew-python:
description: 'Whether Python should be installed via Homebrew'
required: false
default: 'false'
python-version:
description: 'Python version to use, if installing'
required: false
default: '3.11'
architecture:
description: 'Install Python for specific architecture, if installing'
required: false
default: x64
with-venv:
description: 'Whether to use Virtualenv during a build'
required: false
default: 'true'
install-pyb:
description: 'Install PyBuilder'
required: false
default: 'true'
pyb-version:
description: 'PyBuilder version to install'
required: false
default: '>=0.12.0'
pyb-command:
description: 'Command to run PyBuilder'
required: false
default: 'pyb'
pyb-args:
description: 'PyBuilder command line arguments'
required: false
default: "-E ci -v -X"
pyb-extra-args:
description: 'PyBuilder extra command line arguments'
required: false
default: ""
pre-build:
description: 'Run a script before PyBuilder build'
required: false
default: ""
is-upload:
description: 'Whether this matrix slot runs the PyBuilder upload task'
required: false
default: 'false'
auto-release:
description: 'Enable release automation on [release] in commit message'
required: false
default: 'true'
auto-release-version:
description: 'Strip .dev from version when [release] detected'
required: false
default: 'true'
auto-github-release:
description: 'Create GitHub Release with tag (upload job only)'
required: false
default: 'true'
auto-bump-dev:
description: 'Bump to next .dev version after release (upload job only)'
required: false
default: 'true'
release-tag-prefix:
description: 'Prefix for release tags'
required: false
default: 'v'
release-title-template:
description: 'GitHub Release title template ({version} placeholder)'
required: false
default: 'Release v{version}'
build-py-path:
description: 'Path to build.py relative to repo root'
required: false
default: 'build.py'
github-token:
description: 'Token for git push and gh CLI; override with PAT for protected branches'
required: false
default: ${{ github.token }}
release-app-id:
description: 'GitHub App ID for generating release tokens that bypass branch protection'
required: false
default: ''
release-app-private-key:
description: 'GitHub App private key (PEM) for generating release tokens'
required: false
default: ''
outputs:
is-release:
description: 'true if [release] was detected in commit message'
value: ${{ steps.release-detect.outputs.is-release }}
release-version:
description: 'The release version, empty if not a release'
value: ${{ steps.release-commit.outputs.version }}
runs:
using: "composite"
steps:
- name: Configure Python Executable
shell: bash
run: |
PV='${{inputs.python-version}}'
PV="${PV##pypy-}"
echo "PYTHON=python${PV%%.*}" >> $GITHUB_ENV
- name: Checkout
if: inputs.checkout == 'true'
uses: actions/checkout@v6
with:
persist-credentials: ${{ inputs.release-app-id == '' }}
- name: Detect Release
id: release-detect
shell: bash
if: inputs.auto-release == 'true' && github.event_name == 'push'
env:
COMMIT_MSG: ${{ github.event.head_commit.message }}
run: |
if [[ "$COMMIT_MSG" =~ \[release[[:space:]]+([0-9][0-9A-Za-z._]*)\] ]]; then
echo "is-release=true" >> $GITHUB_OUTPUT
echo "explicit-version=${BASH_REMATCH[1]}" >> $GITHUB_OUTPUT
elif [[ "$COMMIT_MSG" =~ \[release\] ]]; then
echo "is-release=true" >> $GITHUB_OUTPUT
fi
- name: Setup Python
if: inputs.install-python == 'true' && (runner.os != 'macOS' || inputs.homebrew-python != 'true')
uses: actions/setup-python@v6
with:
python-version: '${{ inputs.python-version }}'
architecture: '${{ inputs.architecture }}'
- name: Setup Python with Homebrew
shell: bash
if: inputs.install-python == 'true' && runner.os == 'macOS' && inputs.homebrew-python == 'true'
run: |
brew update
brew upgrade --overwrite
brew uninstall --force --ignore-dependencies 'python@${{ inputs.python-version }}' || true
brew install --overwrite 'python@${{ inputs.python-version }}'
echo "/usr/local/opt/python@${{ inputs.python-version }}/Frameworks/Python.framework/Versions/${{ inputs.python-version }}/bin" >> $GITHUB_PATH
echo "/usr/local/opt/python@${{ inputs.python-version }}/libexec/bin" >> $GITHUB_PATH
echo "/usr/local/opt/python@${{ inputs.python-version }}/bin" >> $GITHUB_PATH
brew cleanup
- name: Python Pre-VENV Check
shell: bash
run: |
pwd
echo $PATH
which $PYTHON
$PYTHON --version
$PYTHON -m pip --version
# $PYTHON -c "import sysconfig; import pprint; pprint.pprint(sysconfig.get_config_vars())"
- name: Create VEnv
shell: bash
if: inputs.with-venv == 'true'
run: |
$PYTHON -m pip install --break-system-packages virtualenv
virtualenv $HOME/.pyb
echo "$HOME/.pyb/bin" >> $GITHUB_PATH
- name: Python Post-VENV Check
shell: bash
run: |
pwd
echo $PATH
which $PYTHON
$PYTHON --version
$PYTHON -m pip --version
# $PYTHON -c "import sysconfig; import pprint; pprint.pprint(sysconfig.get_config_vars())"
- name: Install PyBuilder
shell: bash
if: inputs.install-pyb == 'true'
run: |
$PYTHON -m pip install --break-system-packages 'pybuilder${{ inputs.pyb-version }}'
which pyb
pyb --version
- name: Configure Upload
shell: bash
if: inputs.is-upload == 'true'
run: |
if [ "$IS_RELEASE" = "true" ]; then
if [[ "$PYB_EXTRA_ARGS" =~ (^|[[:space:]])\+?upload($|[[:space:]]) ]]; then
echo "ERROR: pyb-extra-args contains 'upload' or '+upload' but [release] mode is active."
echo "Remove the upload task from pyb-extra-args and use is-upload instead."
exit 1
fi
fi
echo "PYB_UPLOAD=+upload" >> $GITHUB_ENV
env:
IS_RELEASE: ${{ steps.release-detect.outputs.is-release }}
PYB_EXTRA_ARGS: ${{ inputs.pyb-extra-args }}
- name: Set Release Version
id: release-version
shell: bash
if: >-
steps.release-detect.outputs.is-release == 'true' &&
inputs.auto-release-version == 'true'
run: |
TOOL_PATH="${GITHUB_ACTION_PATH}/version_tool.py"
EXPLICIT="${{ steps.release-detect.outputs.explicit-version }}"
if [ -n "$EXPLICIT" ]; then
$PYTHON "$TOOL_PATH" set-release "${{ inputs.build-py-path }}" "$EXPLICIT"
else
$PYTHON "$TOOL_PATH" set-release "${{ inputs.build-py-path }}"
fi
VERSION_JSON=$($PYTHON "$TOOL_PATH" read "${{ inputs.build-py-path }}")
VERSION=$(echo "$VERSION_JSON" | $PYTHON -c "import sys,json; print(json.load(sys.stdin)['version'])")
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Pre-Build
shell: bash
if: inputs.pre-build != ''
run: |
${{ inputs.pre-build }}
- name: Build
shell: bash
run: ${{ inputs.pyb-command }} ${{ inputs.pyb-args }} ${{ inputs.pyb-extra-args }} $PYB_UPLOAD
- name: Generate Release Token
id: release-token
if: >-
inputs.is-upload == 'true' &&
steps.release-detect.outputs.is-release == 'true' &&
inputs.release-app-id != '' &&
inputs.release-app-private-key != ''
uses: actions/create-github-app-token@v2
with:
app-id: ${{ inputs.release-app-id }}
private-key: ${{ inputs.release-app-private-key }}
- name: Release Commit and Tag
id: release-commit
shell: bash
if: >-
inputs.is-upload == 'true' &&
steps.release-detect.outputs.is-release == 'true'
run: |
TOOL_PATH="${GITHUB_ACTION_PATH}/version_tool.py"
DEFAULT_BRANCH="${{ github.ref_name }}"
VERSION="${{ steps.release-version.outputs.version }}"
if [ -z "$VERSION" ]; then
VERSION_JSON=$($PYTHON "$TOOL_PATH" read "${{ inputs.build-py-path }}")
VERSION=$(echo "$VERSION_JSON" | $PYTHON -c "import sys,json; print(json.load(sys.stdin)['version'])")
fi
TAG="${{ inputs.release-tag-prefix }}${VERSION}"
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Remove checkout's credential helper so the app token in the URL is used
git config --unset-all http.https://github.com/.extraheader || true
git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${{ github.repository }}.git"
# Unshallow if needed — shallow clones can cause push failures
if [ -f "$(git rev-parse --git-dir)/shallow" ]; then
git fetch --unshallow origin
fi
HAVE_RELEASE_COMMIT=false
if ! git diff --quiet "${{ inputs.build-py-path }}" 2>/dev/null; then
git add "${{ inputs.build-py-path }}"
git commit -m "Release version ${VERSION} [skip ci]"
HAVE_RELEASE_COMMIT=true
fi
git tag "$TAG"
PUSH_REFS=("refs/tags/${TAG}")
if [ "$HAVE_RELEASE_COMMIT" = "true" ]; then
PUSH_REFS+=("HEAD:refs/heads/${DEFAULT_BRANCH}")
fi
PUSH_STDERR=$(mktemp)
if git push --atomic origin "${PUSH_REFS[@]}" 2>"$PUSH_STDERR"; then
echo "is-coordinator=true" >> $GITHUB_OUTPUT
echo "tag=${TAG}" >> $GITHUB_OUTPUT
echo "version=${VERSION}" >> $GITHUB_OUTPUT
else
PUSH_ERR=$(cat "$PUSH_STDERR")
rm -f "$PUSH_STDERR"
# Check if this is a tag collision (another matrix job already pushed)
if git ls-remote --tags origin "$TAG" | grep -q "$TAG"; then
echo "Tag ${TAG} already exists on remote — another job is handling the release"
echo "is-coordinator=false" >> $GITHUB_OUTPUT
if [ "$HAVE_RELEASE_COMMIT" = "true" ]; then
git reset --soft HEAD~1
fi
else
echo "::error::git push --atomic failed for refs: ${PUSH_REFS[*]}"
echo "::error::git stderr: ${PUSH_ERR}"
exit 1
fi
fi
rm -f "$PUSH_STDERR"
env:
GH_TOKEN: ${{ steps.release-token.outputs.token || inputs.github-token }}
- name: Create GitHub Release
shell: bash
if: >-
steps.release-commit.outputs.is-coordinator == 'true' &&
inputs.auto-github-release == 'true'
run: |
VERSION="${{ steps.release-commit.outputs.version }}"
TAG="${{ steps.release-commit.outputs.tag }}"
TITLE="${{ inputs.release-title-template }}"
TITLE="${TITLE//\{version\}/$VERSION}"
gh release create "$TAG" \
--verify-tag \
--title "$TITLE" \
--generate-notes
env:
GH_TOKEN: ${{ steps.release-token.outputs.token || inputs.github-token }}
- name: Bump Dev Version
shell: bash
if: >-
steps.release-commit.outputs.is-coordinator == 'true' &&
inputs.auto-bump-dev == 'true'
run: |
TOOL_PATH="${GITHUB_ACTION_PATH}/version_tool.py"
DEFAULT_BRANCH="${{ github.ref_name }}"
$PYTHON "$TOOL_PATH" bump-dev "${{ inputs.build-py-path }}"
VERSION_JSON=$($PYTHON "$TOOL_PATH" read "${{ inputs.build-py-path }}")
NEW_VERSION=$(echo "$VERSION_JSON" | $PYTHON -c "import sys,json; print(json.load(sys.stdin)['version'])")
git add "${{ inputs.build-py-path }}"
git commit -m "Bump version to ${NEW_VERSION} [skip ci]"
git pull --rebase origin "${DEFAULT_BRANCH}"
git push origin "HEAD:refs/heads/${DEFAULT_BRANCH}"
env:
GH_TOKEN: ${{ steps.release-token.outputs.token || inputs.github-token }}