Skip a job based on dynamic custom variable

Hi guys,

I’m trying to fully skip a job if I have changes, in a specific colder, in a branch diff VS master.

I have something like this

activation-sast:
stage: sast
before_script:
- apk update && apk add git
- git fetch
- ACTIVATION_HAS_CHANGES=$(git diff --name-only $(git rev-parse HEAD) origin/master | grep services/activation/src || echo ‘FALSE’)
script:
- echo “###ACTIVATIONCHANGES###”
- echo $ACTIVATION_HAS_CHANGES ## this works, outputs a list of files or FALSE
- echo “######”
rules:
- if: $ACTIVATION_HAS_CHANGES != “FALSE” # THIS DOES NOT WORK
when: always

it seems that the variable $ACTIVATION_HAS_CHANGES does not exist within the rules scope.

Is there a way to do this?

Thanks in advance for your time

2 Likes

Hi @ned
rules are evaluated before before_script is executed. Even if that wouldn’t be the case, the variable is defined in the context of shell of the job’s container and it is not propagated to GitLab Runner and cannot be used outside before_script or script shell.

Hi @balonik, thanks for your time and reply.

Is there a way for me to achieve that?

I’ve tried to move that command outside of the job, in a before_script or variables but I still can’t seem to get ahold of that variable in the rules section.

1 Like

Only way I can think of is using dynamic child pipelines where you generate one based on logic in your script: