52

I'm trying to implement GitLab CI Pipelines to build and deploy an Angular app. In our project we have two general branches: master (for production only) and develop. For development we create feature/some-feature branches from develop branch. When development finished, we create merge request from feature/some-feature to develop. When merge request approved and merged into develop branch I want to run a Pipeline in order to build application and deploy the build on some environment.

I use the following setup in .gitlab-ci.yml:

image: node:7.5-configured

stages:
    - build
    - deploy

build_job:
    stage: build
    only:
        - develop
    script:
        - /bin/bash <some script here>

...

The problem is that Pipeline executed every time I push into any feature/some-feature branch. What's wrong with my setup? How can I force the Pipeline to be executed only when push performed into develop branch directly?

Solution It was my mistake - I had two different .gitlab-ci.yml files in develop branch and feature/some-feature branch.

2
  • I'm struggling with this too. I my stage is running always. The solutions below didn't help.
    – geoidesic
    Sep 1, 2022 at 9:12
  • You need to use the ref: section under only:
    – Don
    Mar 4 at 20:16

4 Answers 4

21

It was my mistake - I had two different .gitlab-ci.yml files in develop branch and feature/some-feature branch and that's why the Pipeline was executed for all branches.

19

Although it has been passed much time on this discussion I would like to suggest an opinion, which I am using most of the time.

rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"'
      when: on_success
      changes:
        - folder/some_folder/**/* 
    - if: '$CI_PIPELINE_SOURCE == "web" && $CI_COMMIT_BRANCH == "development"'
      when: manual
      changes:
        - folder/some_other_folder/**/* 

This structure solved my various problems, I hope it helps you to!

Regards.

2
  • 7
    It may help for the reader's if you explained why this is a better alternative. It would be appreciated Jun 16, 2022 at 16:26
  • First of all, I am not saying that this is the best approach, everyone can use different approach. I am just saying this approach helped me a lot and yes I have not seen bugs/errors as much rather than the other approaches that I have tried. Anyway, here is the link for the detailed documentation. Jul 16, 2022 at 12:14
2

You can also define the rule at the global level instead of job if you want

Here is the example for that

image: python:3.8

stages:
 - test

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH == "development" || ($CI_PIPELINE_SOURCE == 'merge_request_event' && ( $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "development" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master" ))

unit_test:
  stage: test
  services:
    - postgres:13.4
  before_script:
    - export PYTHONPATH=$PWD
   script:
    - export AWS_DEFAULT_REGION=us-west-2
    - python -c "import sys;print(sys.path)"

-6

You should add the .gitlab-ci.yml file in the branch you need to run the CI on.

2
  • 1
    But then I need to add it to .gitignore as well, right? Otherwise it will overwrite the .gitlab-ci.yml in the master branch when I merge.
    – LostPhysx
    Feb 22, 2021 at 9:03
  • 2
    This doesn't solve the problem. The .gitlab-ci-yml file will eventually end up on other branches because they're creating new branches from development and merging into development. I'm not sure about the .gitignore comment but that file isn't really meant for this. Sep 13, 2021 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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