I am new a gitlab and experimenting with gitlab ci. Currently I am confused, how the file exchange between stages works and I didn’t found any explanation about. As described in Best practices | GitLab each runner has its own working directory in the build directory. It seems, that all created files are moved to the build-directory and kept for the following stage(s).
For a better understanding, I’ve created the following gitlab-ci file. The first stage contains to jobs to prepare some files:
The first job
- creates a file “localfile.txt” - this file must exists in the following stage
- creates “job.txt” file with content “1”
The second job
- creates a file “localfile2.txt” - this file must exists in the following stage
- creates “job.txt” file with content “2” (which one wins?)
The second stage shows the results (content of the working directory and of the job.txt file).
To be sure, there is really no interaction between jobs in the same stage I use “>>” to append content to the job.txt file, if there is an interaction, the file will be not overwritten.
stages: - build image - build - deploy build: stage: build before_script: - sleep 10 # Simulating this job takes a longer time as the second job script: - date - "echo 1 > localfile.txt" - "echo 1 >> job.txt" build2: stage: build script: - date - "echo 2 > localfile2.txt" - "echo 2 >> job.txt" deploy: stage: deploy only: - build-deploy-example before_script: - ls -lhA script: - test -f job.txt && cat job.txt
After running the pipeline, the content of job.txt is “2”, although the first job in the build stage runs later:
Wed Dec 8 04:51:59 UTC 2021
$ echo 1 > localfile.txt
$ echo 1 >> job.txt
Wed Dec 8 04:51:53 UTC 2021
$ echo 2 > localfile.txt
$ echo 2 >> job.txt
$ test -f localfile.txt && cat localfile.txt
$ test -f job.txt && cat job.txt
Since the jobs of a stage can runs parallel and normally no one knows which job is finished first, it should actually be random which number is in the job.txt file. But since this is not the case (every time I run the pipeline, the file contains “2”), I interpret that the order in the gitlab-ci file plays a role?
Although I think that this is not a sensible and reliable application, and it should not be implemented this way in practice. But for understanding, it is certainly not wrong to know how the result comes about.
Therefore, in my opinion, caching is interesting only if “git clean” is called in the before_script. Also “git clean” is important to get a clean working (build) directory?