I got some problems with a detached head for my repository in a CI pipeline. In the build stage of the pipeline I am running a script, which changes a specific file. After changing this file get pushed to the repository.
before_script:
- git config --global user.name "Bot"
- git config --global user.email "[email protected]"
- git status
script:
- npx ts-node ./generate.ts
- git push "https://git:${GIT_PUSH_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:main"
- git status
Running the script give me the output
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/fKSu5-y_/0/project/.git/
Created fresh repository.
Checking out 9b4a88be as main...
$ git status
HEAD detached at 9b4a88be
$ git push "https://git:${GIT_PUSH_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:main"
To https://gitlab.domain.com/project.git
9b4a88b..98be83e HEAD -> main
$ git status
HEAD detached from 9b4a88b
I do not understand why the first git status
in the before_script already gives me a detached head.
I think the pipeline creates a detached repository by initial fetching. So the question is how to handle my push in a correct way.
In my release stage I'm running semantic-release
which fails because of detached head if I do the previous push. If I disable the push, semantic release is working as expected. But of course I need the push. I do not see, what I am doing wrong.
Update
Adding git checkout main
in the before_script give me for the first git status
the expected result. But after the push command I still have the detached head - which I do not understand.
$ git checkout main
Branch 'main' set up to track remote branch 'main' from 'origin'.
Switched to a new branch 'main'
$ git status
On branch main
Your branch is up to date with 'origin/main'.
$ git push "https://git:${GIT_PUSH_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:main"
To https://gitlab.domain.com/project.git
336b065..8299e43 HEAD -> main
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
Although everything the push is working on the release state semantic-release
is still not working. I do get: The local branch main is behind the remote one, therefore a new version won't be published.
git fetch origin
, but to re-emphasise nothing has failed. – HildegardehildesheimI think the pipeline creates a detached repository by initial fetching
see docs.gitlab.com/ee/ci/runners/… – Rhaetic