3

How do Gitlab CI behaves, if there are 2 jobs that have artifacts in the same path, and both jobs are the dependency of the same job?

job1:
 stage: stage1
 artifacts:
  paths: 
  - samedir
  
job2:
 stage: stage1
 artifacts:
  paths: 
  - samedir

job3:
 stage: stage2
 dependencies:
 - job1
 - job2

I couldn't find the example in gitlab docs or the expected behaviour.

Is the behaviour deterministic? If the job1 and job2 puts the same file in the samedir, will the job2's artifact overwrite that from job1 because of the order they are defined in the dependencies?

Miguel Trejo
  • 5,913
  • 5
  • 24
  • 49
9ilsdx 9rvj 0lo
  • 7,955
  • 10
  • 38
  • 77

1 Answers1

5

According to the docs

Job artifacts created in the most recent successful pipeline for a specific ref are considered the latest artifacts. If you run two types of pipelines for the same ref, timing determines which artifacts are the latest.

to prove this statement let's consider the following .gitlab-ci.yml file

image: python:3.6

stages: 
    - stage1
    - stage2
    - stage3


job1:
  stage: stage1
  script:
    - echo "artifact from stage 1" > test.txt
  artifacts:
    paths: 
      - test.txt

  
job2:
  stage: stage1
  script:
    - echo "artifact from stage 2" > test.txt
  artifacts:
    paths: 
      - test.txt

job3:
  stage: stage3
  script:
    - cat test.txt
  dependencies:
    - job1
    - job2

We notice that both job1 and job2 give the same reference for the file test.txt.

Now, if I run both jobs at the same time, the most recent succesful job will store the latest artifact to be available at job3. In this example, job1 took 87 seconds while job2 took 90 seconds. The output from job3 confirm this

enter image description here

On the UI CI/CD --> Pipelines you're still able to download each job artifact

enter image description here

Miguel Trejo
  • 5,913
  • 5
  • 24
  • 49
  • 1
    So if I need artifacts from both jobs, I must write them to the separate directories, otherwise I'll get a race condition? – 9ilsdx 9rvj 0lo Mar 26 '21 at 15:07
  • 1
    exactly, @9ilsdx9rvj0lo on your case is there any limitation to write to different directories? – Miguel Trejo Mar 26 '21 at 15:12
  • No, in some cases it could be better. I was just thinking about storing the 'output' of maven build of multiple projects, without pushing them to central or shared storage, but it's probably not the best idea ever. – 9ilsdx 9rvj 0lo Mar 26 '21 at 15:28
  • What if the artifacts used different names like `artifacts:name`. Maybe that still won't work as they will be unpacked to the same location. – CMCDragonkai Jul 21 '22 at 08:55