admin管理员组

文章数量:1123650

we use the GitLab-CI for our building/testing envorinment. The system is Ubuntu and the git version is 2.25.1. I tried to update the git version, but appareantly no more recent version is available for this distribution. Because of technical reasons the working directory of the gitlab-runner is not where the code under test is, so we have a manual step for switching the branch in the target directory to the desired git branch.

The job looks like this:

  script:
    - cd <target-directory>
    - git reset --hard origin/master
    - git clean -d -f .
    - git pull :$BOT_SSO@<my-repository>.git $BRANCH
    - git status

What i thought it does is to switch to the target directory, reset whatever there is to the last known master state and clean up surviving untracked files. Afterwards i want to get and switch to the desired branch and check the state now.

What it actually does is appareantly to just get the files from the desired branch but not switching to it. The log looks like this:

$ cd <target-directory>
$ git reset --hard origin/master
HEAD is now at 08d09b4 <Commit-Message>:
$ git clean -d -f .
$ git pull <repository> $BRANCH
From <repository>
 * branch            <desired-branch> -> FETCH_HEAD
Updating 08d09b4..46ba060
Fast-forward

... changes 

$ git status
On branch master
nothing to commit, working tree clean
Cleaning up project directory and file based variables
00:00
Job succeeded

So this job changes files but does not change the branch.

If i alter the script like this:

  script:
    - cd <target-directory>
    - git reset --hard origin/master
    - git clean -d -f .
    - git pull :$BOT_SSO@<my-repository>.git
    - git checkout $BRANCH
    - git status

I get an error:

$ cd <target-directory>
$ git reset --hard origin/master
HEAD is now at 8d47e28 <commit-message>
$ git clean -d -f .
$ git pull <repository>
From <repository>
 * branch            HEAD       -> FETCH_HEAD
Updating 8d47e28..08aa112
Fast-forward
 ... file changes
 11 files changed, 45 insertions(+), 78 deletions(-)
$ git checkout $BRANCH
error: pathspec '<branch name>' did not match any file(s) known to git
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit status 1

So it looks like the branch i want to checkout it is unkown to git. I also tried to replace pull with fetch but the result remains the same. I also observed if i update the master branch with the statement above, the 'reset origin/master' resets it not to the current state of the master branch remote, but to the last known state locally.

Does anyone know how to fix this?

本文标签: Try to checkout git branch with gitlab ci job does not work as expectedStack Overflow