Petziferum
Petziferum

Reputation: 229

gitlab-ci.yml only on master branch

I have a gitlab-ci.yml file like this, and want to run it only on Branch Master. If there is a push into develop branch the Pipeline should NOT start.

I tried with 'only' keyword, but it shows an Error.

stages:
  - info
  - build
  - test
  - review
  - cleanup
  - deploy-dev
  - integration-test
  - deploy-test
  - system-test
  - deploy-production

only:
  refs:
    - master

Upvotes: 10

Views: 16207

Answers (2)

Guillermo Gerard
Guillermo Gerard

Reputation: 892

To define a trigger rule for every stage you can use the workflow keyword, like this:

    workflow:
      rules:
        - if: $CI_COMMIT_TAG
          when: never
        - if: $CI_COMMIT_BRANCH == 'master'

This has to be on the "root" of your yaml, as it is not part of any job in particular.

In the example above, I am telling the pipeline to avoid running when a repository tag is pushed and to run only when a commit is done in the master branch.

You can use this as a base and add other conditions to trigger the stages in the pipeline, the complete documentation on this matters can be found here: https://docs.gitlab.com/ee/ci/yaml/#workflow

Upvotes: 20

Marco Somma
Marco Somma

Reputation: 21

I thin you have an indentation problem here. It should be something like:

stages:
 - job_1
 - job_2
 - ....
 - job_n

job_1:
  stage: job_1
  ....
  only:
    refs:
      - master

job_2:
  stage: job_2
  ....
  only:
    refs:
      - master
....

You need to define the target branch for each stage.

Upvotes: 2

Related Questions