title: Dynamic execution of gitlab-ci jobs
body: <p>I have a job called 'job1' which checks something. Depending on this result 'job2' or 'job3' should be executed. I tried it with the build.env artifact. But the environment variable <code>EXIT_CODE</code> is only available in the script section and not in the <code>rules</code> or <code>only</code> job annotations. Then I tried it with global variables (<code>variables: EXIT_CODE: &quot;0&quot;</code>) and trying to set them in the script section of 'job1'. But this doesn't work as well. How I'am supposed to execute job 2/3 depending on some results of job1</p>
  <pre><code>stages:
    - check
    - merge
  
  variables:
    EXIT_CODE: &quot;0&quot;
  
  job1:
    stage: check
    image: ubuntu:20.10
    allow_failure: true
    script:
      - set +e
      - git merge --ff-only github/master
      - EXIT_CODE=4
      - EXIT_CODE=&quot;4&quot;
        # - echo &quot;EXIT_CODE=$?&quot; &gt;&gt; build.env
        # - cat build.env
        #artifacts:
        #  reports:
        #    dotenv: build.env
  
  job2:
    stage: merge
    dependencies:
      - job1
    rules:
      - if: '$EXIT_CODE == &quot;0&quot;'
    script:
      - echo &quot;WILL FAIL $EXIT_CODE&quot;
        #  only:
        #    variables:
        #      - $EXIT_CODE == &quot;128&quot;
  
  job3:
    stage: merge
    dependencies:
      - job1
    script:
      - echo &quot;WILL success&quot;
    only:
      variables:
        - $EXIT_CODE == &quot;0&quot;
  </code></pre>