在 Visual Studio Team Services 的构建作业中执行 git 命令(以前是 VSO) [英] Executing git commands inside a build job in Visual Studio Team Services (was VSO)

查看:21
本文介绍了在 Visual Studio Team Services 的构建作业中执行 git 命令(以前是 VSO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

[Environment: Team Services, GIT, hosted build agent]

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

I'd like to create a Team Services build definition that is able to do the following:

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

  1. Executing a script to generate some new files based on existing files in the repo

将那些生成的文件提交/推送回仓库

Commit/push those generated files back to repo

我可以毫无问题地做到#1.但我不确定如何做 #2.

I can do #1 with no problem. But I'm not sure how I can do #2.

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

I discovered I was actually able to run git.exe from inside a build job. But I'm not sure how I can pass the credential to git. Based on the build logs, it's failing because it's trying to get the username from stdin.

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

I tried adding a step in the build definition with something like "git config --global user.name xxxx" but it still didn't help.

这是一个受支持的场景吗?有什么建议吗?

Is this a supported scenario at all? Any suggestions?

谢谢!

编辑

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

I tried the following approach in my build script:

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

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

It seemed to work for commands like pull, etc. But when I was trying to push the changes, I got the below error:

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

想法?

推荐答案

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

Sorry to answer my own question here...

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

I just got some hint from some TFS expert, who pointed me to this article: https://www.visualstudio.com/en-us/docs/build/scripts/git-commands, which perfectly solved my problem.

我想我应该把它分享出来,以帮助可能遇到和我一样情况的人.

I think I should share it out to help whoever might run into the same situation as I did.

这里我引用了关键步骤(稍微重新格式化):

Here I am quoting the key steps (reformatted a bit):

转到版本控制控制面板选项卡

Grant version control permissions to the build service

Go to the Version Control control panel tab

  • 团队服务:https://{your-account}.visualstudio.com/DefaultCollection/{your-team-project}/_admin/_versioncontrol

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

本地:https://{your-server}:8080/tfs/DefaultCollection/{your-team-project}/_admin/_versioncontrol

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

版本控制选项卡上,选择要在其中运行 Git 命令的存储库,然后选择项目集合构建服务 (account_name).授予您要运行的 Git 命令所需的权限.通常,您需要授予:

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.

  • 变量选项卡上设置这个变量:system.prefergit = true
  • 选项标签上,选择允许脚本访问 OAuth 令牌.
  • On the variables tab set this variable: system.prefergit = true
  • On the options tab select Allow scripts to access OAuth token.

通过这些设置,无需安装 Git Build Tools 扩展或调整凭据管理器.您也不需要为 OAuth 令牌显式设置额外的标头.我觉得这确实是一个非常巧妙的解决方案.:)

With these settings, there is no need to install the Git Build Tools extension or tweak the Credential Manager. You don't need to explicitly set the extra header for OAuth token, either. I feel it's indeed a very neat solution. :)

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

But really appreciate the help from Eddie and VonC!

这篇关于在 Visual Studio Team Services 的构建作业中执行 git 命令(以前是 VSO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆