'Gitlab/CI job rule evaluation

I am trying to skip a GitLab ci job based on the results of the previous job, however, as a result, the job never runs. I have the impression that rules are evaluated at the beginning of the Pipeline not at the beginning of the Job. Is there any way to make it work?

cache:
  paths:
  - .images

stages:
  - prepare
  - build

dirs:
  stage: prepare
  image:
    name: docker.image.me/run:latest
  script:
    - rm -rf .images/*
    - [ $(($RANDOM % 2)) -eq 1 ] && touch .images/DESKTOP

desktop:
  stage: build
  needs: ["dirs"]
  image:
    name: docker.image.me/run:latest
  rules:
    - exists:
        - .images/DESKTOP
      when: always
  script: 
    - echo "Why is this never launched?"


Solution 1:[1]

Dynamically created jobs could be a solution (https://docs.gitlab.com/ee/ci/parent_child_pipelines.html#dynamic-child-pipelines).

You could create a yml-file with a your "desktop"-job in section "script" in your "dirs"-job if ".images/DESKTOP" is created. Else your created yml-file should be empty.

The created yml-file can be triggered in a seperat job after "dirs"-job.

I'm using for creating dynamic child pipelines jsonnet (https://jsonnet.org/).

Solution 2:[2]

The rules evaluation is happening at the beginning of a Gitlab pipeline

Quoting from Gitlab docs https://docs.gitlab.com/ee/ci/yaml/#rules

Rules are evaluated when the pipeline is created, and evaluated in order until the first match. When a match is found, the job is either included or excluded from the pipeline, depending on the configuration.

Here the problem seems to be with the usage of exists keyword

Quoting from Gitlab docs https://docs.gitlab.com/ee/ci/yaml/#rulesexists

Use exists to run a job when certain files exist in the repository

But here it seems that .images/DESKTOP is in Gitlab's runner cache, not in your repository.

cache:
  paths:
  - .images

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tolis Gerodimos