Using builds scripts in Visual Studio App Center

Corrado Cavalli
Corrado Cavalli
Published in
3 min readSep 1, 2018

How to use build scripts and how to authenticate git commands to VSTS

Microsoft App Center is a suite of online service to Build, Test, Distribute, Diagnose, Analyze and manage Push Notification of your app (Android, iOS, React Native, UWP, Xamarin, macOS and Cordova)

One of the things i like most of App Center is the Build service, with it you can select the repository where code to build resides,

after entering credentials, you have the list of all the repository branches and for any branch you can configure related build process in every detail.
So far, so easy, at least if all you have to do is to build a branch and nothing else.

Using build scripts

In case some extra stuff need to be executed before or after the build you can ‘inject’ some custom scripts that will be executed at proper time.
You can find all info regarding scripts at this link , the only note i want to add is that, for UWP application the PowerShell scripts files (.ps1) must be included into your project at solution level and not at project level as indicated into documentation

If you need to retrieve some environment info there are several App Center variables like APPCENTER_SOURCE_DIRECTORY or APPCENTER_BRANCH etc that are accessible using $env:APPCENTER_SOURCE_DIRECTORY inside PowerShell script (for bash scripts syntax is $APPCENTER_BRANCH)

Using authenticated git commands

This is where things get really complicated, in my case i wanted to clone a repository that main repository depends on before the build process starts, otherwise it couldn’t find some projects and Build would inevitably fail, so what i did was writing a script like this

echo **Custom_AppCenter_Script**
echo $env:APPCENTER_SOURCE_DIRECTORY
echo **Cloning...**
git -clone https://mydomain.visualstudio.com/AppCenterBuild/_git/AppCenterBuild $env:APPCENTER_SOURCE_DIRECTORY\..\MyDir
echo **Cloning completed.**

But script was hanging endlessly at git clone command.
After some internal discussion with the team the problem seemed to be that git -clone command was missing required credentials, and so the question was: how do i pass them since looks like that git way user:password@xxx.git doesn’t work?

If you want to know how follow this steps:

1- Login into your VSTS account
2- Under your profile, select Security and then Personal access tokens (PAT)
3- Add a new token, add a name, choose expiration date and select Create Token
4-Make a note of it!
5-Modify your git clone this way

git clone https://[whateveryouwant]:[PAT]@mydomain.visualstudio.com/AppCenterBuild/_git/AppCenterBuild $env:APPCENTER_SOURCE_DIRECTORY\..\MyDir

And now clone command will be authenticated by VSTS and properly executed by your script.

--

--

Corrado Cavalli
Corrado Cavalli

Senior Sofware Engineer at Microsoft, former Xamarin/Microsoft MVP mad about technology. MTB & Ski mountaineering addicted.