Skip to content

Add GIT_STRATEGY of "empty" to support clean build directories

Nathan Cain requested to merge nathan.cain/gitlab-runner:git-strategy-empty into main
  • Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA

What does this MR do?

Adds the additional GIT_STRATEGY: empty option requested by gitlab#17103 (closed) and #27499 (closed) which has the unworked Spike story #28226 (closed)

Though I wrote these changes separately, I noticed that this MR is very similar to !2716 (closed). Most of the differences are confined to changes in how the repository is laid out; however, I did choose to leave the pre_clone_script and post_clone_script hooks active based on user comments in the above stories. I personally do not use them, so I could go either way.

Why was this MR needed?

As the stories above explain - this has been an entirely missing ability of GitLab CI/CD since its inception, with the earliest story being 7 years old at this point. I was shocked to learn that this common problem case had not been addressed after migrating the teams I support to GitLab from Atlassian.

Running tasks in an empty directory is a common and straight-forward goal (as shown by the simplicity of this MR). I understand that there may be better approaches (see the single comment on the spike story here: #28226 (comment 1207701518)); however, it has been years and something is better than nothing. This can always be optimized later to copy the files elsewhere such that they can be restored after the script runs; however, the only people who will be negatively impacted by this implementation requiring additional clone operations are the end users who choose this non-default option to fill an unmet need they have in their workflow.

GitLab often preaches a Bias for Action - it is far past time for action on this.

What's the best way to test this MR?

  1. Create a test pipeline that runs on a single shell runner (or any other runner which will preserve files from job to job)
  2. Add a job that echos "Hello World" out to 2 files, runs ls -al, and stores one of the files as an artifact.
  3. Add a job with GIT_STRATEGY=empty that needs the job from step 2 which simply runs ls -al.
  4. Confirm that only the artifact file is in the build directory in the second job.
write-files:
  script:
    - echo "Hello World" > foo.txt
    - echo "Hello World" > bar.txt
    - ls -al
  artifacts:
    paths:
      - foo.txt

only-read-artifacts:
  variables:
    GIT_STRATEGY: empty
  script:
    - ls -al
    - if ! [ -f foo.txt ]; then exit 1; fi # foo.txt should exist
    - if [ -f bar.txt ]; then exit 1; fi # bar.txt should not exist
  needs:
    - write-files

What are the relevant issue numbers?

gitlab#17103 (closed) gitlab#218426 (closed) #27499 (closed) #28226 (closed)

Edited by Axel von Bertoldi

Merge request reports

Loading