Automatic Sitecore Publish with Windows Scheduled Tasks

We know that allowing your editors to publish is not a good idea. Let's set up an incremental publish at 3am every day by creating a windows scheduled task and using Sitecore.Ship.

Sitecore.Ship is a wonderful tool that allows the manipulation of a Sitecore installation through a service. It permits two main operations:

  • install a package
  • publish

installing Sitecore.Ship

The best way of installing Sitecore.Ship is through a Nuget package. You have two options, get the version available from nuget.org (at the time of writing 0.3.5) or be an adventurer and build your own from the latest develop branch in git. Let's do the latter.

We start by cloning the repo

    git clone https://github.com/kevinobee/Sitecore.Ship.git Sitecore.Ship

we need to add references for the Sitecore binaries in the build.proj file. We will open it

    cd Sitecore.Ship\
    notepad build\build.proj

and change the LibsSrcPath property1

<LibsSrcPath Condition="$(LibsSrcPath) == ''">C:\SitecoreVault\Binaries\v.8.0rev150812</LibsSrcPath>

Now we can build

 .\build.cmd

It should have generated the package in the artifacts\Packages folder

    cd artifacts\Packages
    ls

    Directory: C:\Projects\Sitecore.Ship\artifacts\Packages

    Mode                LastWriteTime         Length Name                                                                  
    ----                -------------         ------ ----                                                                  
    -a----       21/10/2015     09:18          50072 Sitecore.Ship.0.4.0.nupkg                                             
    -a----       21/10/2015     09:18          51186 Sitecore.Ship.AspNet.0.4.0.nupkg                                      

Let's copy this path as we will need it in a second

    $pwd.Path | clip

Next we open the solution for our Sitecore website. Using the Package Manager Console we install the nuget package using as source the path we had in the clipboard

    Install-Package Sitecore.Ship -source c:\projects\Sitecore.Ship\artifacts\Packages

Deploy your solution onto a Sitecore installation and we can use Postman to try the new service.

Image of postman

We can choose which publish we want to invoke according to the endpoint url

/services/publish/incremental
/services/publish/smart
/services/publish/full

Alternatively we can supply a JSON object to only publish certain items. See the readme for more details.

By default Sitecore.Ship will only work from the localhost computer which is aboslutely fine for our purposes. You can configure it to accept remote connections, and limit which IPs are allowed to use the service.

creating the scheduled task

Our last step is to configure a scheduled task to invoke this service. This is quite easy and we only need a few powershell commands

    $action = New-ScheduledTaskAction -Execute powershell -Argument "-noprofile -command {Invoke-WebRequest basicsitecore/services/publish/incremental -method post}"
    $trigger = New-ScheduledTaskTrigger -Daily -At 3am
    Register-ScheduledTask SitecorePublish -action $action -trigger $trigger

One last thing, this will momentarily open a powershell window when the task executes. I don't think this matters in a server but if it bothers you do not waste your time with the -windowStyle switch in powershell as it does not work. You can instead use a VBScript as explained in this post

  1. I always keep in my computer a copy of all the Sitecore versions as they are released. In the SitecoreVault\Installs I put the zip as downloaded from dev.sitecore.net. In SitecoreVault\Binaries I create a folder for each new version and copy all the DLLs for easy referencing in any project.