Skip to content

Commit e0fdf17

Browse files
committed
feat: add support to cache pre-commit in s3
1 parent 4b9d5c6 commit e0fdf17

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ jobs:
5959
| config-file | The config file to present to commitlint-github-action | `true` | .commitlintrc.yaml |
6060
| turo-conventional-commit | Set this to "false" to customize conventional commit configuration | `true` | true |
6161
| only-changed | Set this to "true" to only run pre-commit against changed files, and not the entire repository | `false` | |
62+
| s3-bucket-name | S3 bucket name to cache node_modules to speed up dependency installation. | `false` | |
63+
| s3-bucket-region | S3 bucket region to cache node_modules to speed up dependency installation. | `false` | |
6264
<!-- action-docs-inputs -->
6365

6466
<!-- action-docs-outputs -->
67+
## Outputs
6568

69+
| parameter | description |
70+
| --- | --- |
71+
| cache-hit | Whether the cache was hit when installing dependencies |
6672
<!-- action-docs-outputs -->
6773

6874
<!-- action-docs-runs -->

action.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ inputs:
1414
description: >-
1515
Set this to "true" to only run pre-commit against changed files, and not
1616
the entire repository
17+
s3-bucket-name:
18+
required: false
19+
description: S3 bucket name to cache node_modules to speed up dependency installation.
20+
s3-bucket-region:
21+
required: false
22+
description: S3 bucket region to cache node_modules to speed up dependency installation.
23+
outputs:
24+
cache-hit:
25+
description: Whether the cache was hit when installing dependencies
26+
value: ${{ steps.read_cache.outputs.cache-hit }}
1727

1828
runs:
1929
using: composite
@@ -72,17 +82,43 @@ runs:
7282
PRE_COMMIT_ARGS="--files ${{ steps.changed-files.outputs.all_changed_files }}"
7383
echo "PRE_COMMIT_ARGS=$PRE_COMMIT_ARGS" >> "$GITHUB_ENV"
7484
fi
85+
86+
- name: Compute pre-commit cache key
87+
if: inputs.s3-bucket-name != ''
88+
id: pre_commit_cache_key
89+
run: |
90+
echo "key=${{ hashFiles('**/.pre-commit-config.yaml') }}" >> $GITHUB_OUTPUT
91+
shell: bash
92+
- name: Load cached pre-commit hooks if available
93+
if: inputs.s3-bucket-name != ''
94+
uses: everpcpc/actions-cache@v2
95+
id: read_cache
96+
with:
97+
bucket: ${{ inputs.s3-bucket-name }}
98+
use-fallback: false
99+
path: ${{ runner.temp }}/${{ env.cache-name }}-${{ steps.pre_commit_cache_key.outputs.key }}
100+
key: ${{ env.cache-name }}-${{ steps.pre_commit_cache_key.outputs.key }}
101+
restore-keys: ${{ env.cache-name }}-
102+
env:
103+
AWS_REGION: ${{ inputs.s3-bucket-region }}
104+
cache-name: ${{ github.event.repository.name }}/cache-pre-commit
105+
75106
- name: Pre-commit (action)
76107
# Same as above, this will install and run pre-commit for us
77108
if: env.PRE_COMMIT_BIN == null && steps.pre-commit-config.outputs.exists != 'false'
78109
uses: pre-commit/action@v3.0.1
79110
with:
80111
extra_args: ${{ env.PRE_COMMIT_ARGS }}
112+
env:
113+
PRE_COMMIT_HOME: ${{ runner.temp }}/${{ github.event.repository.name }}/cache-pre-commit-${{ steps.pre_commit_cache_key.outputs.key }}
81114
- name: Pre-commit (from PATH)
82115
# Run pre-commit directly if we found it on the PATH
83116
if: env.PRE_COMMIT_BIN != null && steps.pre-commit-config.outputs.exists != 'false'
84117
shell: bash
85118
run: pre-commit run --show-diff-on-failure --color=always ${{ env.PRE_COMMIT_ARGS }}
119+
env:
120+
PRE_COMMIT_HOME: ${{ runner.temp }}/${{ github.event.repository.name }}/cache-pre-commit-${{ steps.pre_commit_cache_key.outputs.key }}
121+
86122
- name: Restore commitlint config
87123
if: always() && hashFiles(inputs.config-file) != '' && inputs.turo-conventional-commit == 'true'
88124
shell: bash

0 commit comments

Comments
 (0)