[Environment: Team Services, GIT, hosted build agent]
I'd like to create a Team Services build definition that is able to do the following:
Executing a script to generate some new files based on existing files in the repo
Commit/push those generated files back to repo
I can do #1 with no problem. But I'm not sure how I can do #2.
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.
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?
Thanks!
Edit
I tried the following approach in my build script:
git -c http.extraheader="AUTHORIZATION: bearer %SYSTEM_ACCESSTOKEN%" 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
Thoughts?
You can install Git Build Tools extension and then add "Allow Git remote access" task in your build definition. Make sure "Allow Scripts to Access OAuth Token" feature under "Options" tab is enabled.
Add the steps for using the powershell script in the extension:
The code I use to commit and push changes:
Sorry to answer my own question here...
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):
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. :)
But really appreciate the help from Eddie and VonC!
Visual Studio Team Services (VSTS) now has built in functionality to do this:
SYSTEM_ACCESSTOKEN
to access the git repository:git clone https://randomusername:${SYSTEM_ACCESSTOKEN}@instance.visualstudio.com/proj1/_git/repo
Ref: https://github.com/Microsoft/vsts-tasks/issues/962
This is just a followup of Tony's Blues answer.
Sorry I can't post links since my reputation is below 10, but all are placed at visualstudio website, so I'm sure you can figure this out yourself.
To allow GIT contributions within a script you need to
Make sure you have all stuff mentioned in VSTS Agent prerequisites done
Make sure you followed instructions at /en-us/docs/build/scripts/git-commands
What's different between this post and Tony's one is that in our configuration (TFS 2015; VSTS Agent installed on Mac OS Sierra) we've had to add permission "Contribute" for account "Project Build Service" - so not the account with the word "collection" mentioned in name. Also be careful and not mix it up with the group named Project Collection Build Service Accounts - I believe it may be used under certain conditions but it doesn't work by default. I'm pointing this out since this is what I've accidentally did and so I've spent additional time debugging what's wrong.
Please check following picture It can be found under your project --> Control Panel --> Version control --> GIT repository
Also please be careful with system requirements since in my case (on MacOS Sierra) the part with symbolic links for two specific directories turned critical. Specific system requirements for OSX are placed at [github]/Microsoft/vsts-agent/blob/master/docs/start/envosx.md and states
Install openssl
Create symbolic links to openssl libs -- this is required on MacOS (Sierra)
Find out your version of GIT
Update GIT in case you have lower than 2.9.0
I was having this same issue. The solution was to put the git config options within the script portion of the yaml. See this GitHub issue for examples:
https://github.com/Microsoft/azure-pipelines-agent/issues/1925
Any file that you can generate from the source is generally considered as build artifact, and not added/committed/pushed to a git repo.
That being said, if you can, you should use an ssh url instead of an https one: ssh would require an ssh key, and if your private ssh key is passphrase-less, git won't have to query anything on stdin.
Another way is to use the Microsoft GCH (Git Credential Helper), which is included in Git for Windows (since Git 2.7.3, March 2016).
See this answer for an example. That would cache your login/password within the Windows Credential store.