12

I'd like to automatically tag a commit when a build is successful on VSO build vNext. I've read the doc including this page https://msdn.microsoft.com/Library/vs/alm/Build/scripts/variables and I've setup a small PowerShell script. First of all, it seems that BUILD_REPOSITORY_AUTH_USERNAME variable (and its friend password) are empty. I guess they're only available with external gits ? Second of all, it seems that the checkout for build is done via tasks which runs LibGit2Sharp, therefore credentials are not stored in any helper.

This is my PowerShell script :

git tag $Env:BUILD_BUILDNUMBER
git status
git config -l 
git push --progress https://$Env:GITUSER:$Env:GITPASSWORD@myrepo.visualstudio.com/DefaultCollection/_git/myproject tag $Env:BUILD_BUILDNUMBER
git status
exit

It works well on my machine but on the agent (hosted pool) but it seems to hang on the agent at the push command (after 1h, it got automatically killed by the controller).

Here are the log:

******************************************************************************
Starting task: Powershell: tools/GitCommands.ps1
******************************************************************************
HEAD detached at 819e778
nothing to commit, working directory clean
core.symlinks=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.packsizelimit=2g
help.format=html
http.sslcainfo=/bin/curl-ca-bundle.crt
sendemail.smtpserver=/bin/msmtp.exe
diff.astextplain.textconv=astextplain
rebase.autosquash=true
core.bare=false
core.filemode=false
core.symlinks=false
core.ignorecase=true
core.logallrefupdates=true
core.repositoryformatversion=0
remote.origin.url=https://myrepo.visualstudio.com/DefaultCollection/_git/myproject
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

******************************************************************************
Finishing task: PowerShell
******************************************************************************

As you can see, no log for the push nor the status command. Any idea on how to achieve this ?

  • Seems like there is no env variable BUILD_REPOSITORY_AUTH_USERNAME anymore. so it's a big question how to authorize into git. – Ilya Sep 10 '15 at 09:51

2 Answers2

9

To answer the question (or better the title of the question). Just let the vNext build do it.

enter image description here

As mentioned in the comments, this is not available for external git repositories.

quadroid
  • 8,444
  • 6
  • 49
  • 82
  • Any idea how you could get a custom, in script defined variable in there, such as the output of GitVersion? – MytyMyky Dec 08 '15 at 16:14
  • Figured it out - you can update the buildNumber with logging commands, and then use the strategy above! https://github.com/Microsoft/vso-agent-tasks/issues/375 – MytyMyky Dec 08 '15 at 17:25
  • 2
    FYI, the "Label sources" option is not available when using an "External Git" repository. – Rami A. May 14 '16 at 11:31
  • 1
    Important caveat here: you cannot use *ANY* variables that were modified during the 'Build' portion of the .. build. The 'Finalize Build' steps are not run on the Build Agent, and therefore will not work. BuildNumber is a special case. see: http://stackoverflow.com/a/41028346/237723 – JoeBrockhaus Dec 08 '16 at 18:34
  • Unable to use this if you want to use a build variable created during the build, as not all of us want to tag our source with some meaningless build number. – Simon Gymer Dec 03 '19 at 09:27
1

I'm answering myself on what I was trying to do (git tag was one of the few tasks I wanted to do). It's now possible to execute git commands in scripts. The documentation is here : https://www.visualstudio.com/en-us/docs/build/scripts/git-commands

Here is the howto:

Enable scripts to run Git commands

Grant version control permissions to the build service:

  • Go to the Version Control control panel tab ▼
  • On the Version Control tab, select the repository in which you want to run Git commands, and then select Project Collection Build Service.
  • 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 to true

On the options tab select Allow scripts to access OAuth token.

  • On `git push origin *ref*` I get `fatal: could not read Username for *URL*: Invalid argument`, any clue what I can do? Do I need to add credentials to the TFS server (on premis)? – Love Dec 21 '16 at 16:24