Repository navigation
108 lines (96 loc) · 4.88 KB
/
Copy pathpre-commit.yaml
File metadata and controls
108 lines (96 loc) · 4.88 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
# ============== WARNING ==============================================================================
# File is managed by copier template: gh:LabAutomationAndScreening/copier-base-template.git
# See .config/.copier-managed-files.json for details.
#
# You are welcome to make changes to this file in your repo if they are custom to your project,
# but if the change should be shared with other projects, please backport it to the template repo.
# =====================================================================================================
name: Pre-commit
on:
workflow_call:
inputs:
python-version:
description: 'What version of python'
type: string
required: true
setup-node:
description: 'Whether to set up Node'
type: boolean
default: false
node-version:
description: 'What version of node'
type: string
required: false
default: 'notUsing'
env:
PYTHONUNBUFFERED: True
PRE_COMMIT_HOME: ${{ github.workspace }}/.precommit_cache
permissions:
contents: write # needed for mutex
id-token: write # needed to assume OIDC roles (e.g. for downloading from CodeArtifact)
jobs:
pre-commit:
runs-on: ubuntu-24.04
timeout-minutes: 8
name: Pre-commit
steps:
- name: Checkout code during push
if: ${{ github.event_name == 'push' }}
uses: actions/checkout@v7.0.1
with:
ref: ${{ github.ref_name }} # explicitly get the head of the branch, which will include any new commits pushed if this is a dependabot branch
persist-credentials: false
- name: Checkout code not during push
if: ${{ github.event_name != 'push' }}
uses: actions/checkout@v7.0.1
with:
persist-credentials: false
- name: Install latest versions of packages
uses: ./.github/actions/install_deps
env:
SKIP_PLAYWRIGHT_INSTALL: "1"
with:
python-version: ${{ inputs.python-version }}
node-version: ${{ inputs.node-version }}
skip-installing-ssm-plugin-manager: true
skip-installing-pulumi-cli: true
- name: Compute Pre-commit cache key # must run after checkout, since hashFiles needs the file on disk
run: |
echo "CACHE_RESTORE_KEY=ubuntu-24.04-py${{ inputs.python-version }}-node-${{ inputs.node-version}}-cache-pre-commit-hooks-" >> "$GITHUB_ENV"
echo "CACHE_KEY=ubuntu-24.04-py${{ inputs.python-version }}-node-${{ inputs.node-version}}-cache-pre-commit-hooks-${{ hashFiles('.pre-commit-config.yaml') }}" >> "$GITHUB_ENV"
- name: Restore Pre-commit hooks cache # no mutex needed; concurrent cache reads are safe, only writes cause trouble
id: cache-probe
uses: actions/cache/restore@v6.1.0
with:
path: ${{ env.PRE_COMMIT_HOME }}
key: ${{ env.CACHE_KEY }}
restore-keys: ${{ env.CACHE_RESTORE_KEY }}
- name: Set up mutex # Github concurrency management is horrible, things get arbitrarily cancelled if queued up. So using mutex until github fixes itself. When multiple jobs are modifying cache at once, weird things can happen. possible issue is https://github.com/actions/toolkit/issues/658
if: ${{ runner.os != 'Windows' && steps.cache-probe.outputs.cache-hit != 'true' }} # we're just gonna have to YOLO on Windows, because this action doesn't support it yet https://github.com/ben-z/gh-action-mutex/issues/14. Only serialize when this job will have to write a new cache entry.
uses: ben-z/gh-action-mutex@1ebad517141198e08d47cf72f3c0975316620a65 # v1.0.0-alpha.10
with:
branch: mutex-venv-ubuntu-24.04-py${{ inputs.python-version }}-nodejs-${{ inputs.node-version }}
timeout-minutes: 8 # this is the amount of time this action will wait to attempt to acquire the mutex lock before failing, e.g. if other jobs are queued up in front of it
- name: Re-restore Pre-commit hooks cache # another job may have created the exact-key cache while we waited on the mutex
id: cache-recheck
if: ${{ steps.cache-probe.outputs.cache-hit != 'true' }}
uses: actions/cache/restore@v6.1.0
with:
path: ${{ env.PRE_COMMIT_HOME }}
key: ${{ env.CACHE_KEY }}
restore-keys: ${{ env.CACHE_RESTORE_KEY }}
- name: Run pre-commit
run: |
pre-commit run -a || PRE_COMMIT_EXIT_CODE=$?
if [ -n "$PRE_COMMIT_EXIT_CODE" ]; then
echo "Pre-commit failed with exit code $PRE_COMMIT_EXIT_CODE"
echo "Showing git diff:"
git --no-pager diff
exit $PRE_COMMIT_EXIT_CODE
fi
- name: Save Pre-commit hooks cache
if: ${{ steps.cache-probe.outputs.cache-hit != 'true' && steps.cache-recheck.outputs.cache-hit != 'true' }}
uses: actions/cache/save@v6.1.0
with:
path: ${{ env.PRE_COMMIT_HOME }}
key: ${{ env.CACHE_KEY }}