Home > database >  In Gitlab CI/CD how to checkout another branch during the pipeline
In Gitlab CI/CD how to checkout another branch during the pipeline

Time:11-29

I would like to checkout to dev branch in a merge request pipeline that is opened for merging to dev with some other branch. I am going to check somethings in this job and then continue with some other jobs depending on the success of this one. Is it possible to do such thing or do I always have to work with the branch the pipeline triggered at?

I add the related part of the gitlab configuration


variables:
  GIT_STRATEGY: clone

checksomething:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "dev"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH
  environment:
    name: development
  before_script:
    - git checkout dev
  script:
    - !reference [.check-something-on-dev]

But I have this error:

$ git checkout dev
error: pathspec 'dev' did not match any file(s) known to git```

CodePudding user response:

Actually it was this easy:

    before_script:
    - git fetch origin dev
    - git checkout dev

CodePudding user response:

Try first a git branch -avv to see what branches are available through the original clone/checkout done the GitLab-CI job.

Depending on the refspec used, the cloned repository might not reference any origin/dev branch, preventing the guess mode of git checkout (or, for that matter, of git switch, the more modern alternative to checkout) to work.

  • Related