Thursday, 7 December 2017

SharePoint Addin: VSTS CI/CD pipeline hosted agent challenge

In this post I will explain setting up a CI/CD pipeline using VSTS for SharePoint Addins without the need to install PnPPowerShell scripts on your Build/Release Agents.

By default the hosted VSTS Release agent doesn't include SharePoint online PowerShell cmdlets. The easy and straight forward option is to use your agent and install SharePoint PowerShell cmdlets on it. However, I want to have a more portable option that will allow me to use the hosted agent without maintaining a release VM.

PnP PowerShell cmdlets

Firstly, what is actually the underlying logic that PnP PowerShell cmdlets encapsulates. It's basically HTTP calls to SharePoint Online RESTful APIs. So in way we can replace the PowerShell Cmdlets with simple http requests.

Gulp to the rescue

By Default, VSTS hosted agent will have node and gulp installed so we don't need to worry about setting up VSTS hosted agent, we will build a gulp task that allow us to publish SharePoint Addin to our app catalog, the main steps will be:
  • Getting app principle
    In order to upload the app package to the app catalog we need to get app principle which will run in app-only mode check my post here to learn how to get client Id and client secret
  • Acquire access token
    Using sharepoint-apponly nodejs module we will be able to get the access token
  • Upload .app package to app catalog site.
  • First we will create a new file let's name it sharepoint.js, then let's import fs and http modules, we will create a single function uploadFile which will be exported to be used in our gulpfile.js 
    here is the sharepoint.js
    and the gulpfile.js

Putting it all together

  1. Let's create new directory , initiate new node module:
  2. Create SharePoint.js and gulpfile.js and paste our code there.
  3. Install needed dependencies which includes sharepoint-apponly module explained here
  4. Let's create new build definition which includes step to copy gulpfile.js /sharepoint.js and package.json to artefact directory
  5. In release definiton let's create two release steps. The first step will simply install npm dependencies 

  6. the other step will be gulp task which will run publish-app task defined in gulpfile.js , notice you can supply the parameters as argument which will be evaluated from release definition variables.

  7. the hosted agent can copy the app package to the appcatalogUrl which in my case defined in release variables.