Reputation: 971
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
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?
Upvotes: 3
Views: 19009
Reputation: 486
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.
Upvotes: 5
Reputation: 2758
The only way to limit when jobs are created is by using only/except
in 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.)
Upvotes: 8