Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic publish of Sitecore content at end of TDS CI deploy

I'm using Teamcity to automate (single click) deploys into our QA environment. At the moment content items are being deployed, but the QA guys then have to go and manually trigger a re-publish of the site.

Is there anyway using either TDS, Sitecore Rocks or A.N.Other tool to automate the re-publish at the end of the deploy process.

I know I can configure Sitecore to automatically publish every x minutes, but I would rather leave that deactivated as QA will also be performing load tests and I don't want the scheduler getting in the way.

like image 757
andycwk Avatar asked Oct 15 '12 13:10

andycwk


Video Answer


2 Answers

We have done this by setting up an ASPX on our daily build and QA websites that triggers a publish. The build then has a Powershell call to trigger this. We've done this with CruiseControl, TeamCity, and Team Build.

TeamCity Configuration

The configuration for TeamCity uses an additional build step after you've deployed files and TDS:

  1. Runner Type: Powershell
  2. Step name: Trigger Publish to Web DB
  3. Powershell run mode: x64
  4. Working Directory: [no value]
  5. Script: Source code
  6. Script Source:

    $r = [System.Net.WebRequest]::Create('http://myqasite/SomePath/Publish.aspx'); $resp = $r.GetResponse();

  7. Script execution mode: Put script into Powershell stdin with "-Command -" arguments

Publish.aspx

The code for our Publish page is something like this:

    string full = Request.QueryString["full"];

    // Set up the publish mode
    PublishMode publishMode = PublishMode.Smart;
    if (!string.IsNullOrWhiteSpace(full) && (full == "1" || full.Equals("true", StringComparison.InvariantCultureIgnoreCase)) ) {
        publishMode = PublishMode.Full;
    }

    using (new Sitecore.SecurityModel.SecurityDisabler()) {
        //We need the target database
        var webDb = Sitecore.Configuration.Factory.GetDatabase("web");

        //source db
        var masterDb = Sitecore.Configuration.Factory.GetDatabase("master");

        try {
            foreach (Language language in masterDb.Languages) {
                //loops on the languages and do a full republish on the whole sitecore content tree
                var options = new PublishOptions(masterDb, webDb, publishMode, language, DateTime.Now)
                              {RootItem = masterDb.Items["/sitecore"], RepublishAll = true, Deep = true};
                        var myPublisher = new Publisher(options);
                        myPublisher.Publish();
            }
        }
        catch (Exception ex) {
            Sitecore.Diagnostics.Log.Error("Could not publish the master database to the web", ex);
        }

    }
like image 147
Jay S Avatar answered Sep 24 '22 07:09

Jay S


While waiting for an answer I shared the question on twitter, this resulted being given a guiding hand by Stephen Pope (his response) He suggested using the PowerShell enhancements offered by Sitecore Rocks, it took a while (documentation is thin on the ground) but I have achieved the result I was after :)

As a record of what I found, the following is provided as a potential answer to my own question, though big thanks to Jay S who's solution I would have used if not for this..

Anyway.. Using an additional build step in the Teamcity build, I have the following:

  • Runner type: Powershell
  • Step name: Publish site
  • Powershell run mode: x64
  • script: Source code
  • script execution mode: Put script into powershell stdin with "-Command -" arguments
  • Additional command line parameters: -ExecutionPolicy Unrestricted
  • Script source:

    import-module '.\build-modules\sitecore\sitecore.psd1';
    new-psdrive -name "rocks" -psp SitecoreRocks -root "" -host "%QA.Url%" -usr "%QA.sitecore.user%" -pwd "%QA.sitecore.password%" -databasename "master" -scope "Script";
    set-location rocks:
    Publish-SCDatabase;

The magic happens within the Publish-SCDatabase commandlet which when you run Get-Help shows a bunch of parameters, it turns out that only two of the parameters are usable -Name and -Mode

Finding documentation beyond the vs-plugins link above was impossible, so a bit of .net reflection and a good dose of patiense showes that the parameters have the following options:

  • -Name The name of the database to run the publish against (default is the psdrive database
  • -Mode Sets the publish mode of the comandlet with the following options available:
    • 0: Republish
    • 1: Incremental
    • 2: Smart
    • 3: Rebuild

Of course better documentation, and possibly additional info via the Get-Help commandlet would be nice.. if the rocks project was open source, I may well have forked the project to generate the additional help.

Now that there are two very good solutions to this question, I will let peoples votes decide on which is the best answer, in a few days I will check the votes and mark the most voted as the accepted answer.

like image 21
andycwk Avatar answered Sep 20 '22 07:09

andycwk