Mobile First Cloud First

A blog by Geert van der Cruijsen on Apps, Cloud & ALM

Category: Continuous Delivery

Setting up Continuous delivery for Azure API management with VSTS

Where Continuous delivery for web applications on Azure is becoming quite popular and common  I couldn’t find anything about settting this up for your API definitions in Azure API management. Since i had to set this up at my current customer I thought it was a good idea to share this in a blogpost so everyone can enjoy it. In this blogpost i’ll explain how you can set up Continuous delivery of your API definitions in Azure API management including the actual API implementation in Azure Web apps using VSTS (Visual Studio Team Services)

azure api management

First let me explain the architecture we use for our API landscape. As explained we use Azure API management for exposing the APIs to the outside world and we use Azure Web Apps for hosting the API implementation. These Web apps (Both .Net Core and Full framework .Net Web APIs) are hosted in an ASE (App Service Environment) so they are not exposed directly to the internet while we can still use all the cool things Azure Web Apps offer. These API web apps then connect to datastores hosted in Azure or connect to the on premise hosted environments through an express route or VPN.

To be able to set up our Continuous Delivery pipeline we have to arrange the following things.

  • Build your API implementation so we have a releasable package
  • Create infrastructure to host the API implementation
  • Deploy the API implementation package to the newly created infrastructure
  • Add API definition to Azure API management.
  • Repeat above steps for each environment. (DTAP)

Building your App

The first step can be different from my example if you’re not building your APIs using .Net technology. In our landscape we have a mix of APIs made with .Net Core and APIs made with .Net Full Framework because they needed libraries that were not available in .Net Core (yet). I’m not going into details on how to build your API using VSTS because i’ll assume  you’re already doing this or you know how to do this. If not here is a link to the official documentation.

One thing to keep in mind is that your API  web app does have to expose a API definition so Azure API Management can import this. We use Swashbuckle for this to automatically generate a swagger definition. If you’re using .Net Core you’ll have to use Swashbuckle.AspNetCore

Deploying the API implementation & adding it to Azure API management

For automating the deployments we’re going to the use Release Management feature of VSTS. In our first environment we’ll create steps to do all the things we described above.

 Screen Shot 2017-07-21 at 13.20.30

 The steps in our workflow are the following:

  1. Create web application infrastructure by rolling out an ARM template
  2. Set environment specific variables
  3. deploy the API implementation package
  4. Use a task group to add the API definition to Azure API management.

Creating the web app infrastructure & deploying the API Implementation package

the first and third steps are the basic steps of deploying a web application to Azure web apps. This is no different for APIs so i’ll just link to an existing blogpost here that explains these if you don’t know what they do.

Setting environment specific variables

the second task is a custom task created by my colleague Pascal Naber. It can help you overwrite specific variables you want to use in your environments by storing these settings as App Settings on your Azure web app. We use this to set the connection strings to backend systems for example a Redis Cache or a database.

Add API to API Management

So if we release the first 3 steps we would have an API that would on it’s own. But the main reason of this blogpost was that we want to have our API exposed through Azure API management so let’s have a look on how we can do that.

Azure API management has Powershell commands to interact with it and we can use this to add API definitions to Azure API management too. Below is a sample piece of Powershell that can import such an API definition from a Swagger file.

The script is built up out of 3 parts: first we retrieve the API management context by using the New-AzureRmApiManagementContext Commandlet. When we’ve gotten a context we can use this to interact with our API management instance. The second part is retrieving the swagger file from our running Web app through wget which is short for doing a GET web request. We’ll download the swagger file to a temporary disk location because in our case our web apps are running in an ASE  and therefore are not accessible through the Internet. if your web apps are connected to the internet you can also directly use the URL in the 3rd command to import the Swagger file into Azure API Management. Import-AzureRmApiManagementApi.

So now we have a script that we can use to import the API let’s add it to the VSTS release pipeline we could just add the powershell script to our source control and call the powershell using the build in powershell task. I’d like to make the developers’ life in our dev teams as easy as possible so i’m tried to abstract all Powershell mumbo jumbo away from them so they can focus on their APIs. To do this i’ve created a “Task Group” in VSTS containing this Powershell task so developers can just pick the “Add API to API Management Task” from the list in VSTS and supply the necessary parameters.

Screen Shot 2017-07-21 at 13.22.00

Screen Shot 2017-07-21 at 13.23.46

When we add this task group to the release we can run our release again and the API should be added to Azure API Management.

 Screen Shot 2017-07-21 at 13.20.30

Success!! Our initial continuous delivery process is fixed. At my current client we have 4 different API management instances and we also deploy our APIs 4x. A Development, Test, Acceptance and Production instance. The workflow we created deploys the API to our development environment. We’ve set this up to be continuous so every time a build completes on the master branch we create a new release that will deploy a new API instance to Azure and will update our Development Azure API management instance.

We can now clone this environment 3x so we create a pipeline that will move from dev, test to acceptance and production. I always set the trigger to automatically after the previous environment is completed. if we run our release again we’ll have 4 API instances deployed and in all 4 Azure API management instances they corresponding API will be imported.

Now the only thing you have to add is optionally adding integration tests to the environment you prefer and you are ready to roll!

Screen Shot 2017-07-21 at 13.24.10

 

Happy Coding!

Geert van der Cruijsen

Created an open source VSTS build & release task for Azure Web App Virtual File System

I’ve created a new VSTS Build & Release task to help you interact with the (VFS) Virtual File System API (Part of KUDU API of your Azure Web App). Currently this task can only be used to delete specific files or directories from the web app during your build or release workflow. It will be updated in the near future to also be able to list files or to upload / download files through the VFS API.

banner

The reason i made this task was that i needed it at my current customer. We’re deploying our custom solution to a Sitecore website running on Azure web apps using MSDeploy. The deployment consists of 2 parts: an install of the out-of-the-box Sitecore installation and the deployment of our customisations. When deploying new versions we want to keep the Sitecore installation and MSDeploy will update most of our customisations. Some customisations however create artifacts that stay on the server and aren’t  in control of the MSDeploy package that can cause errors on our web application. This new VSTS Build / Release task can help you delete these files. In the future this task will be updated with other functionality of the VFS API such as listing, uploading or downloading files.

The task is available in the VSTS Marketplace and is open source on github.

Let’s have a look how to use this task and how it works under the hood.
Continue reading

Building, testing and deploying precompiled Azure Functions

Azure functions are great to build small specialized services really fast. When you create an Azure Functions project by using the built-in template from the SDK in Visual Studio you’ll automatically get a function made in a CSX file. This looks like plain old C# but in fact it is actually  is C# Script. When you’re deploying these files to Azure you don’t have to compile them locally or on a build server but you can just upload them to your Azure Storage directly.

In the last update for Azure Functions the option to build precompiled functions was added. Doing this is actually pretty simple. I’ve created a sample project on Github containing a precompiled Azure function, unit tests for the function and an ARM template to deploy the function. Lets go over the steps to create a precompiled Azure function.

Continue reading

It’s 2017: Test automation is not optional when building mobile apps!

Note: although this post focusses on mobile app development using Xamarin it also applies to other native mobile apps built in Swift, Java or even web apps. it’s 2017! whatever you are building get started with Test Automation!

As a consultant working for Xpirit i get to see a lot of different customers which I help with my expertise in building mobile applications to improve their mobile apps. Something I noticed in the previous year is that continuous delivery is a hot topic and companies and teams focus on deploying apps automatically to their testers through hockeyapp or even to the stores in beta and / or production.

In agile scenario’s (and come on who isn’t doing that currently? Every company or project I visit is saying they are agile or doing Scrum although some only do dailies and call that scrum 😉 ) In the current world it is really important to be able to release often because you want to be able to adapt to customer needs which are almost always changing and evolving.

Implementing a Shift left Quality Model

Test Automation is a process that does not belong to the developers or testers alone. It’s something that has to be in everyone’s mind from Product Owner to Developer and Tester. Automated tests can help you lower regression test effort but investing in Test Automation can really help you make a shift left focussing on quality earlier in your application development process.

Continue reading

Created an open source VSTS build & release task for Sitecore.Ship

At my current customer we’re implementing an Azure environment which also contains a Sitecore application. We’re using VSTS to for all aspects of the application development lifecycle from agile planning to source control and automated builds & releases. In the release pipeline we can use the out of the box Azure web deploy build steps to deploy our code to our web app which is a Sitecore instance. The next step is to be able to also deploy the Sitecore content we created as code using TDS to our Sitecore Site.

Sitecore.Ship is an open source project which makes it easy to deploy Sitecore .Update files to your Sitecore Instance. It is created by Kevin Obee and it can be found here on Github

in our Build we build our TDS projects using MSBuild to generate .Update files which we can then deploy in our release pipeline to Sitecore. There was one problem however which was that there was no build task to do this. Since my role at my current client is supporting all the development teams in improving their continuous delivery process i decided to create a task to make their life easier.

sitecoreship
Continue reading

Improved Hockeyapp publishing from VSTS for Windows 10 UWP apps

In my last blogposts that  showed a tutorial on how to do automated builds in VSTS and continuous deployments to Hockeyapp. For UWP the support wasn’t that good and we couldn’t use the Hockeyapp Build steps from VSTS because Hockeyapp could not handle zip files containing the files from your app package such as the appx or appxbundle file, the powershell install script etc. I made a workaround using powershell but that is not needed anymore because the Hockeyapp team made some changes to the Hockeyapp build step.

This has changed last week although it wasn’t announced anywhere. I asked the question on the Hockeyapp slack group when the Hockeyapp team would implement this feature and they told me they just did it before the Microsoft Build conference started.

So if you’ve used my Powershell script before to publish your app to Hockeyapp you can now change it back to the  Hockeyapp build step and leave the rest of the steps the same. the zip file should contain all your files from the AppPackages folder. I’ve also updated the tutorial post for anyone who’s using it in the future to use the correct way right away.

image_thumb62.png

Hockey app UWP deployment

If there is an .appxsym file in the zip file as well the symbols will also be used within Hockeyapp.

I’m really glad that UWP apps are on the same level of maturity again as Android and iOS apps are regarding Continuous deployments in Hockeyapp.

The Hockeyapp team also announced in the public Hockeyapp Slack group that they are working with the Windows product team to improve installation of apps so more to come in the future. I can’t wait!

Happy Coding!

Geert van der Cruijsen

Continuous deployment of Xamarin.iOS apps to Hockeyapp using VSTS

Last week I’ve created 2 posts on setting up VSTS and Hockeyapp in a continuous deployment scenario for both Xamarin.Android and Windows 10 UWP apps. Today we’ll discuss the 3rd platform: Apps built using Xamarin.iOS

The basics of all 3 platforms are the same but there are still quite some differences so lets look at the steps for Xamarin.iOS apps:

image

Context: the app we are going to deploy

We’re going to deploy the same app as yesterday in the Xamarin Android post. I’ve created a simple solution in Visual Studio containing an average Xamarin project. 1 PCL, 1 Android app, 1 iOS app and 1 Windows 10 UWP app.

image_thumb1_thumb

To make things easier during automated build I’ve also created a new solution that only contains the projects relevant for the iOS app.

image-1

This solution only contains the PCL project and the Xamarin.iOS app project.

Setting up the build in VSTS: Prerequisites

 

There are some difficulties comparing the build of iOS apps to Android or Windows apps. first difficulty is that you are going to need a Mac to do the actual build steps. to do this you have 2 options. The first is to just use a Mac within your network and use that as a build agent, the other option is to use a service called MacInCloud.com which has a special plan for VSTS build agents.

Mac Build agent

Mac in cloud has a special VSTS build agent plan which only costs 30$ a month. in my experience this works really well. If you have a spare Mac somewhere to use as build agent this would also work fine but most clients I come don’t have that Winking smile

Setting up a VSTS build agent on a mac isn’t that hard. There is a good guide on this on github here: https://github.com/Microsoft/vso-agent/blob/master/docs/vsts.md

make sure Xamarin Studio is installed on the mac because it’s needed for doing the actual builds. (not in the VSTS build agent guide)

App Certificates

To be able to build the iOS app and to do ad-hoc distribution we’re going to need to set up certificates from the apple developer portal on our mac build agent. Xamarin has a great guide on how to do this so I won’t copy all the steps in this blogpost: https://developer.xamarin.com/guides/ios/deployment,_testing,_and_metrics/app_distribution/ad-hoc-distribution/

After we’ve arranged a mac build agent and set up the app certificates we can actually start building our app.

Setting up the build in VSTS

in VSTS open your team project and go to the BUILD tab. in here we’re going to create a new build definition by clicking the green + sign.

image_thumb3_thumb

Choose the Xamarin.iOS build template to set up the build.

image

In the next step select the correct repository you want to use for your app and make sure you select the Mac build agent in the drop down box.

image
Click Create and now our build definition is created with 2 out of the box steps. The first step is doing the actual Xamarin.iOS build. select the iOS specific solution so only the iOS related projects are build.

We’ll be removing the Xamarin test cloud step for now. if you want to know more about this let me know in a comment so I can create a new blogpost about this if people would like that.

image

so we only have the Xamarion.iOS step left but this will only build our app. we’re also going to need 2 extra steps which are not part of the Xamarin.iOS step:

  • Nuget package restore
  • Copy and Publish Artifacts.

Nuget Package restore:

I’ve you’ve read my previous posts on Android and Windows UWP you would expect we’re going to use the out of the box “restore nuget package” build step. But that is not possible for iOS. Why not? these steps are implemented using Powershell which doesn’t run on your mac agent. so we’ll have to do it manually by executing a shell script. So we’re going to create a shell script task. First we need to create the actual script. The script is quite simple. download the nuget.exe file and execute the nuget restore command

 

 

Save the .sh file in your repository so we can add it to our build step. Click the green + and select “Shell Script”

image

Drag the .sh script to the top of the build so the nuget restore will be executed before the actual build step. Select your .sh file in the script path and as argumenets pass in the path to your iOS solution file so the script knows what packages to restore.

image

Copy and Publish Artifacts

So we’ve set up the nuget restore and the build. the last step of our build is to copy and publish the build artificats  so we can send these to Hockeyapp later.

Add a “Copy and Publish Build Artifacts” step to your build definition.

image

In the copy and publish step set the root to the correct folder and for contents we’re going to select the .ipa file. The Artifact name we’re naming “drop” of type server.

image

These 3 steps should together be able to do a successful build. Go to triggers to schedule your build nightly or set it to build every time someone checks in code.

Queue a build to see if everything works.

image

Setting up Hockeyapp

I assume you’ve already created an account at Hockeyapp otherwise just sign up at www.hockeyapp.net (It’s free for 2 apps or less) once you’ve logged in go and create your first app by pressing the New App Button

image_thumb38_thumb

Hockeyapp will ask you to upload a build. we’re not going to do that since we’re setting up automatic deployments. choose to add the app manually

image_thumb40_thumb

Choose  iOS as the platform and fill in your release type and title of the app.

image

Click Save and in the overview of the app copy and save the App ID

image_thumb-19

To be able to deploy from VSTS we need to set up an API token we can use in VSTS. If you already followed the Android or Windows UWP guide you might have already taken these steps so you can skip these steps and move to the chapter of deploying the app.

Click on your user icon in the top right and select API Tokens from the menu on the left.

Create a new API token and call it VSTS. Copy this API token. we’ll need it in VSTS

image_thumb46_thumb

Move back to VSTS and  open up the marketplace (top right next to your name) and click manage extensions. browse the marketplace and install the Hockeyapp extension

image_thumb49_thumb

After installing the extension go back to your VSTS team project and navigate to the settings window. in the settings menu go to Services and add a new service Endpoint of type “Hockeyapp”

Give the endpoint a proper name and copy in the API token you’ve generated earlier. now save the service endpoint.

image_thumb51_thumb

Now all the plumbing with Hockeyapp is done we can actually start deploying our app to hockeyapp

Deploying your automated build to Hockeyapp

Go back to your team project in VSTS and navigate to the Release tab

image_thumb53_thumb

Choose an Empty deployment template and press OK.

image_thumb56_thumb

First go to the Artifact tab and select the build we’ve created earlier as supplier of the artifacts we’re going to release to hockeyapp.

image

Go back to Environments, name your deployment template and add a new Task

image_thumb60

This list should now contain a Hockeyapp step since we installed that as our extension

image_thumb62

Configure the Hockeyapp step by selecting the Hockeyapp connection from the list. (the service connection you’ve created earlier should be listed here) Enter the APP ID you copied earlier and select the .ipa file that was generated by the build.

image

That’s all it takes to set up the release. for the final step we’re going to set the triggers in the “Triggers” tab of the release to be deployed continuously after each build.

image

Press the Green + To start a manual release towards Hockeyapp. Everything should work and the release should be created in Hockeyapp.

If you have any questions let me know via twitter @geertvdc or by commenting below.

Happy coding!

Geert van der  Cruijsen

Continuous deployment of Windows 10 UWP apps to Hockeyapp using VSTS

In yesterdays post I’ve shown you how to set up Continuous deployments of Xamarin Android apps to Hockeyapp. Today we’ll focus on Windows 10 UWP apps since this process is a bit more complex.

This post is part of a series of 3 blogposts:

The basic process of setting up the continuous deployments is the same for all platforms: first build the sources, create a release and send that to Hockeyapp. However building the UWP app and sending it to hockey app require a few steps that aren’t so obvious. so lets start deploying

windows10uwphockeyapp2

Context: the app we are going to deploy

We’re going to deploy the same app as yesterday in the Xamarin Android post. I’ve created a simple solution in Visual Studio containing an average Xamarin project. 1 PCL, 1 Android app, 1 iOS app and 1 Windows 10 UWP app.

image_thumb1

This solution is checked into my GIT repository stored in VSTS so lets move to there to start building the UWP app today. please note that this also works for regular UWP apps that do not have any Xamarin involvement. it’s actually easiest to create a new solution containing only your class libraries (can be PCL) and your UWP project leaving out the Android and iOS specific projects (if you have any)

image

Setting up the build in VSTS

in VSTS open your team project and go to the BUILD tab. in here we’re going to create a new build definition by clicking the green + sign.

image_thumb3

Choose the Universal Windows Platform build template and click next.

image

Select the  repository containing the solution and select the branch you want to deploy. you can even choose to build apps that are stored in different sources such as GitHub or remote Git repositories.

After everything is set click on Create

image_thumb7

A build definition will be created and several build steps are generated for you. We’ll go over them one by one.

but first we need to set some other things up. UWP apps can run on both Mobile devices and tablets or pc’s. this will need different builds focussing on either ARM or the x86 architecture. We’ll set up 1 build definition that will handle both.

To make sure the build runs for each platform we go into the options tab of our build definition and select “Multi-configuration”. set the multipliers to “BuildPlatform” so the build will run for each different build platform we select.

image

Next go to the “Variables” tab and set the “BuildPlatform” variable to both x86 as ARM

image

Back to the build steps. We’ll start with restoring all nuget packages. this step was automatically generated and we only need to set the correct solution. set this to the .sln file that only contains the UWP related projects.

image

Next step is building the projects in the Visual Studio build step that is already created for us. Set the solution to the solution containing the UWP projects and set the platform to the $(BuildPlatform) variable

image

If you’ve read the Xamarin Android guide as well you might expect what is the next step: publishing the artifacts. but here is when Windows 10 UWP apps start to be different from the Xamarin Android guide.

Hockeyapp does not have any option to upload .appxbundles that contain both ARM ad x86 sources. we need to split them up into 2 packages and then send them to Hockeyapp separately. for Windows 10 Mobile we can still upload an .appxbundle file but for Windows 10 tablet/desktop (x86) we need to send a .zip file containing the .appxbundle, the .cer file and the .ps1 file to install the app on the pc or tablet (installing is currently still manually running the .ps1 file for Windows 10. I hope and expect this will become better in the near future.

To be able to publish both the artifacts: .appxbundle as the zip file we need to remove the predefined step called “publish build artifacts” and replace it with another. We’ll also be removing the “Index sources & publish symbols” step.

image

Now it’s time to add 3 new steps. 2 powershell tasks (1 at the top of the build and 1 at the bottom) and after that a “copy and publish build artifacts”

image   image

Wait! Powershell? You would expect for Windows 10 UWP that everything would work out of the box. Well it isn’t currently so we have to do some parts manually.

The First powershell script we will add to the top will increase the version number of the Windows package to a unique number so Hockeyapp will understand new incoming versions. The version number always contains 4 digits:X.X.X.X where I would like to keep the first 3 digits up to the development team to clearly define their releases. the last digit we’re going to use for our release number. This version number is stored within an XML file in the project so we’ll open it, increase the number and save it so the build will use this new number. We’ll insert the BuildID as the last number since this is a unique number and an integer.

You’ll need to store the powershell code below in a .ps1 file and store it somewhere in your repository.

to make it easier I’ve already created all 3 powershell scripts you are going to need during this guide and stored them on Github. so you can also download them from there so you always have the newest version. Check them in to your own repository so you can use them in the powershell task.

UpdateAppVersion.ps1

In the powershell step select the UpdateAppVersion.ps1 and for arguments make sure you set 2 parameters. –projectFolder pointing at the directory where your AppxPackages will be created and the –buildId that points to $(Build.BuildId) image So now this powershell script will change to build number and the Visual studio Build step will take this number into account and will version your appxpackage folder. next step is to create a .zip file of Appxpackage directory. again with powershell. ZipAppxPackage.ps1Select the ZipAppxPackage.ps1 as the script filename and pass a parameter called –dir to the script that has $(Build.BinariesDirectory) as its value.

image

so now everything is build and zipped and we can send it to the drop folder. our last step of the build.

Set the copy root to the $(Build.BinariesDirectory)\ and set the contents to copy to both *.zip files as *arm*.appxBundle files. The artifact name and type are set to drop and Server.

image

This was the final step of our build. you can run the app and everything should work. on to the next step: deploying to Hockeyapp

image

Setting up Hockeyapp

I assume you’ve already created an account at Hockeyapp otherwise just sign up at www.hockeyapp.net (It’s free for 2 apps or less) once you’ve logged in go and create your first app by pressing the New App Button

image_thumb38

Hockeyapp will ask you to upload a build. we’re not going to do that since we’re setting up automatic deployments. choose to add the app manually

image_thumb40

Choose  Windows as a platform and fill in your release type and title of the app. Note that this is only the tablet/pc (x86) release of the app and not the ARM version.

image

Repeat the steps above for the Windows 10 mobile (ARM) version but then select Windows Phone as plataform

image

Copy the AppID of both apps and save them:

image

To be able to deploy from VSTS we need to set up an API token we can use in VSTS. If you already followed the Android guide you might have already taken these steps so you can skip these steps and move to the chapter of deploying the app.

Click on your user icon in the top right and select API Tokens from the menu on the left.

Create a new API token and call it VSTS. Copy this API token. we’ll need it in VSTS

image_thumb46

Move back to VSTS and  open up the marketplace (top right next to your name) and click manage extensions. browse the marketplace and install the Hockeyapp extension

image_thumb49

After installing the extension go back to your VSTS team project and navigate to the settings window. in the settings menu go to Services and add a new service Endpoint of type “Hockeyapp”

Give the endpoint a proper name and copy in the API token you’ve generated earlier. now save the service endpoint.

image_thumb51

Now all the plumbing with Hockeyapp is done we can actually start deploying our app to hockeyapp

Deploying your automated build to Hockeyapp

Go back to your team project in VSTS and navigate to the Release tab

image_thumb53

Choose an Empty deployment template and press OK.

image_thumb56

First go to the Artifact tab and select the build we’ve created earlier as supplier of the artifacts we’re going to release to hockeyapp.

image_thumb66

image

Go back to Environments, name your deployment template and add a new Task

image_thumb60

This list should now contain a Hockeyapp step since we installed that as our extension

image_thumb62

This hockeyapp step will only work for Windows 10 Mobile (phone) and not for the pc/tablet (x86) version because sending .zip files is not supported by this step. we’ll cover this again by a custom powershell script.

Update: Hockeyapp made some changes to the Hockeyapp step in our deployment script so it can be used for both Windows 10 Mobile and Windows 10 Desktop/Tablet (x86/x64)

Windows 10 Mobile

To set up the hockeyapp step select the Hockeyapp connection you’ve set up earlier and enter your App ID. for the binary file path click on the … and select your ARM.appxbundle file

image

This step should do the trick. Schedule a release and you should be able to see a new version pop up in Hockeyapp.

Windows 10 (x86) Tablet/PC

For the Windows 10 (x86/x64) Tablet/PC version of the app we can now also use the hockey app build step so add another task of type Hockeyapp but let this one point to the other App ID for the Windows 10 desktop/tablet app and as Binary File path select the Zip file we created during the build instead of the appxbundle. Hockeyapp will see if there is already a version created and otherwise it will create a new version and upload it.

Now save the release definition and test it. press the green + to create a new release to see if everything works. if everything works fine go to triggers and set the build to continuous deployments so it will fire every time a new build is created.

image

Hopefully this guide helps you into creating higher quality apps and will save you time by not having to manually create releases all the time.

If you have any questions contact me on twitter @geertvdc or leave a comment below.

Happy Coding & deploying!

Geert

Continuous deployment of Xamarin Android apps to Hockeyapp using VSTS

Continuous integration and continuous deployments are a hot topic in the web world and are becoming a common practice there.  In the mobile world however this is not the case “yet”.

Microsoft seems to have everything in place now for a full Mobile ALM suite. Building your cross platform apps with Xamarin (which they just acquired yesterday), storing your code and builds in VSTS (Visual Studio Team Services) and deploying your app to Hockeyapp. Usage and bug tracking can be done by either Xamarin insights, Hockeyapp or Microsoft Application insights (although the last one is deprecated). In the end I believe these 3 applications will merge into 1. So how do we set up these continuous deployments of our apps to hockeyapp? I’ll be describing this in a series of 3 blogposts (1 for each platform).

There are quite some differences in deploying the apps for the 3 platforms to Hockeyapp so that’s why I’ve decided to split them up into separate blog posts.

We’ll start with Android because this is the easiest and most straight forward. (who would have expected that?)

 

image

Context: the app we are going to deploy

I’ve created a simple solution in Visual Studio containing an average Xamarin project. 1 PCL, 1 Android app, 1 iOS app and 1 Windows 10 UWP app.

image

This solution is checked into my GIT repository stored in VSTS so lets move to there to start building the Android

Setting up the build in VSTS

in VSTS open your team project and go to the BUILD tab. in here we’re going to create a new build definition by clicking the green + sign.

image_thumb3

Select the Xamarin.Android template which is already created by Microsoft for you.

image_thumb-2 (1)

Select the  repository containing the solution and select the branch you want to deploy. you can even choose to build apps that are stored in different sources such as GitHub or remote Git repositories.

After everything is set click on Create

image_thumb7

A build definition will be created and several build steps are generated for you. We’ll go over them one by one.

image_thumb-4 (1)

The first step is setting up your Xamarin license steps that will activate and deactivate your Xamarin license which is needed to execute the build of your Xamarin.Android project.

In this step you have to enter your email and password. Please note that the password field is a plain textbox and no password box. to fix this go to the “Variables” tab and create a variable with your password while you check the lock icon on the right. now you can store your password without other people being able to see it.

image_thumb-5 (1)

After you’ve created the Variable enter your email address and password variable into the 2 Xamarin License Steps (1 for activation and 1 for deactivation)

image_thumb-4 (1)

Please note that the deactivation step is set  to always run (even if the build crashes half way)

Next we’ll be removing the Xamarin Test cloud build step in our build. Xamarin test cloud is a very nice tool to test your app on real devices but for this blogpost it’s a bit off topic. if you want to know more about how this works let me know and I’ll write another blog post about it.

I’ll also be deleting the MSBuild step that would build the unit test projects since my sample project did not have any unit tests.

image_thumb-7 (1)

Now the Xamarin license is set up we will start building the actual Android app. to do this we first need to build the PCL project. Add 2 extra build steps to our build definition by pressing the Add Build Step button

image_thumb-8 (1)

First Add a Nuget Installer step.

image_thumb-9 (1)

after that add a Visual Studio Build step.

image_thumb-10 (1)

Drag both steps to the top so the Nuget Installer is the first step and the Visual Studio Build step is the second.

The nuget installer step will be configured so it will install and update all nuget packages that are part of our solution. select your solution file in the “Path to Solution” field.

image_thumb-11 (1)

This is all we have to set up for the nuget packages. If you don’t have any nuget packages (is that possible these days?) you can remove this build step.

Next we’re going to set up the build of the PCL project. select the Visual Studio Build Step and select all the csproj file(s) that are part of your project.

Set the build platform to AnyCPU and make sure the configuration takes the $(BuildConfiguration) variable.

image_thumb-12 (1)

So now the PCL project builds lets move to the actual Android App build step.

Select your Android csproj file and make sure that the output directory is set to $(build.binariesdirectory)\$(BuildConfiguration) and the Configuration is set to $(BuildConfiguration)

image_thumb-13 (1)

Now all projects are being built and the only thing we need to do is publish the build artifacts so we can deploy them to Hockeyapp.

Set the Path to publish to $(build.binariesdirectory)\$(BuildConfiguration) and set the Artifact name to drop and the artifact type to Server.

image_thumb-14 (1)

Save your build and give it a test spin by clicking “Queue Build” you can also set the build to run automatically by adding a trigger in the Triggers tab.

image

if all is correct your build should now pass. on to the next step deploying a release to Hockeyapp!

Setting up Hockeyapp

I assume you’ve already created an account at Hockeyapp otherwise just sign up at www.hockeyapp.net (It’s free for 2 apps or less) once you’ve logged in go and create your first app by pressing the New App Button

image

Hockeyapp will ask you to upload a build. we’re not going to do that since we’re setting up automatic deployments. choose to add the app manually

image

Choose Android as your platform and select Alpha or Beta for release type. (whatever fits your app builds best). Give the app a title and fill in the Package Name and click save.

image

Now the app is created and it will show up on your dashboard. click on your app and copy your App ID. we’ll need it later.

image

To be able to deploy from VSTS we need to set up an API token we can use in VSTS. Click on your user icon in the top right and select API Tokens from the menu on the left.

Create a new API token and call it VSTS. Copy this API token. we’ll need it in VSTS

image

Move back to VSTS and  open up the marketplace (top right next to your name) and click manage extensions. browse the marketplace and install the Hockeyapp extension

image

After installing the extension go back to your VSTS team project and navigate to the settings window. in the settings menu go to Services and add a new service Endpoint of type “Hockeyapp”

Give the endpoint a proper name and copy in the API token you’ve generated earlier. now save the service endpoint.

image

Now all the plumbing with Hockeyapp is done we can actually start deploying our app to hockeyapp

Deploying your automated build to Hockeyapp

Go back to your team project in VSTS and navigate to the Release tab

image

Choose an Empty deployment template and press OK.

image

First go to the Artifact tab and select the build we’ve created earlier as supplier of the artifacts we’re going to release to hockeyapp.

image

image

Go back to Environments, name your deployment template and add a new Task

image

This list should now contain a Hockeyapp step since we installed that as our extension

image

Configure the Hockeyapp step by selecting the Hockeyapp connection from the list. (the service connection you’ve created earlier should be listed here) Enter the APP ID you copied earlier and select the .apk file that was generated by the build.

image

The last step is setting the trigger to automatically start your hockeyapp deployment after each successful build.  go to triggers, select Continuous deployment and make sure that the environment trigger is set to “Automated: after release creation.”

image

Save the deployment template and now you are ready to go. you can now manually create a new release using the deployment template or start a new build and you should see your app show up in Hockeyapp so your testers can download the app.

If you have any questions let me know via twitter @geertvdc or by commenting below.

Hopefully you’ll be back tomorrow for the next post explaining all the steps for Windows 10 UWP apps.

Geert van der  Cruijsen