-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaction.yaml
More file actions
175 lines (171 loc) · 7.01 KB
/
Copy pathaction.yaml
File metadata and controls
175 lines (171 loc) · 7.01 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
name: Pre-commit
description: GitHub Action for running pre-commit hooks against the repository. Conditionally installs tools needed for the Action to be able to perform its duties such as `npm`, `pre-commit`, etc.
inputs:
config-file:
required: true
description: The config file to present to commitlint-github-action
default: .commitlintrc.yaml
disable-commitlint:
required: false
description: Set this to "true" to disable commitlint entirely
default: "false"
turo-conventional-commit:
required: true
description: Set this to "false" to customize conventional commit configuration
default: "true"
only-changed:
required: false
description: >-
Set this to "true" to only run pre-commit against changed files, and not
the entire repository
stage:
required: false
description: Set this to run pre-commit against a specific stage of the pre-commit hooks.
s3-bucket-name:
required: false
description: S3 bucket name to cache node_modules to speed up dependency installation.
s3-bucket-region:
required: false
description: S3 bucket region to cache node_modules to speed up dependency installation.
github-token:
required: false
description: Sets a GitHub token that allows pre-commit to fetch private repos
outputs:
cache-hit:
description: Whether the cache was hit when installing dependencies
value: ${{ steps.read_cache.outputs.cache-hit }}
runs:
using: composite
steps:
- name: Setup Git Auth for Private GitHub Repos
if: inputs.github-token != ''
uses: open-turo/action-git-auth@v4
with:
github-token: ${{ inputs.github-token }}
- name: Check for pre-commit-config.yaml
id: pre-commit-config
shell: bash
run: |
# Check for pre-commit-config.yaml
if [[ ! -f .pre-commit-config.yaml ]]; then
echo "::warning::No .pre-commit-config.yaml found, skipping pre-commit"
echo "exists=false" >> $GITHUB_OUTPUT
exit 0
fi
- name: Version files
shell: bash
run: |
# Finding pre-commit
# This will set an environment variable we can use to conditionally
# install pre-commit if needed and toggle how the action runs.
PRE_COMMIT_BIN=$(command -v pre-commit || true)
echo "PRE_COMMIT_BIN=$PRE_COMMIT_BIN" >> $GITHUB_ENV
# Finding python
# This will set an environment variable we can use to conditionally
# install python.
PYTHON_BIN=$(command -v python || true)
echo "PYTHON_BIN=$PYTHON_BIN" >> $GITHUB_ENV
- name: Setup python
# Only run this if we don't already have a pre-commit on the PATH
if: env.PYTHON_BIN == null && env.PRE_COMMIT_BIN == null && steps.pre-commit-config.outputs.exists != 'false'
uses: actions/setup-python@v7
- name: Determine node version
id: node-version
shell: bash
run: |
# Read version from .node-version or .nvmrc file if it exists, otherwise fall back to v24
if [[ -f .node-version ]]; then
NODE_VERSION=$(cat .node-version | tr -d '[:space:]')
echo "version=$NODE_VERSION" >> $GITHUB_OUTPUT
elif [[ -f .nvmrc ]]; then
NODE_VERSION=$(cat .nvmrc | tr -d '[:space:]')
echo "version=$NODE_VERSION" >> $GITHUB_OUTPUT
else
# Default Node version is v24
echo "version=24" >> $GITHUB_OUTPUT
fi
- name: Setup node
uses: actions/setup-node@v7
with:
node-version: ${{ steps.node-version.outputs.version }}
- name: Commitlint
# This version needs to be sync'd with the repo HEAD if a major version is cut
if: inputs.disable-commitlint != 'true'
uses: open-turo/action-pre-commit/commitlint@v3
with:
restore-config: "false"
config-file: ${{ inputs.config-file }}
turo-conventional-commit: ${{ inputs.turo-conventional-commit }}
- name: Get changed files
uses: dorny/paths-filter@v4
with:
list-files: shell
filters: |
changes:
- '**'
if: inputs.only-changed == 'true'
id: changed-files
- name: Select files to run pre-commit against
shell: bash
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.changes_files }}
ONLY_CHANGED: ${{ inputs.only-changed }}
CHANGED_FILES_COUNT: ${{ steps.changed-files.outputs.all_changed_files_count }}
STAGE: ${{ inputs.stage }}
run: |
PRE_COMMIT_ARGS="--all-files"
if [[ "$ONLY_CHANGED" == "true" ]]; then
[[ "$CHANGED_FILES_COUNT" != "0" ]] || exit 0
PRE_COMMIT_ARGS="--files $CHANGED_FILES"
fi
if [[ "$STAGE" != "" ]]; then
PRE_COMMIT_ARGS="$PRE_COMMIT_ARGS --hook-stage $STAGE"
fi
echo "PRE_COMMIT_ARGS=$PRE_COMMIT_ARGS" >> "$GITHUB_ENV"
- name: Compute pre-commit cache key
if: inputs.s3-bucket-name != ''
id: pre_commit_cache_key
env:
RUNNER_TEMP: ${{ runner.temp }}
REPO_NAME: ${{ github.event.repository.name }}
run: |
KEY="${{ hashFiles('**/.pre-commit-config.yaml') }}"
echo "key=$KEY" >> "$GITHUB_OUTPUT"
# Point pre-commit at the S3-backed cache dir. When this step is skipped
# (no S3 bucket), PRE_COMMIT_HOME stays unset and pre-commit uses its
# default (~/.cache/pre-commit), e.g. the runner image's pre-loaded cache.
echo "PRE_COMMIT_HOME=$RUNNER_TEMP/$REPO_NAME/cache-pre-commit-$KEY" >> "$GITHUB_ENV"
shell: bash
- name: Load cached pre-commit hooks if available
if: inputs.s3-bucket-name != ''
uses: everpcpc/actions-cache@v3
id: read_cache
with:
bucket: ${{ inputs.s3-bucket-name }}
use-fallback: false
path: ${{ runner.temp }}/${{ env.cache-name }}-${{ steps.pre_commit_cache_key.outputs.key }}
key: ${{ env.cache-name }}-${{ steps.pre_commit_cache_key.outputs.key }}
restore-keys: ${{ env.cache-name }}-
env:
AWS_REGION: ${{ inputs.s3-bucket-region }}
cache-name: ${{ github.event.repository.name }}/cache-pre-commit
- name: Install Pre-commit
if: env.PRE_COMMIT_BIN == null && steps.pre-commit-config.outputs.exists != 'false'
run: |
pip install pre-commit
pip freeze --local
shell: bash
- name: Pre-commit (from PATH)
shell: bash
# PRE_COMMIT_HOME (set only when an S3 cache is configured) and
# PRE_COMMIT_ARGS are inherited from $GITHUB_ENV set in earlier steps.
# PRE_COMMIT_ARGS is intentionally unquoted to preserve multi-argument behavior.
run: pre-commit run --show-diff-on-failure --color=always $PRE_COMMIT_ARGS
- name: Restore commitlint config
if: always() && inputs.disable-commitlint != 'true' && hashFiles(inputs.config-file) != '' && inputs.turo-conventional-commit == 'true'
shell: bash
env:
CONFIG_FILE: ${{ inputs.config-file }}
run: |
# Restore the commitlint config file if we wrote it
git checkout -- "$CONFIG_FILE" &>/dev/null || true