How to skip 'Reinitialized existing Git repository' on Gitlab CICD Stage
Asked Answered
O

4

21

Below is my YML file structure. I want the following up stages to be run without reinitializing the git repository. The git repository should only be initialized during the first stage, which is the build stage.

variables:
  GIT_STRATEGY: fetch

stages:
  - build
  - run_test
  - run_test2

build_job:
    variables:
        test_env: "test"
    stage: build
    script:
        - "powershell -File ./Scripts/BuildSolution.ps1"
    only:
        refs:
          - TDD-G2

run_test:
    variables:
        test_env: "test"
    stage: run_test
    script:
        - "powershell -File ./Project1/scripts/RunSelenium.ps1"
    artifacts:
        when: always
        paths:
          - ./Project1/TestResults

run_test2:
    variables:
        test_env: "test"
    stage: run_test2
    script:
        - "powershell -File ./Project2/scripts/RunSelenium.ps1"
    artifacts:
        when: always
        paths:
          - ./Project2/TestResults
Obidiah answered 8/10, 2020 at 4:10 Comment(1)
could you please give a hint, have you solved the question? I've faced with the same situation.Struck
P
11

I was facing quite similar problem and where I had three stage's build, test and deploy. In deploy stage i wanted to create a tag something like But I was facing a strange issue where even after deleting the tag from remote (Note: i was working alone on this project) the pipeline was failing continuously saying the tag already exists. But after some time the same job completed successfully.

To fix this I set the strategy to clone(Explained below) only for deploy(Git repository was reinitializing again here) job but for build and test it is fetch(default option). As

variables:
  GIT_STRATEGY: clone

For GitLab:

Git strategy

Introduced in GitLab Runner 8.9.

By default, GitLab is configured to use the fetch Git strategy, which is recommended for large repositories. This strategy reduces the amount of data to transfer and does not really impact the operations that you might do on a repository from CI.

There are two options. Using:

  • git clone: which is slower because it clones the repository from scratch for every job
  • git fetch: which is default in GitLab and faster as it re-uses the local working copy (falling back to clone if it doesn’t exist). This is recommended, especially for large repositories.

Detail Read is here and here

Hope it help to someone who came to this issue.

Preform answered 7/3, 2021 at 17:35 Comment(0)
L
9

You could use the variable GIT_CLEAN_FLAGS to instruct the gitlab-runner to call git clean in a constrained way. For example, by specifying none, you could disable calling git clean alltogether, so files produced by the previous stage won't be deleted.

run_test:
    variables:
        test_env: "test"
        GIT_CLEAN_FLAGS: none

https://docs.gitlab.com/ee/ci/runners/configure_runners.html#git-clean-flags https://docs.gitlab.com/ee/ci/large_repositories/#git-clean-flags

Lanni answered 2/2, 2022 at 7:33 Comment(0)
V
4

This is what you are looking for:

job1:
  variables:
    GIT_CHECKOUT: "false"

Use that variable in the job where you want to skip checking out the repo. Make sure the GIT_STRATEGY you are using on your project on GitLab is either 'clone' or 'fetch', otherwise it won't work.

Reference: https://docs.gitlab.com/ee/ci/runners/configure_runners.html#git-checkout

Vibrate answered 23/2, 2023 at 13:20 Comment(1)
Exactly what I have been looking for!Madras
S
0

if in any of your jobs you store an artifact or make any changes inside the git initialized repository, it will be removed in the subsequent jobs due to the default GIT_STRATEGY reinitializing git during every single job run. setting GIT_CLEAN_FLAGS to none will keep your local copy safe but would still fetch and initialize the repo.

i did not find any of the above answers appealing, for in my use case, i needed to maintain my local version of the project exactly the same, throughout the entire pipeline without pulling or pushing any changes back. setting GIT_STRATEGY: none in subsequent jobs helped skip all the git operations giving me exactly what i want.

Sochor answered 12/7 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.