'How to detect in GitLab CI that a pipeline was triggered when a Merge Request was created

I'm doing a script that sends a message in Ryver based on certain events for GitLabs Merge Request. The supported scenarios:

  1. When a Merge Request is created
  2. When comments are made (Code Review)
  3. when new commits make the pipeline fail

The following allows to limit the pipeline to merge requests only:

  only:
  - merge_requests
  script:
  - ./ryver.sh #This does the logic of sending the message based on the events

I tried using export to print all the variables in the pipeline but couldn't find a way to explicitly detect the event that triggered this job (Code Review, Creation, etc).

I tried:

  • Merge Request status
  • Comparing commits
  • Using times (not very reliable way)

I wonder:

  1. Can we detect what event triggered the pipeline within the Merge Request scope? Or
  2. Do I need to use a WebHook to access this information? Or
  3. There is another way to do what my team is trying to do?

I'm open to suggestions or other ways to do it that are not related to the gitlab-ci.yml, as long as it is free



Solution 1:[1]

You can use logic like this to detect that the commit came from a Merge Request:

$CI_PIPELINE_SOURCE == 'merge_request_event'

See here for more details on how to control what kicks off a pipeline.

You can find out what triggered a pipeline by looking at this variable:

CI_PIPELINE_SOURCE

From docs:

enter image description here

See here for more details about pre-defined variables.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 frakman1