gitlab ci 管道中的分离头 - 如何正确推送

用户3142695

我在 CI 管道中的存储库的分离头出现了一些问题。在管道的构建阶段,我正在运行一个脚本,该脚本更改特定文件。更改此文件后,将推送到存储库。

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

运行脚本给我输出

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

我不明白为什么git statusbefore_script 中的第一个已经给了我一个超然的头。

我认为管道通过初始获取创建了一个分离的存储库。所以问题是如何以正确的方式处理我的推送。在我的发布阶段semantic-release,如果我执行上一次推送,我正在运行由于分离头而失败。如果我禁用推送,语义发布将按预期工作。但我当然需要推动。我不明白,我做错了什么。


更新

添加git checkout mainbefore_script 给我第一个git status预期的结果。但是在 push 命令之后我仍然有分离的头 - 我不明白。

$ 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)

尽管推送在发布状态下工作的所有内容semantic-release仍然无法正常工作。我确实得到:The local branch main is behind the remote one, therefore a new version won't be published.

恶棍

我不明白为什么git statusbefore_script 中的第一个已经给了我一个超然的头。

我认为管道通过初始获取创建了一个分离的存储库。

你是对的,GitLab CI 会检查特定的提交而不是某个分支。但是,您可以添加git checkout "$CI_COMMIT_REF_NAME"到您的before_script

before_script:
  - git config --global user.name "Bot"
  - git config --global user.email "[email protected]"
  - git checkout "$CI_COMMIT_REF_NAME"
  - git status

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章