在 Gitlab CI 构建时自动标记提交

In Gitlab CI Auto-Tag commit on build

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

这样做的主要原因是为了在 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 推送来源 v%Version%

这里的问题只是一个权限问题。 Gitlab Windows Runner 没有推送权限,只有 fetch/clone。 为了解决这个问题,我添加了一个遥控器作为脚本的一部分,该脚本具有针对此特定任务的用户设置。

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