A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.gitlab.com/administration/server_hooks/ below:

Git server hooks | GitLab Docs

History

Git server hooks (not to be confused with system hooks or file hooks) run custom logic on the GitLab server. You can use them to run Git-related tasks such as:

Git server hooks use pre-receive, post-receive, and update Git server-side hooks.

GitLab administrators configure server hooks using the gitaly command, which also:

If you don’t have access to the gitaly command, alternatives to server hooks include:

Geo doesn’t replicate server hooks to secondary nodes.

Set server hooks for a repository

History

Prerequisites:

To set server hooks for a repository:

  1. Create tarball containing custom hooks:

    1. Write the code to make the server hook function as expected. Git server hooks can be in any programming language. Ensure the shebang at the top reflects the language type. For example, if the script is in Ruby the shebang is probably #!/usr/bin/env ruby.

      • 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.
    2. Ensure the server hook files are executable and do not match the backup file pattern (*~). The server hooks be in a custom_hooks directory that is at the root of the tarball.

    3. Create the custom hooks archive with the tar command. For example, tar -cf custom_hooks.tar custom_hooks.

  2. Run the hooks set subcommand with required options to set the Git hooks for the repository. For example, cat custom_hooks.tar | sudo -u git -- /opt/gitlab/embedded/bin/gitaly hooks set --storage <storage> --repository <relative path> --config <config path>.

  3. If you are using Gitaly Cluster (Praefect), you must run hooks set subcommand on all Gitaly nodes. For more information, see Server hooks on a Gitaly Cluster (Praefect).

If you implemented the server hook code correctly, it should execute when the Git hook is next triggered.

To create server hooks for a repository:

  1. On the left sidebar, at the bottom, select Admin.
  2. Go to Overview > Projects and select the project you want to add a server hook to.
  3. On the page that appears, locate the value of Relative path. This path is where server hooks must be located.
  4. On the file system, create a new directory in the correct location called custom_hooks.
  5. In the new custom_hooks directory:
  6. Make the server hook files executable and ensure that they are owned by the Git user.
  7. Write the code to make the server hook function as expected. Git server hooks can be in any programming language. Ensure the shebang at the top reflects the language type. For example, if the script is in Ruby the shebang is probably #!/usr/bin/env ruby.
  8. Ensure the hook file does not match the backup file pattern (*~).
  9. If you are using Gitaly Cluster (Praefect), you must repeat this process on all Gitaly nodes. For more information, see Server hooks on a Gitaly Cluster (Praefect).

If the server hook code is properly implemented, it should execute when the Git hook is next triggered.

Server hooks on a Gitaly Cluster (Praefect)

If you use Gitaly Cluster (Praefect), an individual repository may be replicated to multiple Gitaly storages in Praefect. Consequentially, the hook scripts must be copied to every Gitaly node that has a replica of the repository. To accomplish this, follow the same steps for setting custom repository hooks for the applicable version and repeat for each storage.

The location to copy the scripts to depends on where repositories are stored. New repositories are created using Praefect-generated replica paths, which are not the hashed storage path. The replica path can be identified by querying the Praefect repository metadata using -relative-path to specify the expected GitLab hashed storage path.

Create global server hooks for all repositories

To create a Git hook that applies to all repositories, set a global server hook. Global server hooks also apply to:

Choose a server hook directory

Before creating a global server hook, you must choose a directory for it.

For Linux package installations, the directory is set in gitlab.rb under gitaly['configuration'][:hooks][:custom_hooks_dir]. You can either:

For self-compiled installations:

Create the global server hook

To create a global server hook for all repositories:

  1. On the GitLab server, go to the configured global server hook directory.
  2. 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.
  3. Inside this new directory, add your server hooks. Git server hooks can be in any programming language. Ensure the shebang at the top reflects the language type. For example, if the script is in Ruby the shebang is probably #!/usr/bin/env ruby.
  4. Make the hook file executable, ensure that it’s owned by the Git user, and ensure it does not match the backup file pattern (*~).

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.

Remove server hooks for a repository

History

Prerequisites:

To remove server hooks, pass an empty tarball to hook set to indicate that the repository should contain no hooks. For example:

cat empty_hooks.tar | sudo -u git -- /opt/gitlab/embedded/bin/gitaly hooks set --storage <storage> --repository <relative path> --config <config path>

To remove server hooks:

  1. Go to the location of the repository on disk.
  2. Delete the server hooks in the custom_hooks directory.
Chained server hooks

GitLab can execute server hooks in a chain. GitLab searches for and executes server hooks in the following order:

Within a server hooks directory, hooks:

Environment variables available to server hooks

You can pass any environment variable to server hooks, but you should only rely on supported environment variables.

The following GitLab environment variables are supported for all server hooks:

Environment variable Description GL_ID GitLab identifier of user or SSH key that initiated the push. For example, user-2234 or key-4. GL_PROJECT_PATH GitLab project path. GL_PROTOCOL 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.

The following Git environment variables are supported for pre-receive and post-receive server hooks:

Custom error messages

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:

For example:

#!/bin/sh
echo "GL-HOOK-ERR: My custom error message.";
exit 1

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4