June 23, 2014

Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Step 3: Configure octopus

This is the 4th post of a series of post about the setup of a continuous integration for Sitecore.  If you have miss the 3 first post here are the links:
  1. Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Architecture
  2. Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Step 1: Customize the TFS workflow
  3. Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Step 2: Configure TFS
Now our TFS is configured correctly, the package are created and published to the nuget server. We can focus on octopus now.

I will not explain how to setup you Octopus environments, machines, ... but only the deployment process I use. 

Here is the steps:
  1. Start Deployment: A notification email to the different member of the project with the details of the packages who will be deployed.
  2. Clean Environment: A cleanup of the files before the deployment to be sure that if we have remove a file it will not be on the project anymore. This is a powershell script with the following content: 
    #RootFolder : the root folder of the project (we should be able to get it from the environment)  
    $rootFolder = "E:\Websites\XXX\Website"   
    
    function Remove-Item-If-Exist
    {
     param
     (
      [string]$pathToDelete,
      [switch]$force,
      [switch]$recurse
     )
     
     If (Test-Path $pathToDelete){
      Remove-Item $pathToDelete -Force:$force -Recurse:$recurse
     }
    } 
     
    #Cleanup the old files  
    Remove-Item-If-Exist "$rootFolder\Design" -Force -Recurse  
    Remove-Item-If-Exist "$rootFolder\layouts\Layouts" -Force -Recurse  
    Remove-Item-If-Exist "$rootFolder\layouts\Sublayouts" -Force -Recurse  
    Remove-Item-If-Exist "$rootFolder\layouts\UserControls" -Force -Recurse  
    Remove-Item-If-Exist "$rootFolder\bin\PROJECTNAMESPACE.*.dll" -Force   
    Remove-Item-If-Exist "$rootFolder\App_Config\Include\PROJECTNAMESPACE.*.config" -Force
  3. Deploy Files: Deploy the NuGet package. You may have multiple time this step if you have multiple nuget package with the website files to deploy.
  4. Deploy TDS: Deploy the NuGet package with the TDS files in a temporary folder. 
  5. Install TDS Packages: This is a powershell to install and publish the TDS packages. I have create a custom tool for it who scan the files in a specific folder and install the packages. If you need to do this kind of program you should take a look at the code of TDS. Basically it install a webservice into your deploy folder, use this webservice to install your packages and then remove this webservice.
    A great alternative could be this tool: https://github.com/adoprog/Sitecore-Deployment-Helpers
To automate the creation of the release into octopus when the build is triggered into TFS we can add a post release script into TFS to create and deploy this release.

To do that:

  1. Add a new file with the extention .ps1 somewhere in your sources and chech-in this file in TFS
  2. Edit your TFS build definition 
  3. In the tab "Process", section 2.5
  4. In the section "Post-build script path", select your .ps1 file
  5. The parameter "Post-build script arguments" I have "-releaseVersion 1.1.J.B" please refer to the post "Step 4: Align the versions numbers" for this.
  6. The content of the powershell file is the following:
    param
    (
     [string]$releaseVersion
    )
     
    #Constants
    $octopusApiUrl = "http://10.0.2.50:8088/api"
    $octopusApiKey = "API-RX6UIIWTB2ZBUWU0TRIYXXXXXXX"
    
    $projectName = "XXX"
    $scriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
     
    #Functions
    
    # Get the formated TFS version number. Replace the tokens J and B by the current build number
    function Format-TFS-Version
    {
     param
     (
      [string]$versionToFormat
     )
     
     $buildNumber = $env:TF_BUILD_BUILDNUMBER
     Write-Host "env:TF_BUILD_BUILDNUMBER: $buildNumber"
     
        if ($buildNumber -eq $null)
        {
            $buildIncrementalNumber = 0
        }
        else
        {
            $splitted = $buildNumber.Split('.')
            $buildIncrementalNumber = $splitted[$splitted.Length - 1]
        }
        
     $jdate = Get-Date -format 1MMdd
     
     return $versionToFormat.Replace("J", $jdate).Replace("B", $buildIncrementalNumber)
    }
    
    #Create a release in Octopus with the specifiic version of the nuget packages
    function Octopus-Create-Release
    {
     param
     (
      [string]$releaseVersion
     )
     
     $releaseVersion = Format-TFS-Version $releaseVersion
     
     $corePackage = "--package=XXX.Core:$releaseVersion"
     $corporatePackage = "--package=XXX.Corporate:$releaseVersion"
     $scriptPackage = "--package=XXX.Tools.SqlScriptsUpdate:$releaseVersion"
     
     Write-Host "Create the octopus release by calling $scriptRoot\Octopus\octo.exe create-release --project=$projectName --server=$octopusApiUrl --apiKey=$octopusApiKey --version $releaseVersion $corePackage $corporatePackage $scriptPackage"
     
     &$scriptRoot\Octopus\octo.exe create-release --project=$projectName --server=$octopusApiUrl --apiKey=$octopusApiKey --version $releaseVersion $corePackage $corporatePackage $scriptPackage  
    }
    
    #Deploy a release in Octopus
    function Octopus-Deploy-Release
    {
     param
     (
      [string]$releaseVersion,
      [string]$deployToEnvironment
     )
     
     Write-Host "Deploy the octopus release by calling $scriptRoot\Octopus\octo.exe  deploy-release --project=$projectName --version=$releaseVersion --deployto=$deployToEnvironment --server=$octopusApiUrl --apiKey=$octopusApiKey"
    
     &$scriptRoot\Octopus\octo.exe deploy-release --project=$projectName --version=$releaseVersion --deployto=$deployToEnvironment --server=$octopusApiUrl --apiKey=$octopusApiKey
    }
    
    
    
    #Main program
    $releaseVersion = Format-TFS-Version $releaseVersion
    Write-Host "Release version: $releaseVersion"
    
    Octopus-Create-Release $releaseVersion
    
    Octopus-Deploy-Release $releaseVersion Integration
    


Ok now your process should be completed. The last post is optional but allow you to have a single ID during the whole build process.

Next steps:
  1. Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Architecture
  2. Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Step 1: Customize the TFS workflow
  3. Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Step 2: Configure TFS
  4. Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Step 3: Configure octopus
  5. Setup a continuous integration for Sitecore with TDS - TFS 2013 and Octopus - Step 4: Align the version number

No comments:

Post a Comment