yml Rules changes and !reference - reference

I use reference for my jobs's rules :
.rules_base:
rules:
CAS_1: $CI_COMMIT_TAG == null && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE != "merge_request_event" #CAS_1 WHEN PUSH INTO BRANCH
CAS_2: $CI_COMMIT_TAG == null && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE != "merge_request_event" #CAS_2 WHEN PUSH INTO MASTER
CAS_3: $CI_PIPELINE_SOURCE == "merge_request_event" #CAS_3 WHEN MERGE BRANCH INTO MASTER
CAS_4: $CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.[0-9]+\.[1-9]+$/ && $CI_COMMIT_BRANCH == null && $CI_PIPELINE_SOURCE != "merge_request_event" #CAS_4 WHEN TAG INTO BRANCH
CAS_5: $CI_COMMIT_TAG =~ /[0-9]+\.[0-9]+\.0+\.0+$/ && $CI_COMMIT_BRANCH == null && $CI_PIPELINE_SOURCE != "merge_request_event" #CAS_5 WHEN TAG INTO MASTER
clone_job:
tags:
- EXEMPLE
stage: clone
needs : []
rules:
- if: !reference [.rules_base, rules, CAS_1]
- if: !reference [.rules_base, rules, CAS_2]
- if: !reference [.rules_base, rules, CAS_3]
- if: !reference [.rules_base, rules, CAS_4]
- if: !reference [.rules_base, rules, CAS_5]
changes:
- /*.cpp
- /*.h
- /*.xml
script:
changes doesn't work.
Job start when I push a change on .md file in branch or master.
Do you have some ideas ?
Regards

Related

Gitlabci: How to pass dynamic variable to downstream pipeline to use it for remote include line?

I can pass the variable successfully from upstream to downstream pipeline. But I can't get the dynamic variable value for ref: $branch line. I'm getting the following error in the downstream pipeline: Project main/project reference does not exist! I think that somehow I can't give the value to ref section.
Upstream pipeline:
---
stages:
- test
test:
stage: test
variables:
branch: "dev"
trigger:
project: child/test
strategy: depend
branch: master
allow_failure: false
rules:
- if: $CI_COMMIT_BRANCH == 'master' || $CI_COMMIT_BRANCH == 'dev'
when: never
Child pipeline:
---
stages:
- hellotest
hellotest_1:
stage: hellotest
trigger:
include:
- project: 'main/project'
file: 'samplejob/example.gitlab-ci.yml'
ref: $branch

GitLab CI does add a job even if the rules:changes do not match

I have a multi-module Maven project containing the following modules:
core
backend
frontend
Both backend and frontend modules depend on the core module.
I have two separate jobs in gitlab-ci.yml to build backend and frontend jars and have a workflow section.
The problem is that BOTH backend and frontend are added to a pipeline even if I change ONLY file(s) in one of them, when merge-request is opened.
Expected behavior is the following:
If there are changes in core module only - add both build-backend and build-frontend jobs.
If there are changes in backend module - add build-backend only.
If there are changes in frontend module - add build-frontend only.
Nevertheless, everything is fine for the branch-pipelines. Only merge-request-pipelines are affected.
variables:
SKIP_BACKEND_BUILD:
value: "false"
SKIP_FRONTEND_BUILD:
value: "false"
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH || $CI_COMMIT_TAG
build-backend:
stage: build
script:
- mvn -q -e clean package -pl core,backend
rules:
- if: $SKIP_BACKEND_BUILD == "true"
when: never
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
- core/**/*
- backend/**/*
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH != "master"
changes:
- core/**/*
- backend/**/*
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"
- if: $CI_PIPELINE_SOURCE == "web" && $SKIP_BACKEND_BUILD == "false"
- when: never
artifacts:
paths:
- backend/target/*.jar
build-frontend:
stage: build
script:
- mvn -q -e clean package -pl core,frontend
rules:
- if: $SKIP_FRONTEND_BUILD == "true"
when: never
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
- core/**/*
- frontend/**/*
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH != "master"
changes:
- core/**/*
- frontend/**/*
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"
- if: $CI_PIPELINE_SOURCE == "web" && $SKIP_FRONTEND_BUILD == "false"
- when: never
artifacts:
paths:
- frontend/target/*.jar

Is there an alternative to gitlab ci workflow:rules:variables in lower versions of gitlab

The workflow:rules:variables rule was introduced in gitlab 13.11 and is not yet supported in my version 13.8.8, is there an easy alternative?
variables:
VAR1: my var 1
VAR2: my var 2
workflow:
rules:
- if: $CI_COMMIT_REF_NAME =~ /master/
variables:
VAR1: overridden var 1
- if: $CI_COMMIT_REF_NAME =~ /feature/
variables:
VAR2: overridden var 2
VAR3: new var 3
- when: on_success

$CI_ENVIRONMENT_NAME remains empty after set in previous stage

I have the following GitLab pipeline. For brevity I have removed some code. In deploy-dev stage I am creating environment dev. Then in the next stage test-postdeploy I am using $CI_ENVIRONMENT_NAME to set the base_url variable value. However, in test-postdeploy stage the $CI_ENVIRONMENT_NAME always empty
stages:
deploy:dev
deploy:qa
deploy:prod
test:postdeploy
deploy-dev:
image: node
stage: deploy:dev
environment:
name: dev
script:
- . deploy/scripts/deploy-dev.sh
rules:
- if: $CI_COMMIT_REF_NAME == "dev"
deploy-qa:
image: node
stage: deploy:qa
environment:
name: qa
script:
- . deploy/scripts/deploy-qa.sh
rules:
- if: $CI_COMMIT_REF_NAME == "qa"
deploy-prod:
image: node
stage: deploy:prod
environment:
name: prod
script:
- . deploy/scripts/deploy-prod.sh
rules:
- if: $CI_COMMIT_REF_NAME == "release"
test-postdeploy:
tags:
- xlarge
image: node
stage: test:postdeploy
variables:
base_url: ""
script:
- echo "$CI_ENVIRONMENT_NAME" // this is empty
- script/postdeploy-test.sh
rules:
- if: $CI_ENVIRONMENT_NAME == "dev"
variables:
base_url: "https://dev.example.com"
- if: $CI_ENVIRONMENT_NAME == "qa"
variables:
base_url: "https://qa.example.com"
- if: $CI_ENVIRONMENT_NAME == "prod"
variables:
base_url: "https://prod.example.com"
UPDATE 1
I can probably pass environment name to another stage using https://docs.gitlab.com/ee/ci/variables/#pass-an-environment-variable-to-another-job
However, this does not make sense. Environment Name is most basic value that any ci/cd pipeline would need to know. The CI_ENVIRONMENT_NAME
variable should be available in all stages once its set without doing some extra work.

Gitlab CI automatical run pipeline on merge request

I have below pipeline.
After creating merge request, created Detached merge request pipeline with failed status (app1 - no stages/jobs).
In scope of below pipeline need to run pipeline when merge request is created and after merging changes main. Flow described in here Gitlab CI Child pipeline
Below pipeline does not work.
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "schedule"'
- if: '$CI_PIPELINE_SOURCE == "web"'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_PIPELINE_SOURCE == "push"'
stages:
- child-pipelines
app1:
stage: child-pipelines
variables:
COMPONENT NAME: 'app1'
trigger:
include:
- local: .ci/.gitlab-ci.yml
strategy: depend
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
- test1/**/*
variables:
DEPLOY_RELEASE: '11111'
- if : '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "main"'
changes:
- test1/**/*
variables:
DEPLOY_RELEASE: '222222'
app2:
stage: child-pipelines
variables:
COMPONENT NAME: 'app1'
trigger:
include:
- local: .ci/.gitlab-ci.yml
strategy: depend
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
- test2/**/*
variables:
DEPLOY_RELEASE: '11111'
- if : '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "main"'
changes:
- test2/**/*
variables:
DEPLOY_RELEASE: '222222'
If you want to run a pipeline when merge request was created and after merge to main branch, take a look this example:
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main" && $CI_PIPELINE_SOURCE == "push"
job1:
script:
- echo "This job runs in merge request and also after merge to main branch"
According to Gitlab documentation.
If you want to run a rule for the entire pipeline:
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
job1:
script:
- echo "This job runs in merge request pipelines"
job2:
script:
- echo "This job also runs in merge request pipelines"
If you want to run a rule in certain job:
job1:
script:
- echo "This job runs in merge request pipelines"
only:
- merge_requests

Resources