Job run even when dependencies are not run

Hello,

I am trying to setup my CI for automatic Releases. For testing it will be done fore merge requests.
The “stable release” job depends on the flatpak job because it needs the artifacts from it. The problem I have now is that for merge requests flatpak_x86_64 gets not executed, but “stable release” will. I expected that gitlab shows an error, because flatpak_x86_64 is not run. But in my case it does and fails because the artifacts are not available

stages:
  - flatpak
  - deploy

variables:
  UDPLogger_ID: com.gitlab.Murmele.UDPLogger

flatpak_x86_64:
  stage: flatpak
  image: murmele/udplogger:kde_5.12
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
  script:
    - echo "Test" > $UDPLogger_ID/UDPLogger.flatpak
  artifacts:
    expose_as: 'Flatpak Package'
    paths:
      - $UDPLogger_ID/UDPLogger.flatpak

stable release:
  stage: deploy
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  dependencies: ["flatpak_x86_64"]
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  artifacts: # artifacts the release refer
    expire_in: never
    paths:
      - $UDPLogger_ID/UDPLogger.flatpak
  script: # mandatory
    - echo "Creating stable release"
  release:
    name: "Release $CI_COMMIT_TAG"
    tag_name: $CI_COMMIT_TAG
    description: "Release"
    assets:
      links:
        - name: Flatpak
          url: "https://gitlab.com/Murmele/UDPLogger/-/jobs/${CI_JOB_ID}/artifacts/file/$UDPLogger_ID/UDPLogger.flatpak"

If I am adding the needs keyword as below, it shows an error.

stages:
  - flatpak
  - deploy

variables:
  UDPLogger_ID: com.gitlab.Murmele.UDPLogger

flatpak_x86_64:
  stage: flatpak
  image: murmele/udplogger:kde_5.12
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
  script:
    - echo "Test" > $UDPLogger_ID/UDPLogger.flatpak
  artifacts:
    expose_as: 'Flatpak Package'
    paths:
      - $UDPLogger_ID/UDPLogger.flatpak

stable release:
  stage: deploy
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  dependencies: ["flatpak_x86_64"]
  needs: ["flatpak_x86_64"]
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  artifacts: # artifacts the release refer
    expire_in: never
    paths:
      - $UDPLogger_ID/UDPLogger.flatpak
  script: # mandatory
    - echo "Creating stable release"
  release:
    name: "Release $CI_COMMIT_TAG"
    tag_name: $CI_COMMIT_TAG
    description: "Release"
    assets:
      links:
        - name: Flatpak
          url: "https://gitlab.com/Murmele/UDPLogger/-/jobs/${CI_JOB_ID}/artifacts/file/$UDPLogger_ID/UDPLogger.flatpak"

So the question is why do I need the needs keyword? Can I let the dependencies keyword away? What is the best practice to prevent issues like this?

Hi @Murmele

needs keyword defines real dependencies between jobs, if you define a needs keyword in a job, that job will run only if the needed jobs are successful

dependencies keyword is used ONLY to define a list of jobs to fetch artifacts from. Otherwise artifacts from all previous jobs are fetched. This is not a hard requirement, so there is no error if the job that you configured in dependencies fails.

The best practice here would be just using the needs keyword and have the job that creates the artifact in previous or same stage. dependencies is useful for example in multi-arch builds where you need a specific artifact (binary) in next jobs and fetching all artifacts will break your builds.

Hi @balonik,

thank you for the good explanation. Then I will go on with needs!