Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing Git repo #962

Closed
pascalberger opened this issue Dec 10, 2015 · 17 comments
Closed

Accessing Git repo #962

pascalberger opened this issue Dec 10, 2015 · 17 comments

Comments

@pascalberger
Copy link
Contributor

This is maybe not directly on issue with the tasks but more with the agent. What is the recommended approach to run Git operation during a build?

My use case is that I have a repository utilizing git-lfs. Since VSTS only does a checkout I manually need to call git lfs fetch afterwards. The problem here is the authentication. I want to avoid to have to setup an service account for accessing the repo just for this case. The agent already somehow manages to checkout the repo. Is it possible to use the same approach for additional operations?

@bryanmacfarlane
Copy link
Contributor

The current prepare does it via libgit and uses the generated per build OAuth token sent down to the agent.

That oauth token is available to custom task authors so that's one possibility.

However in the sprint rolling out right now (S92 - in a couple scale units so far), we added a checkbox on the options tab to make the token available to 'ad-hoc scripts'. This means you trust everyone that can edits your definition (and all process and scripts that run it**) :) It's a variable (can't remember off the top of my head - do a set and inspect)

Now, onto getting git.exe to use it. There's ASKPASS and credhelpers but those are actually problematic with per build passwords since cred helpers can store and then you don't get called back, then your token for that build expires, next build fails :(

So, one option is to edit your remote. Call get to set the remote to Ouath:@ At the end of the script you can set it back.

We are contemplating doing what I just mentioned for you as an option. If we do that (and you check that box), ad-hoc git scripting via cmd files just works. What won't work is authenticated sub-modules with that approach since the url is embedded in the sub-module ref

@pascalberger
Copy link
Contributor Author

Thanks. I will wait for S92 and try the OAuth token / edit remote way.

@pascalberger
Copy link
Contributor Author

@bryanmacfarlane I tried to update the git remote using the SYSTEM_ACCESSTOKEN environment variables but couldn't make it work. Did I get it right that I should be able to replace the url of the origin remote (https://mytenant.visualstudio.com/DefaultCollection/MyProject/_git/MyRepo) with the OAuth token like this: https://<Token>@mytenant.visualstudio.com/DefaultCollection/MyProject/_git/MyRepo) and then be able to perform git lfs operations? Unfortunately I still get an "Git credentials not found error"

@bryanmacfarlane
Copy link
Contributor

Needs to be username:password

So use any username like OAuth and then token as password

@pascalberger
Copy link
Contributor Author

Thanks, https://OAuth:<Token>@mytenant.visualstudio.com/DefaultCollection/MyProject/_git/MyRepo unfortunately still doesn't work

@bryanmacfarlane
Copy link
Contributor

Hmmm - that's what our xplat agent does. I'll try it out later. Until then any other details like the cmds run, output, scripts etc would help

@pascalberger
Copy link
Contributor Author

I've written a build task which updates the remote. You can find it in this PR or in this ZIP.

My test build definition is:

  • The custom build task
  • A command line task calling git with lfs fetch as argument

Allow Scripts to Access OAuth Token is set in the options. Repository clean is set to true.

The repo contains one binary file which was added with git lfs.

While calling the git lfs fetch it throws the Git credentials for https://mytenant.visualstudio.com/DefaultCollection/MyProject/_git/MyRepo/info/lfs/objects/841e3a4174433932fc9e0a7f506cfda7f240b640199a656632300a971d11bd58 not found. error.

If I manually call git remote -v in the Git repository on the agent after running the build it shows the remotes being updated with username/password.

@bryanmacfarlane
Copy link
Contributor

Just as a test - does it work if it's just gig fetch? Trying to figure out if it's an lfs thing ...

@pascalberger
Copy link
Contributor Author

A git fetch works fine. Seems to be an lfs issue

@pascalberger
Copy link
Contributor Author

@bryanmacfarlane I opened a ticket on git-lfs regarding this: git-lfs/git-lfs/issues/906

@pascalberger
Copy link
Contributor Author

Closing the issue since the VSTS related part seems to work fine with standard Git commands. Lfs related stuff should be handled in git-lfs/git-lfs/issues/906.

Thanks @bryanmacfarlane for the help!

@rmarinho
Copy link
Member

rmarinho commented Mar 8, 2017

As some feedback i think we could have a GetSources as a task,where we have a primary source but can i have other sources.. TeamCity allows this with multiple VCS roots

@ericsciple
Copy link
Contributor

@rmarinho agreed. we are working toward that.

@ggirard07
Copy link

@ericsciple is there any reference on VSTS features timeline about it I can look for to know when this will be completed and actually released?

@ericsciple
Copy link
Contributor

@ggirard07 no but we've been wanting to do it for a long time and we're working a feature now that i think is going to force it (next few months would be my best guess). we've also been making incremental steps along the way to decouple get sources from the agent.

for a workaround today you could clone the repo yourself. if you check Allow scripts access to OAuth token then you can use the token for the Project Collection Build Service identity.

@fahnzmode
Copy link

@ericsciple - would you happen to have any additional info on the possibility of VSTS builds being able to checkout multiple git repositories? Would be very nice to have first-class support for this.

Dave Burnison mentions the steps needed to cleanly clone the git repo yourself:

  1. Use a PowerShell task to call a script that:
    – Changes directories to the $(build.sourcesDirectory)
    – Creates a new folder called “Build” under $(build.sourcesDirectory)
    – Changes directories to the $(build.sourcesDirectory)\Build
    – Calls “git init”
    – Calls “git clone https://VSTSAccountName.visualstudio.com/SomeCollection/Build/_git/BuildUtilsStoredInGit” where the URL points to a Git repository that has a set of scripts that are similar to $/Build/PSScripts in the blog post.
  2. Use additional PowerShell tasks to execute the script(s) as needed from $(build.sourcesDirectory)\Build (e.g. $(build.sourcesDirectory)\Build\PSScripts\ApplyVersionToAssemblies.ps1 )
  3. At the end of your build process add a command line task to call the following command which will remove the local copy of the Git repository
    – RmDir /S /Q “$(build.sourcesDirectory)\Build”

(see his 'June 1, 2016 at 8:22 pm' comment here: https://blogs.msdn.microsoft.com/premier_developer/2016/04/13/tips-for-writing-powershell-scripts-to-use-in-build-and-release-tasks/)

@ericsciple
Copy link
Contributor

It's something we want to do for multiple reasons. It's not next though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants