debian-mirror-gitlab/doc/administration/server_hooks.md

153 lines
9.8 KiB
Markdown
Raw Normal View History

2020-03-13 15:44:24 +05:30
---
2022-08-13 15:12:31 +05:30
stage: Systems
2020-06-23 00:09:42 +05:30
group: Gitaly
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2020-03-13 15:44:24 +05:30
disqus_identifier: 'https://docs.gitlab.com/ee/administration/custom_hooks.html'
---
2023-01-13 00:05:48 +05:30
# Git server hooks **(FREE SELF)**
2020-03-13 15:44:24 +05:30
2023-01-13 00:05:48 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/196051) in GitLab 12.8 replacing Custom Hooks.
> - [Renamed](https://gitlab.com/gitlab-org/gitlab/-/issues/372991) from server hooks to Git server hooks in GitLab 15.6.
2020-07-28 23:09:34 +05:30
2023-01-13 00:05:48 +05:30
Git server hooks (not to be confused with [system hooks](system_hooks.md) or [file hooks](file_hooks.md)) run custom logic
on the GitLab server. You can use them to run Git-related tasks such as:
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
- Enforcing specific commit policies.
- Performing tasks based on the state of the repository.
2020-07-28 23:09:34 +05:30
2023-01-13 00:05:48 +05:30
Git server hooks use `pre-receive`, `post-receive`, and `update`
2022-04-04 11:22:00 +05:30
[Git server-side hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#_server_side_hooks).
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
GitLab administrators configure server hooks on the file system of the GitLab server. If you don't have file system access,
alternatives to server hooks include:
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
- [Webhooks](../user/project/integrations/webhooks.md).
- [GitLab CI/CD](../ci/index.md).
2022-07-23 23:45:48 +05:30
- [Push rules](../user/project/repository/push_rules.md), for a user-configurable Git hook interface.
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
[Geo](geo/index.md) doesn't replicate server hooks to secondary nodes.
2020-07-28 23:09:34 +05:30
2022-08-13 15:12:31 +05:30
## Create server hooks for a repository
2020-07-28 23:09:34 +05:30
2022-08-13 15:12:31 +05:30
To create server hooks for a repository:
2020-07-28 23:09:34 +05:30
2022-10-11 01:57:18 +05:30
1. On the top bar, select **Main menu > Admin**.
2022-04-04 11:22:00 +05:30
1. Go to **Overview > Projects** and select the project you want to add a server hook to.
1. On the page that appears, locate the value of **Gitaly relative path**. This path is where server hooks must be located.
- If you are using [hashed storage](repository_storage_types.md#hashed-storage), see
[Translate hashed storage paths](repository_storage_types.md#translate-hashed-storage-paths) for information on
interpreting the relative path.
- If you are not using [hashed storage](repository_storage_types.md#hashed-storage):
- For Omnibus GitLab installations, the path is usually `/var/opt/gitlab/git-data/repositories/<group>/<project>.git`.
- For an installation from source, the path is usually `/home/git/repositories/<group>/<project>.git`.
1. On the file system, create a new directory in the correct location called `custom_hooks`.
2022-08-13 15:12:31 +05:30
1. In the new `custom_hooks` directory:
- To create a single server hook, create a file with a name that matches the hook type. For example, for a
`pre-receive` server hook, the filename should be `pre-receive` with no extension.
- To create many server hooks, create a directory for the hooks that matches the hook type. For example, for a
`pre-receive` server hook, the directory name should be `pre-receive.d`. Put the files for the hook in that directory.
1. Make the server hook files executable and ensure that they are owned by the Git user.
2023-01-13 00:05:48 +05:30
1. Write the code to make the server hook function as expected. Git server hooks can be in any programming language. Ensure
2022-04-04 11:22:00 +05:30
the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) at the top reflects the language type. For
example, if the script is in Ruby the shebang is probably `#!/usr/bin/env ruby`.
2022-08-13 15:12:31 +05:30
1. Make the hook file executable, ensure that it's owned by the Git user, and ensure it does not match the backup file
pattern (`*~`).
2020-05-24 23:13:21 +05:30
2022-04-04 11:22:00 +05:30
If the server hook code is properly implemented, it should execute when the Git hook is next triggered.
2020-07-28 23:09:34 +05:30
2022-08-13 15:12:31 +05:30
## Create global server hooks for all repositories
2020-07-28 23:09:34 +05:30
2022-10-11 01:57:18 +05:30
To create a Git hook that applies to all repositories, set a global server hook. Global server hooks also apply to:
2022-04-04 11:22:00 +05:30
- [Project and group wiki](../user/project/wiki/index.md) repositories. Their storage directory names are in the format
`<id>.wiki.git`.
- [Design management](../user/project/issues/design_management.md) repositories under a project. Their storage directory
names are in the format `<id>.design.git`.
2021-06-08 01:23:25 +05:30
2022-04-04 11:22:00 +05:30
### Choose a server hook directory
2020-07-28 23:09:34 +05:30
2022-10-11 01:57:18 +05:30
Before creating a global server hook, you must choose a directory for it.
2020-07-28 23:09:34 +05:30
2022-10-11 01:57:18 +05:30
For Omnibus GitLab installations, the directory is set in `gitlab.rb` under `gitaly['custom_hooks_dir']`. You can either:
2020-07-28 23:09:34 +05:30
2022-10-11 01:57:18 +05:30
- Use the default suggestion of the `/var/opt/gitlab/gitaly/custom_hooks` directory by uncommenting it.
- Add your own setting.
2020-07-28 23:09:34 +05:30
2022-10-11 01:57:18 +05:30
For installations from source:
- The directory is set in a configuration file. The location of the configuration file depends on the GitLab version:
- For GitLab 13.1 and later, the directory is set in `gitaly/config.toml` under the `[hooks]` section. However,
GitLab honors the `custom_hooks_dir` value in `gitlab-shell/config.yml` if the value in `gitaly/config.toml` is blank
or non-existent.
- For GitLab 13.0 and earlier, the directory set in `gitlab-shell/config.yml`.
- The default directory is `/home/git/gitlab-shell/hooks`.
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
### Create the global server hook
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
To create a global server hook for all repositories:
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
1. On the GitLab server, go to the configured global server hook directory.
2022-10-11 01:57:18 +05:30
1. In the configured global server hook directory, create a directory for the hooks that matches the hook type. For example, for a `pre-receive` server hook, the directory name should be `pre-receive.d`.
2023-01-13 00:05:48 +05:30
1. Inside this new directory, add your server hooks. Git server hooks can be in any programming language. Ensure the
2022-04-04 11:22:00 +05:30
[shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) at the top reflects the language type. For example, if the
script is in Ruby the shebang is probably `#!/usr/bin/env ruby`.
1. Make the hook file executable, ensure that it's owned by the Git user, and ensure it does not match the backup file
pattern (`*~`).
2020-03-13 15:44:24 +05:30
2022-10-11 01:57:18 +05:30
If the server hook code is properly implemented, it should execute when the Git hook is next triggered. Hooks are executed in alphabetical order by filename in the hook type
subdirectories.
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
## Chained server hooks
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
GitLab can execute server hooks in a chain. GitLab searches for and executes server hooks in the following order:
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
- Built-in GitLab server hooks. These server hooks are not customizable by users.
- `<project>.git/custom_hooks/<hook_name>`: Per-project hooks. This location is kept for backwards compatibility.
2020-07-28 23:09:34 +05:30
- `<project>.git/custom_hooks/<hook_name>.d/*`: Location for per-project hooks.
2022-04-04 11:22:00 +05:30
- `<custom_hooks_dir>/<hook_name>.d/*`: Location for all executable global hook files except editor backup files.
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
Within a server hooks directory, hooks:
2020-07-28 23:09:34 +05:30
- Are executed in alphabetical order.
- Stop executing when a hook exits with a non-zero value.
2022-04-04 11:22:00 +05:30
## Environment variables available to server hooks
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
You can pass any environment variable to server hooks, but you should only rely on supported environment variables.
2021-02-22 17:27:13 +05:30
2022-04-04 11:22:00 +05:30
The following GitLab environment variables are supported for all server hooks:
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
| Environment variable | Description |
|:---------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| `GL_ID` | GitLab identifier of user that initiated the push. For example, `user-2234`. |
| `GL_PROJECT_PATH` | (GitLab 13.2 and later) GitLab project path. |
| `GL_PROTOCOL` | (GitLab 13.2 and later) Protocol used for this change. One of: `http` (Git `push` using HTTP), `ssh` (Git `push` using SSH), or `web` (all other actions). |
| `GL_REPOSITORY` | `project-<id>` where `id` is the ID of the project. |
| `GL_USERNAME` | GitLab username of the user that initiated the push. |
2020-07-28 23:09:34 +05:30
2022-04-04 11:22:00 +05:30
The following Git environment variables are supported for `pre-receive` and `post-receive` server hooks:
2020-07-28 23:09:34 +05:30
| Environment variable | Description |
|:-----------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `GIT_ALTERNATE_OBJECT_DIRECTORIES` | Alternate object directories in the quarantine environment. See [Git `receive-pack` documentation](https://git-scm.com/docs/git-receive-pack#_quarantine_environment). |
| `GIT_OBJECT_DIRECTORY` | GitLab project path in the quarantine environment. See [Git `receive-pack` documentation](https://git-scm.com/docs/git-receive-pack#_quarantine_environment). |
2022-04-04 11:22:00 +05:30
| `GIT_PUSH_OPTION_COUNT` | Number of [push options](../user/project/push_options.md). See [Git `pre-receive` documentation](https://git-scm.com/docs/githooks#pre-receive). |
| `GIT_PUSH_OPTION_<i>` | Value of [push options](../user/project/push_options.md) where `i` is from `0` to `GIT_PUSH_OPTION_COUNT - 1`. See [Git `pre-receive` documentation](https://git-scm.com/docs/githooks#pre-receive). |
2020-03-13 15:44:24 +05:30
## Custom error messages
2022-04-04 11:22:00 +05:30
You can have custom error messages appear in the GitLab UI when a commit is declined or an error occurs during the Git
hook. To display a custom error message, your script must:
2020-03-13 15:44:24 +05:30
- Send the custom error messages to either the script's `stdout` or `stderr`.
- Prefix each message with `GL-HOOK-ERR:` with no characters appearing before the prefix.
2022-04-04 11:22:00 +05:30
For example:
2020-03-13 15:44:24 +05:30
```shell
#!/bin/sh
echo "GL-HOOK-ERR: My custom error message.";
exit 1
```