3

What I have so far

I have configured gitlab ci/cd pipeline for my project. So I have used base template which has 2 stages, build & package:

The build stage builds the project and creates a jar file. The package stage creates an image and store it in the container registry. A .gitlab-ci.yml file has been created.

image: docker:latest
services:
  - docker:dind

stages:
  - build
  - package

build:
  image: gradle:5.6.1-jdk11
  stage: build
  script:
    - gradle clean build -x test
  artifacts:
    paths:
      - build/libs/*.jar

package:
  stage: package
  script:
    - docker build -t registry.gitlab.com/my-project/sample .
    - docker login -u $CI_DOCKER_USERNAME -p $CI_DOCKER_PASSWORD registry.gitlab.com
    - docker push rregistry.gitlab.com/my-project/sample

My problem

I don't want to trigger a build for all every branch. I want to trigger only in the development- and master-branch.

Is there anyway we can do that at project level instead of adding only in each stage?

2 Answers 2

6

The only way to limit when jobs are created is by using only/exceptin the .gitlab-ci.yml file.

To not have to write so much text, you can use extends and also sometimes yaml anchors. More info here:

https://docs.gitlab.com/ee/ci/yaml/#extends

and here:

https://docs.gitlab.com/ee/ci/yaml/#anchors

An example of how extends can be used:

image: docker:latest
services:
  - docker:dind

.only-master-and-develop:
  only:
   - master
   - development

stages:
  - build
  - package

build:
  extends: .only-master-and-develop
  image: gradle:5.6.1-jdk11
  stage: build
  script:
    - gradle clean build -x test
  artifacts:
    paths:
      - build/libs/*.jar

package:
  extends: .only-master-and-develop
  stage: package
  script:
    - docker build -t registry.gitlab.com/my-project/sample .
    - docker login -u $CI_DOCKER_USERNAME -p $CI_DOCKER_PASSWORD registry.gitlab.com
    - docker push rregistry.gitlab.com/my-project/sample

(I haven't tested this file, please let me know if there's something wrong with it.)

2
  • this script actually not working... so i had to add only in both stages. It worked in gitlan-runner in my local thats i made it as answer. But when build get fired from commit it is failing because of invalid yaml and I even checked with lint on gitlab.com and it is also saying same.
    – nani21984
    Nov 12, 2019 at 17:26
  • Thanks for letting me know! I tried it myself now, and I got a yaml error because of a missing colon. I fixed it in the code example I gave. Does it work after adding the colon?
    – MrBerta
    Nov 12, 2019 at 18:27
6

You should specify the target branch using via $CI_COMMIT_BRANCH == "<your_branch_name>".

rules:
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"'
      something regarding the project..

    - if: '$CI_PIPELINE_SOURCE == "web" && $CI_COMMIT_BRANCH == "development"'
      something regarding the project..

I hope above example can help anyone!

Also here is the documentation for these types of questions.

1
  • I think this is better: if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH', so if your master branch is main or master it doesn't matter. Apr 4, 2023 at 19:17

Your Answer

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

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