21

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?

2
  • 2
    Remove origin from the only: part.
    – marcolz
    Commented May 24, 2017 at 15:20
  • 1
    The test stage is triggered by every commit on every branch. To restrict it to merge requests, add only: merge_requests to the stage.
    – Wolfson
    Commented Sep 1, 2020 at 9:37

3 Answers 3

29

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.

14

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.

4
  • Here what I receive after pushing the changes prntscr.com/fblm8h
    – oneturkmen
    Commented May 24, 2017 at 15:24
  • @alwaysone You can check the output of that stage on GitLab. Does your runner have all the tools it needs, like node / npm ?
    – marcolz
    Commented May 24, 2017 at 15:41
  • Yep, I forgot image: node:latest. However, I cannot push into Heroku as I need to define Heroku's API key. How do I do that? Through dpl tool?
    – oneturkmen
    Commented May 24, 2017 at 16:09
  • Little update to my latest comment: defined the Heroku API key through the VSC (Gitlab) as a config var
    – oneturkmen
    Commented Dec 15, 2017 at 21:18
1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.