oneturkmen
oneturkmen

Reputation: 1330

Run gitlab-ci.yml only when merge request to master made

I am currently having my project in GitLab and Heroku. What I wanna do is as soon as I ask for merge request with my feature branch (let's call it crud-on-spaghetti), I want to automatically run the tests on this branch (npm test basically, using Mocha/Chai), and after they succeed, merge this crud-on-spaghetti with master, commit it and push it to origin/master (which is remote on GitLab) and after git push heroku master (basically, push it to the master branch in Heroku, where my app is stored). I have read several articles on GitLab CI and I think this is more suitable for me (rather than Heroku CI, because I do not have DEV and PROD instances).

So, as of now, I do this manually. And this is my .gitlab-ci.yml file now (which is not committed/pushed yet):

stages:
  - test
  - deploy

test_for_illegal_bugs:
  stage: test
  script:
    - npm test

deploy_to_dev:
  stage: deploy
  only:
    - origin master
  script:
    - git commit
    - git push origin master
    - git pull heroku master --rebase
    - git push heroku master

Hence, my questions is: What do I exactly need to write in .gitlab-ci.yml in order to automate all these "manipulations" (above)?

PS. And another (theoretical) follow-up question: how is GitLab-CI Runner triggered? For instance, if I want it to trigger upon merge request with master, do I do that using only: ... in .gitlab-ci.yml?

Upvotes: 21

Views: 29388

Answers (3)

Wolfson
Wolfson

Reputation: 1407

As of 2023 the keywords only and except have been deprecated in favor of rules (see docs).

In newer versions

Using rules: to restrict the execution of a job to a merge request to the target branch:

  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'

For older versions

Restrict stages to Merge Requests:

To have your test stage only being executed when a Merge Request (MR) is opened, use:

   only:
     - merge_requests

According to the Gitlab docs, you can further restrict this to only being executed for MRs with a certain target branch, e.g. only MRs for master

   only:
     - merge_requests
   except:
     variables:
       - $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "master"

This adds an exception for all target branches that are not master.

Restrict stages to Branches:

As already mentioned by @marcolz, this is achieved by

   only:
     - master

to only execute the stage for pushes to the master branch.

Upvotes: 29

Hanan Shteingart
Hanan Shteingart

Reputation: 9078

I am not sure, but I think that the rule one should use is to detect a "result merge event":

($CI_MERGE_REQUEST_EVENT_TYPE   == "merged_result") &&
 ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)

see https://docs.gitlab.com/ee/ci/pipelines/merged_results_pipelines.html

otherwise, the stage might run regardless if merge request was approved or not and finished successfully.

Upvotes: 1

marcolz
marcolz

Reputation: 2970

Try

only:
  - master

origin is just a name for a remote. master is the name of the branch.

The runner is triggered by GitLab-CI the moment that a commit is pushed to the repository, so alas not upon merge request.

You can use a trigger to trigger a pipeline and then call that trigger from a merge request event in integrations.

Upvotes: 14

Related Questions