-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (83 loc) · 2.86 KB
/
Copy pathpre-commit.yml
File metadata and controls
91 lines (83 loc) · 2.86 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
name: pre-commit
on:
workflow_call:
inputs:
image_version:
description: "pre-commit docker image version"
required: false
default: "latest"
type: string
history_depth:
description: "0 means fetch the full history"
required: false
default: 0
type: number
run_all_files:
description: "run all files or specific files"
required: false
default: "false"
type: string
check_pr_title:
description: "check pr title"
required: false
default: true
type: boolean
run_on_merge:
description: "run on merge"
required: false
default: true
type: boolean
jobs:
pre-commit:
name: pre-commit
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
container:
image: ghcr.io/bitbrew-dev/reusable-workflows/pre-commit:${{ inputs.image_version }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUN_ALL_FILES: ${{ inputs.run_all_files }}
CHECK_PR_TITLE: ${{ inputs.check_pr_title }}
RUN_ON_MERGE: ${{ inputs.run_on_merge }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: ${{ inputs.history_depth }}
- name: prepare test environment
run: |
git config --global --add safe.directory $GITHUB_WORKSPACE
pre-commit install && pre-commit install --hook commit-msg
git config --global user.email "${{ github.actor }}@bitbrew.dev"
git config --global user.name "${{ github.actor }}"
- name: cache pre-commit hooks
uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: ${{ runner.os }}-precommit-${{ hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
${{ runner.os }}-precommit-
- name: run pre-commit on pull request (changed files)
if: ${{ github.ref != 'refs/heads/main' }}
shell: bash
run: |
# run pre-commit on all files or specific files
if [[ "${RUN_ALL_FILES}" == "true" ]]; then
pre-commit run --all-files
else
pre-commit run --files $(git diff --name-only origin/${{ github.event.repository.default_branch }}..HEAD)
fi
env:
RUN_ALL_FILES: ${{ inputs.run_all_files }}
- name: run pre-commit on main branch (all files)
if: github.event_name == 'push' && github.ref == 'refs/heads/main' && inputs.run_on_merge
run: |
pre-commit run --all-files
- name: check pr title
if: ${{ github.ref != 'refs/heads/main' && inputs.check_pr_title }}
env:
pull_request_title: ${{ github.event.pull_request.title }}
run: |
echo "checking PR title: '${pull_request_title}'"
git commit --allow-empty -m "${pull_request_title}"