From bf3a56736cd9371148971fbe9d1f2a57532c8647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o?= Date: Tue, 30 Nov 2021 11:18:32 +0800 Subject: [PATCH] feat: add generic gh action --- README.md | 24 +++++++++++++++++++++++- action.yml | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 action.yml diff --git a/README.md b/README.md index a93773c..96357ef 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ pre-commit-hooks ================ -Some out-of-the-box hooks for [pre-commit](https://github.com/pre-commit/pre-commit). +Some out-of-the-box hooks for [pre-commit](https://github.com/pre-commit/pre-commit), and +a github action to easily run all kind of hooks from github CI. ### Using pre-commit-hooks with pre-commit @@ -39,6 +40,27 @@ Time is fleeting, we change services. Consequently to keep the code futureproof we don't want links to ephemeral thrid party stuff (slack, clubhouse, atlassian) +### Github actions + +Run pre-commit hooks of type: commit, push and commit-msg + +Example: +```yaml +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: Kpler/kp-pre-commit-hooks@version + with: + skipped-hooks: 'no-commit-to-branch' + main-branch: 'master' +``` + +#### Alternatives: + - [The official pre-commit action](https://github.com/pre-commit/action): deprecated and hard to use with several type of hooks + - [The pre-commit CI](https://pre-commit.ci/): yet another CI and expensive (pricing per user) + ### Contributing #### Debugging / testing diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..faf533d --- /dev/null +++ b/action.yml @@ -0,0 +1,44 @@ +name: 'Pre-commit' +description: 'Run pre-commit hooks' +inputs: + skipped-hooks: + description: 'Skip some hooks, eg "no-commit-to-branch,foo"' + required: true + default: '' + main-branch: + description: 'master or main' + required: true + default: 'main' +runs: + using: "composite" + steps: + - uses: actions/setup-python@v2 + - name: Skip some hooks + run: echo "SKIP=${{ inputs.skipped-hooks }}" >> $GITHUB_ENV + if: ${{ inputs.skipped-hooks }} != '' + shell: bash + - name: Install pre-commit + run: pip install pre-commit + shell: bash + - name: Cache pre-commit hooks + uses: actions/cache@v2 + with: + path: '~/.cache/pre-commit' + key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }} + - name: Run pre-commit + run: | + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage commit + pre-commit run --show-diff-on-failure --color=always --all-files --hook-stage push + + # https://github.com/jorisroovers/gitlint/issues/148 + git config --add user.email '' + git config --add user.name '' + + git fetch origin ${{ inputs.main-branch }} 2> /dev/null + for commit in $(git rev-list origin/${{ inputs.main-branch }}..HEAD) + do + git show -s --pretty=format:%B $commit > tmp + pre-commit run --color=always --hook-stage commit-msg --commit-msg-filename tmp + done + shell: bash +