在GitLab CI中,如何在构建时自动为提交打标签?

3
我正在尝试在GitLab CI中设置构建流程。 目前我正在使用Windows运行程序和Powershell,并希望构建流程能够自动为提交打上版本号标签。
这样做的主要原因是为了启用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%


你尝试过使用$env:CI_BUILD_REF吗?我认为你没有正确地使用环境变量。而且在使用环境变量时不需要做$()处理,所以不要加上{}。 - 4c74356b41
这些代码行看起来运行良好,我会将输出添加到帖子中。 - SomeInternetGuy
你尝试过从命令行窗口执行此操作吗?它能正常工作吗? - 4c74356b41
1个回答

2
问题很简单,这是一个权限问题。 Gitlab Windows Runner没有推送权限,只有获取/克隆权限。 为了解决这个问题,我在脚本中添加了一个远程仓库,该仓库有一个专门用于此任务的用户。 凭据从secrets文件加载,并设置构建以每次构建都克隆存储库。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接