git - 在 Gitlab CI Auto-Tag commit on build

标签 git powershell tags continuous-integration gitlab

我正在尝试在 GitLab CI 中设置构建过程。 现在我正在使用带有 Powershell 的 Windows 运行器,我希望构建过程自动使用构建的版本号标记提交。

这样做的主要原因是为了在 Gitlab wiki 中启用自动更改日志生成,不要与通常放在实际项目存储库中的 changelog.md 混淆。

我已经尝试从 Powershell 脚本中推送标签,但是推送永远不会完成并且会无限循环,我不清楚为什么会发生这种情况。

脚本调用以下命令

[System.Console]::WriteLine("Tagging git commit (${env:CI_BUILD_REF} ) with tag (v$Version)");
$gitProcess = Start-Process -FilePath "git" -NoNewWindow -Wait -PassThru -ArgumentList "tag -a v$Version ${env:CI_BUILD_REF} -m ${env:CI_BUILD_REF_NAME}_$Version"

if($gitProcess.ExitCode -ne 0)
{
    [System.Console]::WriteLine("Git failed to tag the current build");
    exit $gitProcess.ExitCode
}

[System.Console]::WriteLine("Pushing tag");
$gitProcess = Start-Process -FilePath "git" -NoNewWindow -Wait -PassThru -ArgumentList "push origin v$Version"

if($gitProcess.ExitCode -ne 0)
{
    [System.Console]::WriteLine("Git could not push tags to GitLab");
    exit $gitProcess.ExitCode
}

这是这些行的输出:

Tagging git commit (9b2feb10340c9f8c86ce7c29460e0bfc5dba6f31 ) with tag (v4.1.295.2274)

Pushing tag

进程就卡在这里,标签永远不会推送或显示在存储库中。

为清楚起见,这是批处理等效项:

git tag -a v%Version% %CI_BUILD_REF% git push origin v%Version%

最佳答案

这里的问题只是权限问题。 Gitlab Windows Runner 没有推送权限,只有获取/克隆权限。 为了解决这个问题,我添加了一个 Remote 作为脚本的一部分,它具有针对此特定任务的用户设置。

凭据是从 secret 文件加载的,并且build设置为在每次构建时克隆存储库。

关于git - 在 Gitlab CI Auto-Tag commit on build,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40493286/

相关文章:

django - 使用 Django、Git 和云服务器进行开发

git - 我如何将我最后的 N 提交压缩在一起?

android - Openssl 结果在 Windows 的 cmd 和 power shell 中不匹配

powershell - 将for循环的变量插入另一个变量名中

Python:提取字符串内标签之间的所有子字符串

android - 如果应用程序未打开,在 Android 中发送 NFC 标签?

Git:如何修改服务器的提交?

powershell - 如何合并和压缩多个脚本和CSS文件供生产使用?

jsf - f :facet - there is a list of predefined names attributes?

linux - GitLab:无法从远程存储库读取