@@ -19,94 +19,53 @@ runs:
19
19
using : composite
20
20
steps :
21
21
- name : Check for pre-commit-config.yaml
22
- id : check -config
22
+ id : pre-commit -config
23
23
shell : bash
24
24
run : |
25
25
# Check for pre-commit-config.yaml
26
26
if [[ ! -f .pre-commit-config.yaml ]]; then
27
27
echo "::warning::No .pre-commit-config.yaml found, skipping pre-commit"
28
- echo "found-config =false" >> $GITHUB_OUTPUT
28
+ echo "exists =false" >> $GITHUB_OUTPUT
29
29
exit 0
30
30
fi
31
- - name : Cancel workflow
32
- uses : actions/github-script@v6
33
- if : steps.check-config.outputs.found-config == 'false'
34
- with :
35
- result-encoding : string
36
- retries : 3
37
- script : |
38
- github.rest.actions.cancelWorkflowRun({
39
- owner: context.repo.owner,
40
- repo: context.repo.repo,
41
- run_id: context.runId,
42
- });
43
-
44
- let count = 180;
45
- const check = () => {
46
- count -= 1;
47
- github.rest.actions.getWorkflowRun({
48
- owner: context.repo.owner,
49
- repo: context.repo.repo,
50
- run_id: context.runId,
51
- }).then((run) => {
52
- if (run.conclusion || run.status == "completed") {
53
- return
54
- }
55
- console.log("Waiting for workflow to cancel")
56
- console.log(run)
57
-
58
- if (count > 0) {
59
- setTimeout(check, 1000)
60
- } else {
61
- console.log("Workflow did not cancel in time")
62
- }
63
- })
64
- }
65
- setTimeout(check, 1)
66
-
67
- - name : Set node.js version
68
- if : hashFiles('.node-version') == ''
31
+ - name : Version files
69
32
shell : bash
70
33
run : |
71
- # Get the existing node version, if the command exists
72
- desired_version="$(node --version || true)"
73
- # Chain defaults to get a good value
74
- desired_version="${desired_version:-${NODENV_VERSION:-${NODE_VERSION:-16.19.1}}}"
75
- # Strip the leading "v" in e.g. "v16.0.0", since .node-version will not work with it
76
- desired_version="${desired_version#v}"
77
- # Set the version so action-setup-tools can use it later
78
- echo "$desired_version" > .node-version
79
- - name : Set Python version
80
- if : hashFiles('.python-version') == ''
81
- id : create-python-version-file
82
- shell : bash
83
- run : |
84
- # Use the .python-version from the action repo as the default version
34
+ # Try to use local repo .node-version
35
+ NODE_VERSION="${NODE_VERSION:-$(cat ".node-version")}"
36
+ # Try to use existing version
37
+ NODE_VERSION="${NODE_VERSION:-$(node --version || true)}"
38
+ # Try to use action default version
39
+ NODE_VERSION="${NODE_VERSION:-$(cat "$GITHUB_ACTION_PATH/.node-version")}"
40
+ # Write back the file if it doesn't exist
41
+ [[ -f .node-version ]] || { echo "${NODE_VERSION#v}" > .node-version; echo "CLEAN_NODE_VERSION=true" >> "$GITHUB_ENV"; }
42
+ echo "NODE_VERSION=${NODE_VERSION}" >> "$GITHUB_ENV"
43
+
44
+ # Try to use local repo .python-version
45
+ PYTHON_VERSION="${PYTHON_VERSION:-$(cat ".python-version")}"
46
+ # Try to use existing version
47
+ PYTHON_VERSION="${PYTHON_VERSION:-$(python --version || true)}"
48
+ # Try to use action default version
85
49
PYTHON_VERSION="${PYTHON_VERSION:-$(cat "$GITHUB_ACTION_PATH/.python-version")}"
86
- echo "$PYTHON_VERSION" > .python-version
50
+ # Write back the file if it doesn't exist
51
+ [[ -f .python-version ]] || { echo "${PYTHON_VERSION#v}" > .python-version; echo "CLEAN_PYTHON_VERSION=true" >> "$GITHUB_ENV"; }
87
52
echo "PYTHON_VERSION=${PYTHON_VERSION}" >> "$GITHUB_ENV"
88
- # Store as output that the file was created so we can clean up afterwards
89
- echo "file-created=true" >> "$GITHUB_OUTPUT"
90
- - name : Find pre-commit
91
- shell : bash
92
- run : |
53
+
93
54
# Finding pre-commit
94
55
# This will set an environment variable we can use to conditionally
95
56
# install pre-commit if needed and toggle how the action runs.
96
57
echo "PRE_COMMIT_BIN=$(command -v pre-commit)" >> $GITHUB_ENV
97
- - name : Find python
98
- shell : bash
99
- run : |
58
+
100
59
# Finding python
101
60
# This will set an environment variable we can use to conditionally
102
61
# install python.
103
62
echo "PYTHON_BIN=$(command -v python)" >> $GITHUB_ENV
104
63
- name : Setup python
105
64
# Only run this if we don't already have a pre-commit on the PATH
106
- if : env.PYTHON_BIN == null
65
+ if : env.PYTHON_BIN == null && env.PRE_COMMIT_BIN == null && steps.pre-commit-config.outputs.exists != 'false'
107
66
uses : actions/setup-python@v4
108
- # with:
109
- # python-version: 3.10.2
67
+ with :
68
+ python-version : ${{ env.PYTHON_VERSION }}
110
69
- name : Install tools
111
70
# This will skip the (slow) pyenv install if we triggered the above
112
71
uses : open-turo/action-setup-tools@v1
@@ -195,42 +154,40 @@ runs:
195
154
--color \
196
155
--from "$FROM" \
197
156
--to HEAD
198
- - name : Restore commitlint config
199
- if : always() && hashFiles(inputs.config-file) != '' && inputs.turo-conventional-commit == 'true'
200
- shell : bash
201
- run : git checkout -- "${{ inputs.config-file }}" || true
202
- # Collect file changes
203
- - name : Find changed files
204
- if : inputs.only-changed == 'true'
205
- uses : trilom/file-changes-action@v1.2.4
206
- id : file_changes
207
- with :
208
- output : " "
209
- # Export changed file names for use in pre-commit action
210
- - name : Limit pre-commit to changed files
157
+ - name : Get changed files
158
+ uses : tj-actions/changed-files@v40
211
159
if : inputs.only-changed == 'true'
160
+ id : changed-files
161
+ - name : Select files to run pre-commit against
212
162
shell : bash
213
163
run : |
214
- PRE_COMMIT_FILES="--files ${{ steps.file_changes.outputs.files}}"
215
- echo PRE_COMMIT_FILES="$PRE_COMMIT_FILES" >> $GITHUB_ENV
216
- # If we aren't only looking at changed files, then export the --all-files arg
217
- - name : Run pre-commit against all files
218
- if : inputs.only-changed != 'true'
219
- shell : bash
220
- run : echo PRE_COMMIT_FILES="--all-files" >> $GITHUB_ENV
221
- - name : Pre-commit
164
+ echo "PRE_COMMIT_ARGS=--all-files" >> "$GITHUB_ENV"
165
+ [[ "${{ steps.changed-files.outputs.any_changed }}" == "true" ]] || exit 0
166
+ if [[ "${{ inputs.only-changed }}" == "true" ]]; then
167
+ PRE_COMMIT_ARGS="--files ${{ steps.changed-files.outputs.files}}"
168
+ echo "PRE_COMMIT_ARGS=$PRE_COMMIT_ARGS" >> "$GITHUB_ENV"
169
+ fi
170
+ - name : Pre-commit (action)
222
171
# Same as above, this will install and run pre-commit for us
223
- if : env.PRE_COMMIT_BIN == null
172
+ if : env.PRE_COMMIT_BIN == null && steps.pre-commit-config.outputs.exists != 'false'
224
173
uses : pre-commit/action@v3.0.0
225
174
with :
226
- extra_args : ${{ env.PRE_COMMIT_FILES }}
227
- - name : Pre-commit
175
+ extra_args : ${{ env.PRE_COMMIT_ARGS }}
176
+ - name : Pre-commit (from PATH)
228
177
# Run pre-commit directly if we found it on the PATH
229
- if : env.PRE_COMMIT_BIN != null
178
+ if : env.PRE_COMMIT_BIN != null && steps.pre-commit-config.outputs.exists != 'false'
179
+ shell : bash
180
+ run : pre-commit run --show-diff-on-failure --color=always ${{ env.PRE_COMMIT_ARGS }}
181
+ - name : Clean up version files
182
+ if : always()
230
183
shell : bash
231
- run : pre-commit run --show-diff-on-failure --color=always ${{ env.PRE_COMMIT_FILES }}
232
- - name : Clean up
233
- # Delete the python version file if we created it
234
- if : steps.create-python-version-file.outputs.file-created == 'true'
184
+ run : |
185
+ # Clean up version files if we wrote them
186
+ [[ "${{ env.CLEAN_PYTHON_VERSION }}" == "" ]] || rm .python-version
187
+ [[ "${{ env.CLEAN_NODE_VERSION }}" == "" ]] || rm .node-version
188
+ - name : Restore commitlint config
189
+ if : always() && hashFiles(inputs.config-file) != '' && inputs.turo-conventional-commit == 'true'
235
190
shell : bash
236
- run : rm .python-version
191
+ run : |
192
+ # Restore the commitlint config file if we wrote it
193
+ git checkout -- "${{ inputs.config-file }}" || true
0 commit comments