GitLab CI - Trigger the job based on pattern match in the rules from dotenv variables of an other job

I have two jobs in the Gitlab CI file.

  1. First job env_var_test generates the dotenv variables from the command.

    echo '{"apple":red,"boy":"bar","cat":"white"}' | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]'

  2. Second job env_var_retrive_test looks for a variable from env_var_test dotenv variables and if the variable match the predefined value of the CICD rules, it triggers


env_var_test:
  stage: build
  image: $CFIMAGE
  script:
    - echo '{"apple":red,"boy":"bar","cat":"white"}' | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' > deploy.env
  artifacts:
    reports:
      dotenv: deploy.env
  tags:
    - linux

env_var_retrive_test:
  stage: deploy
  image: $CFIMAGE
  script:
    - echo "[ $apple - $boy - $cat ]"
  tags:
    - linux
  rules:
    - if: '$boy == "bar"'
      when: always

With this setup, I tested them and could see the variables are correctly printing echo "[ $apple - $boy - $cat ]". However, the job was not triggering if I defined the variables in the rules section

  rules:
    - if: '$boy == "bar"'
      when: always

Please correct me if I’m doing it wrong or help me with any better approach to do the same.

-Thanks

Hi @Logicvenu1

I’m not sure whether I’ve properly understood this, but I think what you’re trying to do is to write a file in the first test which contains some env vars, then use those env vars in the second job?

I think you maybe have two issues here. One is that artifacts:reports is for things like unit tests where you want the results to appear automatically on a merge request page, or on the pipeline Tests output. You want artifacts:paths here:

env_var_test:
 ...
  artifacts:
    paths:
      deploy.env
  ...

The second thing is that anything in a script section is essentially a Bash command.

I’m not sure whether your Docker container is doing something special with that deploy.env file, but if you want the contents to be used as environment variables, you will need to explicitly write something in the script or before_script of env_var_retrive_test to export those variables to the shell.

Thanks, @snim2

Basically, I am exporting a couple of variables by referring
GitLab CI/CD artifacts reports types | GitLab

And the variables exported from the job env_var_test, wanted to make use under the rules: section of job env_var_retrive_test. The same variables are not required anywhere else in the second job and are only for deciding the env_var_retrive_test job to run on not based on the rules match.

Ah right, I didn’t know about dotenv variables, so my comments above are not relevant at all!

My guess is that maybe these variables aren’t available early enough in the CI job to be used by rules.

This issue is relevant to you, and I would suggest that you comment there and vote the issue up (which will make it more likely to be scheduled in an upcoming sprint).

I would guess though, that you’ll have to do something more complicated to get the pipeline behaviour you want, whilst that issue is still open.