在 Visual Studio Team Services 中的构建作业中执行 git 命令(是 VSO)

Executing git commands inside a build job in Visual Studio Team Services (was VSO)

[环境:团队服务,GIT,托管构建代理]

我想创建一个能够执行以下操作的团队服务构建定义:

  1. 正在执行脚本以根据存储库中的现有文件生成一些新文件

  2. Commit/push那些生成的文件返回到repo

我可以毫无问题地完成#1。但我不确定我该怎么做#2。

我发现我实际上能够从构建作业中 运行 git.exe。但我不确定如何将凭据传递给 git。根据构建日志,它失败了,因为它试图从 stdin 获取用户名。

我尝试在构建定义中添加一个类似 "git config --global user.name xxxx" 的步骤,但它仍然没有帮助。

完全支持这种情况吗?有什么建议吗?

谢谢!


编辑

我在构建脚本中尝试了以下方法:

git -c http.extraheader="AUTHORIZATION: bearer %SYSTEM_ACCESSTOKEN%" pull ...

它似乎适用于 pull 等命令。但是当我尝试推送更改时,出现以下错误:

fatal: unable to access 'https://example.visualstudio.com/SampleTeam/_git/SampleRepo/': SSL read: error:00000000:lib(0):func(0):reason(0), errno 10054

想法?

您可以从源代码生成的任何文件通常都被视为构建工件,不是 added/committed/pushed git 存储库。

也就是说,如果可以的话,您应该使用 ssh url 而不是 https:ssh 需要一个 ssh 密钥,如果您的 ssh 私钥是无密码的,git 不必在标准输入上查询任何内容。

另一种方法是使用 Microsoft GCH (Git Credential Helper), which is included in Git for Windows (since Git 2.7.3,2016 年 3 月)。
有关示例,请参见 。这会将您的 login/password 缓存在 Windows 凭证存储中。

您可以安装 Git Build Tools 扩展,然后在构建定义中添加 "Allow Git remote access" 任务。确保 "Options" 选项卡下的 "Allow Scripts to Access OAuth Token" 功能已启用。

Enable Git Remote Access

Certain operations require access to the remote repository from during a build. This task updates a remote of the Git repository on the agent to allow access to the upstream repository on Visual Studio Team Services.

Requirements

For this build task to work it is required that the Allow Scripts to Access OAuth Token option is set in the build definition options.

Parameters

Enable Git Remote Access

Remote name: Name of the remote which should be updated. Default is origin.

Related Tasks

Restore Git Remote should be called at the end of the build definition to restore the remote to its original value.

Known issues

Git-Lfs operations, like git lfs fetch still won't work with this. See this Git-Lfs issue

在扩展中添加使用powershell脚本的步骤:

  1. 使用 "EnableGitRemoteAccess.ps1" 脚本中的代码创建一个 power-shell 脚本,并将脚本添加到源代码管理中。
  2. 在构建定义中启用 "Allow Scripts to Access OAuth Token" 选项。
  3. 在构建定义中添加 PowerShell 任务并设置脚本路径以启用 git 远程访问。
  4. 在构建定义中添加另一个 PowerShell 任务以提交和推送更改。

我用来提交和推送更改的代码:

git add .
git commit -m "changesinbuild"
git push origin master 2>&1 | Write-Host

很抱歉在这里回答我自己的问题...

我刚刚从一些 TFS 专家那里得到了一些提示,他们向我指出了这篇文章:https://www.visualstudio.com/en-us/docs/build/scripts/git-commands,它完美地解决了我的问题。

我认为我应该分享它以帮助可能 运行 遇到与我相同情况的人。

这里引用关键步骤(稍微修改了一下格式):

Grant version control permissions to the build service

Go to the Version Control control panel tab

  • Team Services: https://{your-account}.visualstudio.com/DefaultCollection/{your-team-project}/_admin/_versioncontrol

  • On-premises: https://{your-server}:8080/tfs/DefaultCollection/{your-team-project}/_admin/_versioncontrol

On the Version Control tab, select the repository in which you want to run Git commands, and then select Project Collection Build Service (account_name). Grant permissions needed for the Git commands you want to run. Typically you'll want to grant:

  • Branch creation: Allow
  • Contribute: Allow
  • Read: Inherited allow
  • Tag creation: Inherited allow

When you're done granting the permissions, make sure to click Save changes.

Enable your build definition to run Git.exe

  • On the variables tab set this variable: system.prefergit = true
  • On the options tab select Allow scripts to access OAuth token.

使用这些设置,无需安装 Git 构建工具扩展或调整凭据管理器。您也不需要为 OAuth 令牌显式设置额外的 header。我觉得这确实是一个非常巧妙的解决方案。 :)

但非常感谢 Eddie 和 VonC 的帮助!

这只是 Tony's Blues 回答的后续。

抱歉,我不能 post 链接,因为我的声誉低于 10,但所有内容都放在 visualstudio 网站上,所以我相信你可以自己解决这个问题。

要在脚本中允许 GIT 贡献,您需要

  1. 确保您已完成 VSTS 代理先决条件中提到的所有内容

  2. 确保您遵循了 /en-us/docs/build/scripts/git-commands

    中的说明
    • 特别是在您的存储库上向 Project Build Service 帐户 添加所需的权限 - 至少 Contribute(根据需要随意考虑其他权限您的需求)- 这是 "SSL read error"
    • 的真正原因

这个 post 和 Tony 的不同之处在于,在我们的配置中(TFS 2015;安装在 Mac OS Sierra 上的 VSTS 代理)我们必须添加权限 "Contribute" 用于帐户“Project Build Service” - 因此不是名称中提到的单词 "collection" 的帐户。还要小心,不要将它与名为 Project Collection Build Service Accounts 的组混淆 - 我相信它可能在某些条件下使用,但默认情况下不起作用。我指出这一点是因为这是我不小心做的,所以我花了额外的时间来调试错误。

请看下图It can be found under your project --> Control Panel --> Version control --> GIT repository

另外请注意系统要求,因为在我的情况下(在 MacOS Sierra 上),带有两个特定目录的符号链接的部分变得很关键。 OSX 的特定系统要求位于 [github]/Microsoft/vsts-agent/blob/master/docs/start/envosx.md 和状态

安装openssl

$ brew update
$ brew install openssl

创建指向 openssl 库的符号链接 -- 这在 MacOS (Sierra)

上是必需的
$ mkdir -p /usr/local/lib/
$ ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
$ ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/

找出您的 GIT

版本
$ git --version

如果您的版本低于 2.9.0

,请更新 GIT
$ brew update
$ brew install git

Visual Studio Team Services (VSTS) 现在内置了执行此操作的功能:

  1. 授予帐户 Project Collection Build Service (account_name) 访问 VSTS 中相应存储库的权限。
  2. 在代理阶段,勾选允许脚本访问 OAuth 令牌
  3. 现在您可以在任务中引用变量 SYSTEM_ACCESSTOKEN 来访问 git 存储库: git clone https://randomusername:${SYSTEM_ACCESSTOKEN}@instance.visualstudio.com/proj1/_git/repo

参考:https://github.com/Microsoft/vsts-tasks/issues/962

我遇到了同样的问题。解决方案是将 git 配置选项放入 yaml 的脚本部分。有关示例,请参阅此 GitHub 问题:

https://github.com/Microsoft/azure-pipelines-agent/issues/1925