Overview

During deployment it is likely that you want to write Azure Powershell scripts to manipulate files in your builds before deployment or make API calls to Azure. To do this you create "Script Modules".

  • Click "Library" => click "Script Modules" => click "Add Script Module".
  • Name it something like "FL Deployment Scripts" where FL is your company initials.

Create your Script Module

Below is what we currently have in our Deployment scripts.

# Resource of Approved verbs: https://msdn.microsoft.com/en-us/library/windows/desktop/ms714428(v=vs.85).aspx

<# 
.DESCRIPTION
Switches an azure apps production slot with staging. 
- Assumes you named your slot "staging" in assure.

.PARAMETER $app 
a string name of your azure website.

#>
function Switch-StagingAndProduction($app){
    Switch-AzureWebsiteSlot -Name $app -Slot1 'production' -Slot2 'staging' -Force
}

# A Helper function to see what files are in the current path.
function Debug-ShowCurrentDirFiles{
    $cwd = Convert-Path .
    Write-Output $cwd
    $children = Get-ChildItem $cwd
    Write-Output $children    
}

<# 
.DESCRIPTION Rename a config file. 

.PARAMETER $sourceConfigName 
- the file name with the configs you want to use.
.PARAMETER $destinationConfigName 
- the file name you want to write the contents to.

#>
function Rename-Config($source, $destination){
    Write-Output "Environment: $($OctopusParameters['Octopus.Environment.Name']): Setting Source: $($source) to $($destination) ."

  If (Test-Path $destination){
      Remove-Item $destination
  }

  Rename-Item $source $destination

  Write-Output "Using Config Settings: "
  $file = Get-Content $destination
  Write-Output $file
}

<#

.DESCRIPTION
A helper function that uses variables to rename the .tmp config files generated during build to App.config
Environment Dependent
- We use this for the SchemaChange CLI
#>
function Rename-Migration-Config{
    Rename-Config $OctopusParameters['FL.MigrationConfigSubstitutionFile']  $OctopusParameters['FL.AppConfigName']
}

<#

.DESCRIPTION
A helper function that uses variables to rename the environment config files generated during build to Web.config
Environment Dependent
- We use this for the Azure Websites App
#>
function Rename-Web-Config{
    Rename-Config $OctopusParameters['FL.WebConfigSubstitutionName']  $OctopusParameters['FL.WebConfigName']
}

<#

.DESCRIPTION
A helper function that uses variables to rename the environment config files generated during build to App.config
Environment Dependent
- We use this for the CLI App

#>
function Rename-App-Config{
    Rename-Config $OctopusParameters['FL.AppConfigSubstitutionName']  $OctopusParameters['FL.AppConfigName']
}

<#

.DESCRIPTION
Used during deployment to invoke the DB migrations.

#>
function Invoke-SchemaChanges{
  Write-Output "Running Database Migrations scc.exe "
  .\scc.exe
  Write-Output "Done Running Deploy.ps1"
}

Add module to your Project.

After you have created this module, you need to add it to your project so that the methods are availabel to you in process step scripts.

Screenshot: Adding Script Modules to Project



blog comments powered by Disqus

Published

30 October 2015

Category

deployment