Use Git Hooks to run custom shell snippets
masterThe action allows you to run custom shell snippets around specific git operations. These hooks run in the same bash process as the action, meaning they have access to the repository working directory and all INPUT_* environment variables.
Important Details:
- Hooks run under
set -eu. An unset variable will abort the action. Use${VAR:-}to provide defaults. - A hook only runs if its corresponding git operation is actually performed (e.g.,
before_add_hookwon't run if the working tree is clean). - If a hook exits with a non-zero status, the action fails. Use
|| trueto ignore failures. - Security: When using
pull_request_target, do not interpolate GitHub context (like${{ github.event.pull_request.title }}) directly into a hook, as this allows for shell injection. Instead, pass the value via anenvvariable.
Available Hooks
| Hook | Runs around |
|---|---|
before_add_hook / after_add_hook | git add |
before_commit_hook / after_commit_hook | git commit |
before_tag_hook / after_tag_hook | git tag |
before_push_hook / after_push_hook | git push (skipped if skip_push: true) |
- uses: stefanzweifel/git-auto-commit-action@v7
env:
PR_TITLE: ${{ github.event.pull_request.title }}
with:
before_commit_hook: |
# $PR_TITLE is read as data, not evaluated as code
echo "PR: $PR_TITLE"
./scripts/prepare-commit.sh