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

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)'

Related

How to reference a variable within a variable YAML GitLab pipeline

I am using env as a (lowercase) input variable for my pipeline and
I want to be able to have this stage use the correct AWS account based on the environment I input. Right now I have it set just as AWS_ACCOUNT_DEV so I need to have separate stages.
I just want this one stage to be able to be used for all environments based on my input - how can I achieve this?
variables:
AWS_ACCOUNT_DEV: 000000000
AWS_ACCOUNT_NONPROD: 000000000
Import Alertmanager Endpoint:
stage: import
dependencies:
- "Validate Credentials"
tags:
- ${runner}
rules:
- if: $CI_PROJECT_ID != $MONITORING_PROJECT_ID
variables:
AWS_IAM_DEPLOYER_ROLE: "arn:aws:iam::${AWS_ACCOUNT_DEV}:role/${runner_role}"
...
figured it out by adding:
rules:
- if: $CI_PROJECT_ID != $MONITORING_PROJECT_ID
- if: $ecs_env == "dev"
variables:
AWS_IAM_DEPLOYER_ROLE: "arn:aws:iam::${AWS_ACCOUNT_DEV}:role/${runner_role}"
- if: $ecs_env == "nonprod"
variables:
AWS_IAM_DEPLOYER_ROLE: "arn:aws:iam::${AWS_ACCOUNT_NONPROD}:role/${runner_role}"

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 diable job start after rebase in 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!

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?

Ansible registered variable has attribute not found error due to multiple when conditions

How do we check for a registered variable if only one of the two conditions turns out to be true having the same registered variable?
Below is my playbook that executes only one of the two shell modules.
- name: Check file
shell: cat /tmp/front.txt
register: myresult
when: Layer == 'front'
- fail:
msg: data was read from front.txt and print whatever
when: myresult.rc != 0
- name: Check file
shell: cat /tmp/back.txt
register: myresult
when: Layer == 'back'
- fail:
msg: data was read from back.txt and print whatever
when: myresult.rc != 0
Run the above playbook as
ansible-playbook test.yml -e Layer="front"
I do get error that says myresult does not have an attribute rc. What is the best way to print debug one statements based on the condition met?
Note: I wish the fail to terminate the execution of the play as soon as the condition is met hence I beleive ignore_errors with fail will not help.
Note: The shell modules can be any Unix command.
I tried myresult is changed but that too does not help. Can you please suggest.
You may want to look at this logical grouping of tasks: blocks
- name: Check file
block:
- name: check file
shell: cat /tmp/front.txt
register: myresult
ignore_errors: true
- fail:
msg: data was read from front.txt and print whatever
when: myresult.rc != 0
when: Layer == 'front'
- name: Check file
block:
- name: check file
shell: cat /tmp/back.txt
register: myresult
ignore_erros: true
- fail:
msg: data was read from back.txt and print whatever
when: myresult.rc != 0
when: Layer == 'back'
when the variable Layer is set to the front it will execute the shell command for front. but in case when the file doesn't exists it will give the error no such file exists and stop the play. so i have put the ignore_errors in the shell task.it will ignore it and jump to the fail module.