How to check either of variable exist in the gitlab rules?
Asked Answered
S

3

6

I have a gitlab rule condition like below.

'$ACTION && $REGION && $ROLE_ARN && $PACKAGEURL && $ACTION == "new" && $CLOUD_PLATFORM == "aws" && $ROLE_ARN != "" && $PACKAGEURL != "" && $REGION != ""'

Want to modify it a bit so that, it should check either the existence of PACKAGEURL or BUILDRPMREQUIRED above.

Tried keeping as below but getting run even if the PACKAGEURL or BUILDRPMREQUIRED not provided.

'$ACTION && $REGION && $ROLE_ARN && ($PACKAGEURL || $BUILDRPMREQUIRED) && $ACTION == "new" && $CLOUD_PLATFORM == "aws" && $ROLE_ARN != "" && ($PACKAGEURL != "" || $BUILDRPMREQUIRED != "") && $REGION != ""'

I kept in rule as below.

.ifawsfulldeploy:
  rules:
    - if: '$ADMIN_SERVER_IP && $ADMIN_SERVER_IP != ""'
      when: never
    - if: '$ACTION && $REGION && $ROLE_ARN && ($PACKAGE_URL || $BUILDRPMREQUIRED) && $ACTION == "new" && $CLOUD_PLATFORM == "aws" && $ROLE_ARN != "" && ($PACKAGE_URL != "" || $BUILDRPMREQUIRED != "") && $REGION != ""'
      when: always

Other rules should be there, with only variables I am getting syntax error.

.ifawsfulldeploy:
  rules:
    - if: '$ADMIN_SERVER_IP && $ADMIN_SERVER_IP != ""'
      when: never
    - if: '$ACTION && $REGION && $ROLE_ARN && ( $PACKAGE_URL || $BUILDRPMREQUIRED ) && $ACTION == "new" && $CLOUD_PLATFORM == "aws" && $ROLE_ARN != "" && ( $PACKAGE_URL != "" || $BUILDRPMREQUIRED != "" ) && $REGION != ""'
      when: always

And in my job.

  only:
    variables:
      - $PACKAGE_URL
      - $BUILDRPMREQUIRED
  extends:
    - .ifawsfulldeploy
Steel answered 21/6, 2022 at 13:42 Comment(0)
C
8

im new in Gitlab but i had the same question and

$FOO != null

condition helped me. Hope it helped u too!

Chert answered 8/11, 2022 at 16:57 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Analogue
A
3

Have you tried using a rule just for the variables? Like this example from the Gitlab docs:

job1:
  script:
    - echo This rule uses parentheses.
  only:
    variables:
      - ($CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "develop") && $MY_VARIABLE

Source: https://docs.gitlab.com/ee/ci/jobs/job_control.html#only-variables--except-variables-examples

Anaptyxis answered 21/6, 2022 at 13:48 Comment(9)
it seems we can keep only one variableSteel
can you check the content I added at the end of post nowSteel
What about if you add them like: only: variables: - ($PACKAGE_URL || $BUILDRPMREQUIRED) And remove them from the rule above? (Sorry about the formatting, can't figure out how to format the code properly).Anaptyxis
Also, you have a mistake, extends: shouldn't point to the same job name, it's circular reference. @SteelAnaptyxis
sorry for confusion both are at different places, edited the post.Steel
And still doesn't workSteel
Can you please share the whole file? Not just the rules? What else do you have in the job that extends the rule? In the original I mean, before trying with the variables.Anaptyxis
nothing else, only these rules extendsSteel
You can't have jobs without a script element (or before_script/after_script), unless it's a trigger job, so maybe that's why you were getting the syntax errors.Anaptyxis
M
0

You can check variable is not null or check for at least one character in the variable

  rules:
    - if: ( $MY_VARIABLE != null || $MY_VARIABLE =~ /^./ )
Misgive answered 14/6 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.