2018-04-06 08:20:38 -04:00
|
|
|
# GitLab Plugin system
|
2018-02-27 09:01:17 -05:00
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
> Introduced in GitLab 10.6.
|
2018-04-06 05:17:35 -04:00
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
With custom plugins, GitLab administrators can introduce custom integrations
|
|
|
|
without modifying GitLab's source code.
|
2018-02-27 09:01:17 -05:00
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
NOTE: **Note:**
|
|
|
|
Instead of writing and supporting your own plugin you can make changes
|
|
|
|
directly to the GitLab source code and contribute back upstream. This way we can
|
|
|
|
ensure functionality is preserved across versions and covered by tests.
|
2018-02-27 09:01:17 -05:00
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
NOTE: **Note:**
|
|
|
|
Plugins must be configured on the filesystem of the GitLab server. Only GitLab
|
|
|
|
server administrators will be able to complete these tasks. Explore
|
|
|
|
[system hooks] or [webhooks] as an option if you do not have filesystem access.
|
|
|
|
|
|
|
|
A plugin will run on each event so it's up to you to filter events or projects
|
|
|
|
within a plugin code. You can have as many plugins as you want. Each plugin will
|
|
|
|
be triggered by GitLab asynchronously in case of an event. For a list of events
|
|
|
|
see the [system hooks] documentation.
|
2018-02-27 09:01:17 -05:00
|
|
|
|
|
|
|
## Setup
|
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
The plugins must be placed directly into the `plugins` directory, subdirectories
|
|
|
|
will be ignored. There is an
|
2020-01-03 19:07:49 -05:00
|
|
|
[`example` directory inside `plugins`](https://gitlab.com/gitlab-org/gitlab/tree/master/plugins/examples)
|
2018-04-06 08:20:38 -04:00
|
|
|
where you can find some basic examples.
|
2018-02-27 09:01:17 -05:00
|
|
|
|
|
|
|
Follow the steps below to set up a custom hook:
|
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
1. On the GitLab server, navigate to the plugin directory.
|
2018-02-27 09:01:17 -05:00
|
|
|
For an installation from source the path is usually
|
|
|
|
`/home/git/gitlab/plugins/`. For Omnibus installs the path is
|
|
|
|
usually `/opt/gitlab/embedded/service/gitlab-rails/plugins`.
|
2018-04-12 17:10:07 -04:00
|
|
|
|
2018-04-13 13:41:02 -04:00
|
|
|
For [highly available] configurations, your hook file should exist on each
|
|
|
|
application server.
|
2018-04-12 17:10:07 -04:00
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
1. Inside the `plugins` directory, create a file with a name of your choice,
|
|
|
|
without spaces or special characters.
|
2019-08-23 04:50:24 -04:00
|
|
|
1. Make the hook file executable and make sure it's owned by the Git user.
|
2018-04-06 08:20:38 -04:00
|
|
|
1. Write the code to make the plugin function as expected. That can be
|
|
|
|
in any language, and ensure the 'shebang' at the top properly reflects the
|
|
|
|
language type. For example, if the script is in Ruby the shebang will
|
|
|
|
probably be `#!/usr/bin/env ruby`.
|
|
|
|
1. The data to the plugin will be provided as JSON on STDIN. It will be exactly
|
|
|
|
same as for [system hooks]
|
2018-02-27 09:01:17 -05:00
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
That's it! Assuming the plugin code is properly implemented, the hook will fire
|
|
|
|
as appropriate. The plugins file list is updated for each event, there is no
|
|
|
|
need to restart GitLab to apply a new plugin.
|
2018-02-27 09:01:17 -05:00
|
|
|
|
2018-02-28 05:31:19 -05:00
|
|
|
If a plugin executes with non-zero exit code or GitLab fails to execute it, a
|
2019-08-05 17:24:32 -04:00
|
|
|
message will be logged to:
|
|
|
|
|
|
|
|
- `gitlab-rails/plugin.log` in an Omnibus installation.
|
|
|
|
- `log/plugin.log` in a source installation.
|
|
|
|
|
|
|
|
## Creating plugins
|
|
|
|
|
|
|
|
Below is an example that will only response on the event `project_create` and
|
|
|
|
will inform the admins from the GitLab instance that a new project has been created.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# By using the embedded ruby version we eliminate the possibility that our chosen language
|
|
|
|
# would be unavailable from
|
|
|
|
#!/opt/gitlab/embedded/bin/ruby
|
|
|
|
require 'json'
|
|
|
|
require 'mail'
|
|
|
|
|
|
|
|
# The incoming variables are in JSON format so we need to parse it first.
|
|
|
|
ARGS = JSON.parse(STDIN.read)
|
|
|
|
|
|
|
|
# We only want to trigger this plugin on the event project_create
|
|
|
|
return unless ARGS['event_name'] == 'project_create'
|
|
|
|
|
|
|
|
# We will inform our admins of our gitlab instance that a new project is created
|
|
|
|
Mail.deliver do
|
|
|
|
from 'info@gitlab_instance.com'
|
|
|
|
to 'admin@gitlab_instance.com'
|
|
|
|
subject "new project " + ARGS['name']
|
|
|
|
body ARGS['owner_name'] + 'created project ' + ARGS['name']
|
|
|
|
end
|
|
|
|
```
|
2018-02-28 05:31:19 -05:00
|
|
|
|
2018-02-27 09:01:17 -05:00
|
|
|
## Validation
|
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
Writing your own plugin can be tricky and it's easier if you can check it
|
|
|
|
without altering the system. A rake task is provided so that you can use it
|
|
|
|
in a staging environment to test your plugin before using it in production.
|
|
|
|
The rake task will use a sample data and execute each of plugin. The output
|
|
|
|
should be enough to determine if the system sees your plugin and if it was
|
|
|
|
executed without errors.
|
2018-02-27 09:01:17 -05:00
|
|
|
|
|
|
|
```bash
|
|
|
|
# Omnibus installations
|
|
|
|
sudo gitlab-rake plugins:validate
|
|
|
|
|
|
|
|
# Installations from source
|
2018-04-06 08:20:38 -04:00
|
|
|
cd /home/git/gitlab
|
2018-02-27 09:01:17 -05:00
|
|
|
bundle exec rake plugins:validate RAILS_ENV=production
|
|
|
|
```
|
|
|
|
|
2018-04-06 08:20:38 -04:00
|
|
|
Example of output:
|
2018-02-27 09:02:42 -05:00
|
|
|
|
|
|
|
```
|
|
|
|
Validating plugins from /plugins directory
|
|
|
|
* /home/git/gitlab/plugins/save_to_file.clj succeed (zero exit code)
|
|
|
|
* /home/git/gitlab/plugins/save_to_file.rb failure (non-zero exit code)
|
|
|
|
```
|
2018-02-27 09:01:17 -05:00
|
|
|
|
|
|
|
[system hooks]: ../system_hooks/system_hooks.md
|
|
|
|
[webhooks]: ../user/project/integrations/webhooks.md
|
2019-08-22 04:50:31 -04:00
|
|
|
[highly available]: ./high_availability/README.md
|