How to use custom Git Hooks

Important Notes

  • This post is based on Gogs

This feature currently is not working on Windows

Gogs provides custom Git hooks functionality to limited users. By default, only admin users are allowed to setting custom Git hooks. Regular users can grant permission by admins from Admin Panel - Users:

After that, users are able to see the Git hook management panel in repository settings:

Where green dot means you have customized corresponding type of Git hook, and grey dot represents currently not in use.

The hook content is exactly the same as native Git hooks:

For convenience, Gogs also provides Gogs-related values via environment variables:

|          name          |                     description                     |
| `GOGS_AUTH_USER_ID`    | User ID who made the push                           |
| `GOGS_AUTH_USER_NAME`  | User name who made the push (can have mixed cases)  |
| `GOGS_AUTH_USER_EMAIL` | User email who made the push (can have mixed cases) |
| `GOGS_REPO_OWNER_NAME` | Owner name of the repository                        |
| `GOGS_REPO_ID`         | Repository ID                                       |
| `GOGS_REPO_NAME`       | Repository name                                     |

:speech_balloon: Please leave comments if you have any questions or concerns about this post.


Is there a convenient way to add those Git Hooks via /admin API? Else, what’s the suggested path (internal documentation) to add such endpoint (maybe via an Issue/PR)?

If you have access to the server, you can simply write scripts under each repo’s hooks/custom directory, those files inside this path will be called at its time.