Run Git commands in a script

Last Update: 5/4/2017

Team Services | TFS 2017 | TFS 2015 | Previous versions: XAML Build, Release

For some workflows you need your build process to run Git commands. For example, after a CI build on a feature branch is done, the team might want to merge the branch to master.

Git.exe is available on hosted agents and on on-premises agents.

Enable scripts to run Git commands

Grant version control permissions to the build service

Go to the Version Control control panel tab ▼

  • Team Services: https://{your-account}.visualstudio.com/DefaultCollection/{your-team-project}/_admin/_versioncontrol
  • On-premises: https://{your-server}:8080/tfs/DefaultCollection/{your-team-project}/_admin/_versioncontrol

manage project

If you see this page, select the repo, and then click the link:

control panel top to team project

control panel team project version control tab

On the Version Control tab, select the repository in which you want to run Git commands, and then select Project Collection Build Service.

permissions

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:

Name Value
system.prefergit true

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

Make sure to clean up the local repo

Certain kinds of changes to the local repository are not automatically cleaned up by the build process. So make sure to:

  • Delete local branches you create.
  • Undo git config changes.

If you run into problems using an on-premises agent, to make sure the repo is clean:

Examples

List the files in your repo

Make sure to follow the above steps to enable Git.exe.

On the build tab add this step:


Utility: Command Line

List the files in the Git repo.

  • Tool: git
  • Arguments: ls-files

Merge a feature branch to master

You want a CI build to merge to master if the build succeeds.

Make sure to follow the above steps to enable Git.exe.

On the Triggers tab select Continuous integration (CI) and include the branches you want to build.

Create merge.bat at the root of your repo:

@echo off
ECHO SOURCE BRANCH IS %BUILD_SOURCEBRANCH%
IF %BUILD_SOURCEBRANCH% == refs/heads/master (
   ECHO Building master branch so no merge is needed.
   EXIT
)
SET sourceBranch=origin/%BUILD_SOURCEBRANCH:refs/heads/=%
ECHO GIT CHECKOUT MASTER
git checkout master
ECHO GIT STATUS
git status
ECHO GIT MERGE
git merge %sourceBranch% -m "Merge to master"
ECHO GIT STATUS
git status
ECHO GIT PUSH
git push origin
ECHO GIT STATUS
git status

On the build tab add this as the last step:


Utility: Batch Script

Run merge.bat.

  • Path: merge.bat

Q&A

Can I run Git commands if my remote repo is in GitHub or an external Git service such as Bitbucket?

Yes

Which steps can I use to run Git commands?

Batch Script

Command Line

PowerShell

Shell Script

How do I avoid triggering a CI build when the script pushes?

Add ***NO_CI*** to your commit message. For example, git merge origin/features/hello-world -m "Merge to master ***NO_CI***"

How does enabling scripts to run Git commands affect how the build process gets build sources?

When you set system.prefergit to true, the build process uses Git.exe instead of LibGit2Sharp to clone or fetch the source files.

Do I need an agent?

You need at least one agent to run your build or release. Get an agent.

I can't select a default agent queue and I can't queue my build or release. How do I fix this?

See queues.

I use Team Foundation Server on-premises and I don't see some of these features. Why not?

Some of these features are available only on Visual Studio Team Services and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.