1

我在 VSTS 中创建了一个构建定义。我想在“获取资源”之后立即运行一个 powerShell 脚本。

在“获取资源”阶段,我可以看到正在创建一些分支:

From https://domain.visualstudio.com/_git/main
* [new branch]      develop           -> origin/develop
* [new branch]      master            -> origin/master
.......

然后它检查分离的 HEAD 上的最新提交:

Checking out files: 100% (16011/16011), done.
Note: checking out '2c5ac1a6dab64603656be2610dc5c2c627b916d4'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 2c5ac1a...  "Merge develop to master"

然后,当我的 powerShell 脚本到了没有创建分支的时候,当我发出

"git branch -v"

我看到这个:

 2016-11-30T10:42:26.6370529Z * (HEAD detached at 41e7eb4) 41e7eb4 first commit

此外,当我尝试在我的 powerShell 脚本中创建结帐或创建新分支时,它们显示为“红线(错误)”:

2016-11-30T10:42:26.6370529Z * (HEAD detached at 41e7eb4) 41e7eb4 first commit
2016-11-30T10:42:27.0110730Z ##[error]Previous HEAD position was 41e7eb4... first commit
2016-11-30T10:42:27.7881014Z ##[error]Switched to a new branch 'develop'
2016-11-30T10:42:27.9670995Z ##[error] * [new branch]      test -> test
2016-11-30T10:42:28.1945915Z ##[error]Process completed with exit code 0 and     had 3 error(s) written to the error stream.
2016-11-30T10:42:28.1955965Z ##[section]Finishing: PowerShell Script

在“获取资源”阶段创建的其余分支在哪里?为什么我有一个分离的 HEAD?为什么我不能结帐/创建分支?

谢谢,


--UPDATE-- 我的构建能够创建分支并将它们直接推送到远程。我需要授予对我的构建服务帐户的“贡献”和“分支创建”访问权限。但是,尽管实际上已创建分支和提交并将其推送到远程,但我仍然会遇到使我的构建变红的错误。

我的脚本将如下(我简化了它)(它是一个内联 PowerShell 脚本):

git branch -a -v
git fetch --all
git branch -a
git checkout -b master remotes/origin/master

git checkout -b testBranch
Out-File -FilePath auto-file.txt
git add .
git commit -m "just created an auto commit"
git push -u origin testBranch

这是相应的日志:

2016-12-09T08:05:14.9777018Z ##[section]Starting: Build
2016-12-09T08:05:14.9897018Z Current agent version: '2.109.1'
2016-12-09T08:05:15.6197698Z Downloading task: PowerShell
2016-12-09T08:05:16.6935776Z ##[section]Starting: Get Sources
2016-12-09T08:05:16.7755760Z Syncing repository: main (TfsGit)
2016-12-09T08:05:16.7806005Z Prepending Path environment variable with    directory containing 'git.exe'.
2016-12-09T08:05:16.7925998Z ##[command]git version
2016-12-09T08:05:16.9935896Z ##[command]git init "C:\a\1\s"
2016-12-09T08:05:17.0525625Z Initialized empty Git repository in C:/a/1/s/.git/
2016-12-09T08:05:17.0565864Z ##[command]git remote add origin https://domain  .visualstudio.com/MyFirstProject/_git/main
2016-12-09T08:05:17.0795632Z ##[command]git config gc.auto 0
2016-12-09T08:05:17.1035633Z ##[command]git config --get-all http.https://domain   .visualstudio.com/MyFirstProject/_git/main.extraheader
2016-12-09T08:05:17.1337092Z ##[command]git config --get-all http.proxy
2016-12-09T08:05:17.1597344Z ##[command]git -c http.extraheader="AUTHORIZATION: bearer ********" fetch --tags --prune --progress origin
2016-12-09T08:05:18.1333289Z remote: 
2016-12-09T08:05:18.1343246Z remote:                    vSTs           
2016-12-09T08:05:18.1343246Z remote:                  vSTSVSTSv        
2016-12-09T08:05:18.1343246Z remote:                vSTSVSTSVST        
2016-12-09T08:05:18.1343246Z remote: VSTS         vSTSVSTSVSTSV        
2016-12-09T08:05:18.1343246Z remote: VSTSVS     vSTSVSTSV STSVS        
2016-12-09T08:05:18.1343246Z remote: VSTSVSTSvsTSVSTSVS   TSVST        
2016-12-09T08:05:18.1343246Z remote: VS  tSVSTSVSTSv      STSVS        
2016-12-09T08:05:18.1343246Z remote: VS   tSVSTSVST       SVSTS        
2016-12-09T08:05:18.1343246Z remote: VS tSVSTSVSTSVSts    VSTSV        
2016-12-09T08:05:18.1343246Z remote: VSTSVST    SVSTSVSTs VSTSV        
2016-12-09T08:05:18.1343246Z remote: VSTSv        STSVSTSVSTSVS        
2016-12-09T08:05:18.1343246Z remote:                VSTSVSTSVST        
2016-12-09T08:05:18.1343246Z remote:                  VSTSVSTs        
2016-12-09T08:05:18.1343246Z remote:                    VSTs    (TM)        
2016-12-09T08:05:18.1343246Z remote: 
2016-12-09T08:05:18.1343246Z remote:  Microsoft (R) Visual Studio (R) Team Services        
2016-12-09T08:05:18.1343246Z remote: 
2016-12-09T08:05:18.2981730Z From https://domain  .visualstudio.com/MyFirstProject/_git/main
2016-12-09T08:05:18.2981730Z  * [new branch]      develop    -> origin/develop
2016-12-09T08:05:18.2981730Z  * [new branch]      master     -> origin/master
2016-12-09T08:05:18.3001809Z ##[command]git checkout --progress --force 7af1f2611269277da2c520b84148844e650acc4f
2016-12-09T08:05:18.3241819Z Note: checking out '7af1f2611269277da2c520b84148844e650acc4f'.
2016-12-09T08:05:18.3241819Z 
2016-12-09T08:05:18.3241819Z You are in 'detached HEAD' state. You can look around, make experimental
2016-12-09T08:05:18.3241819Z changes and commit them, and you can discard any commits you make in this
2016-12-09T08:05:18.3241819Z state without impacting any branches by performing another checkout.
2016-12-09T08:05:18.3241819Z 
2016-12-09T08:05:18.3241819Z If you want to create a new branch to retain commits you create, you may
2016-12-09T08:05:18.3241819Z do so (now or later) by using -b with the checkout command again. Example:
2016-12-09T08:05:18.3241819Z 
2016-12-09T08:05:18.3241819Z   git checkout -b <new-branch-name>
2016-12-09T08:05:18.3241819Z 
2016-12-09T08:05:18.3241819Z HEAD is now at 7af1f26... try to fix cachedconfig issue
2016-12-09T08:05:18.3261731Z ##[command]git config http.https://domain  .visualstudio.com/MyFirstProject/_git/main.extraheader "AUTHORIZATION: bearer ********"
2016-12-09T08:05:18.3491840Z ##[section]Finishing: Get Sources
2016-12-09T08:05:18.3511738Z ##[section]Starting: PowerShell Script
2016-12-09T08:05:18.3811752Z ==============================================================================
2016-12-09T08:05:18.3811752Z Task         : PowerShell
2016-12-09T08:05:18.3811752Z Description  : Run a PowerShell script
2016-12-09T08:05:18.3821755Z Version      : 1.2.1
2016-12-09T08:05:18.3821755Z Author       : Microsoft Corporation
2016-12-09T08:05:18.3821755Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
2016-12-09T08:05:18.3821755Z ==============================================================================
2016-12-09T08:05:18.4611894Z ##[command]. 'C:\Users\buildguest\AppData\Local\Temp\928a835e-178f-40c1-8ba5-61da96fc8fc7.ps1' 
2016-12-09T08:05:19.1941140Z * (HEAD detached at 7af1f26) 7af1f26 try to fix cachedconfig issue
 2016-12-09T08:05:19.2081155Z   remotes/origin/develop 7af1f26 try to fix cachedconfig issue
 2016-12-09T08:05:19.2090954Z   remotes/origin/master  41e7eb4 first commit
 2016-12-09T08:05:19.2090954Z Fetching origin
 2016-12-09T08:05:19.5648482Z * (HEAD detached at 7af1f26)
 2016-12-09T08:05:19.5648482Z   remotes/origin/develop
 2016-12-09T08:05:19.5648482Z   remotes/origin/master
 2016-12-09T08:05:19.5909153Z ##[error]Previous HEAD position was 7af1f26... try to fix cachedconfig issue
 Switched to a new branch 'master'

 2016-12-09T08:05:19.5939144Z Branch master set up to track remote branch master from origin.
 2016-12-09T08:05:19.6609174Z ##[error]Switched to a new branch 'testBranch'

 2016-12-09T08:05:19.6609174Z [testBranch 847ac8b] just created an auto commit
 2016-12-09T08:05:19.6619147Z  Committer: buildguest <buildguest>
 2016-12-09T08:05:19.6619147Z Your name and email address were configured automatically based
 2016-12-09T08:05:19.6619147Z on your username and hostname. Please check that they are accurate.
 2016-12-09T08:05:19.6619147Z You can suppress this message by setting them explicitly. Run the
 2016-12-09T08:05:19.6619147Z following command and follow the instructions in your editor to edit
 2016-12-09T08:05:19.6619147Z your configuration file:
 2016-12-09T08:05:19.6619147Z 
 2016-12-09T08:05:19.6619147Z     git config --global --edit
 2016-12-09T08:05:19.6619147Z 
 2016-12-09T08:05:19.6619147Z After doing this, you may fix the identity used for this commit with:
 2016-12-09T08:05:19.6619147Z 
 2016-12-09T08:05:19.6619147Z     git commit --amend --reset-author
 2016-12-09T08:05:19.6619147Z 
 2016-12-09T08:05:19.6619147Z  1 file changed, 1 insertion(+)
 2016-12-09T08:05:19.6619147Z  create mode 100644 auto-file.txt
 2016-12-09T08:05:20.4268088Z ##[error]To https://domain .visualstudio.com/MyFirstProject/_git/main

 2016-12-09T08:05:20.4268088Z Branch testBranch set up to track remote branch testBranch from origin.
 2016-12-09T08:05:20.6018082Z ##[error] * [new branch]      testBranch -> testBranch

 2016-12-09T08:05:20.8482358Z ##[error]Process completed with exit code 0 and had 4 error(s) written to the error stream.
 2016-12-09T08:05:20.8492359Z ##[section]Finishing: PowerShell Script
 2016-12-09T08:05:20.8502361Z ##[section]Starting: Post Job Cleanup
 2016-12-09T08:05:20.8562360Z Cleaning any cached credential from repository: main (Git)
 2016-12-09T08:05:20.8602360Z ##[command]git config --unset-all http.https://domain .visualstudio.com/MyFirstProject/_git/main.extraheader
 2016-12-09T08:05:20.8872360Z ##[command]git remote set-url origin https://domain  .visualstudio.com/MyFirstProject/_git/main
 2016-12-09T08:05:20.9142348Z ##[command]git remote set-url --push origin https://domain .visualstudio.com/MyFirstProject/_git/main
 2016-12-09T08:05:20.9342662Z ##[section]Finishing: Post Job Cleanup
 2016-12-09T08:05:20.9352355Z ##[section]Finishing: Build
4

1 回答 1

0

这个问题是因为 git for windows 和 powershell 在标准输入/输出/错误映射方面存在问题,并且 powershell 将信息解释为错误。
用批处理文件替换 powershell 脚本并使用相应的任务解决了该问题。

于 2016-12-01T17:22:34.840 回答