How to diable job start after rebase in gitlab-ci? - gitlab-ci

Good day, I have this rules on my gitlab-task
workflow:
rules:
- if: '$CUSTOM_ENV'
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
when: never
- if: '$CI_COMMIT_BRANCH'
- when: always
...
myTask:
stage: build
script:
- "SOME SCRIPT"
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CUSTOM_ENV'
when: manual
...
myTask run in all merge_request_event. How create "if" which doens't run myTask after (rebase + push)?

There's nothing in particular that distinguishes a rebase and push from any other push event.
You could, however, supply git push options for this:
git push -o ci.variable="SKIP_MY_TASK=true"
Then in your yaml:
- if: '$SKIP_MY_TASK && $CI_PIPELINE_SOURCE == "push" && $CI_OPEN_MERGE_REQUESTS && $CI_COMMIT_BRANCH'
when: never
If you rebase from the UI, you could perhaps set this variable in the CI/CD settings, then unset it afterwards.
Reference

I agree with #sytech, it's work, but not resolve my problem.
Сolleagues suggested a workaround to me:
Before i press "Rebase" button in Gitlab interface, i can set special label (i.e. skip-myTask) for my MR. It's work in one click. And after that i add new rule for myTask.
- if: '$CI_MERGE_REQUEST_LABELS =~ /.*skip-myTask.*/'
when: manual
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CUSTOM_ENV'
when: manual
Important to save this order, otherwise myTask will running.
Sorry for the not quite accurate question!

Related

Gitlab CI variable override in workflow

I want TEST_VAR to be overridden by the workflow but when I run a $NIGHTLY pipeline, it still prints "normal". What am I doing wrong here? According the the precedence, I believe it should work...
image: alpine
variables:
TEST_VAR: "normal"
workflow:
rules:
- if: $CI_COMMIT_BRANCH
- if: $NIGHTLY
variables:
TEST_VAR: "nightly"
test:
script:
- echo $TEST_VAR
One more option is hack with child pipelines.
nightly-run:
variables:
TEST_VAR: "nightly"
rules:
- if: $NIGHTLY
trigger:
include: .gitlab-ci.yml
strategy: depend
It will override global TEST_VAR if "nightly-run" triggers.
Pay attention all jobs that triggered by "parent pipeline" will with CI_PIPELINE_SOURCE == 'parent_pipeline'.
A solution can be resolve your issus is to update the $TEST_VAR in script part.
script:
- |
- if $NIGHTLY
then
$TEST_VAR = "nightly"
else
$TEST_VAR = "others values"
fi

How to skip job in Gitlab pipeline and still run dependencies

I have two jobs in the same stage with a dependency specified via "needs" keyword, for example JobA -> JobB(needs[JobA]).
When I try to skip JobA with a rule (to speedup the build process), JobB throws an 'invalid yaml' error for the "needs" keyword, because the referenced JobA now doesn't exist.
What is the correct syntax/construct to enable such dependency ? Is the use of "rules" in JobA the right approach ?
The simplified version of what I have is:
image1:
stage: build-images
script:
- etc...
rules:
- changes:
- values.env
image2:
stage: build-images
script:
- ...
needs: [image2]
Use needs:optional:
image1:
stage: build-images
script:
- etc...
rules:
- changes:
- values.env
image2:
stage: build-images
script:
- ...
needs:
- job: image1
optional: true

Run a job when a variable is set/unset and it's a merge request

I want a job to run when 2 conditions are met:
There has been a merge request on the branch
A specific variable is not set
With the latest Gitlab in saas mode, I put this config in my job:
rules:
- if: ('$CI_PIPELINE_SOURCE == "merge_request_event"') && ($DESTROY_ONLY == null)
But it's run on a push. Do you see why?
The issue was an incorrect syntax. It should rather be:
rules:
- if: '($CI_PIPELINE_SOURCE == "merge_request_event") && ($DESTROY_ONLY == null)'

How to change variable in gitlab cicd pipeline depending on branch

I need to call a bash script from my gitlab cicd pipeline. When I call it, the input parameter needs to change depending on whether or not this is a merge into master. Basically what I want is this:
if master:
test:
stage: test
script:
- INPUT="foo"
- $(myscript.sh $INPUT)
if NOT master:
test:
stage: test
script:
- INPUT=""
- $(myscript.sh $INPUT)
I'm trying to figure out a way to set INPUT depending on which branch the pipeline is running on. I know there are rules as well as only/except, but they don't seem to allow you to set variable, only test them. I know the brute force way is to just write this twice, one with "only master" and another with "except master", but I would really like to avoid that.
Thanks
I implement this kind of thing using yml anchors to extend tasks.
I find it easier to read and customization can include other things, not only variables.
For example:
.test_my_software:
stage: test
script:
- echo ${MESSAGE}
- bash test.sh
test stable:
<<: *test_my_software
variables:
MESSAGE: testing stable code!
only:
- /stable.*/
test:
<<: *test_my_software
variables:
MESSAGE: testing our code!
except:
- /stable.*/
you get the idea...
Why not have two jobs to run the script and use rules to control when they are ran against master:
test:
stage: test
script:
- if [$CI_COMMIT_REF_NAME == 'master']; then export INPUT="foo"; else INPUT=""; fi
- $(myscript.sh $INPUT)

Why does not gitlab ci execute the rule?

release-gate:
stage: release-gate
script:
- RCM_APPROVAL=$(cat result.json)
- echo $RCM_APPROVAL
rules:
- if: '$RCM_APPROVAL == "0"'
when: manual
- when: always
Folks, even though `echo $RCM_APPROVAL' returns the result as expected, the if condition under rules: does not work. Could you please let me know If I am missing anything here?