Managing Resources Using Scripts

Last updated

Some resources can be created within Octopus from the same scripts that you use to create them on Azure. By adding some additional commands Web Apps you create on Azure can also be created within Octopus as deployment targets.

As of the Octopus 2018.9 release, only Azure Service Principal Accounts, Azure Web Apps, Azure Service Fabric, Azure Cloud Services and Kubernetes targets are supported.

Available Commands and Syntax

Each of the resource commands is available as a PowerShell function anywhere that a step allows you to run a PowerShell script.

Accounts

Azure Service Principal Account

Targets

Before you can create dynamic targets in an Environment, the environment needs to be configured to allow it. See Dynamic Targets in an Environment for more information.

Azure Web App

Azure Service Fabric

Azure Cloud Service

Remove Target

Restrictions

All of the commands will result in an Account or Deployment Target, which will automatically be scoped to the Environment and Tenant (if the deployment is for a tenant). This cannot be overridden through the commands.

These commands are not available in the Script Console.

Examples

Creating an Azure Web App

Let's say you need to deploy an application to a different Azure Resource Group each time you deploy, one per developer or tester for example. The Resource Group, Application Service Plan, and Web App can all be created through an Azure PowerShell script step, and then the Octopus Target can be created in the same step.

# Octopus variables
$environment = $OctopusParameters['Octopus.Environment.Name']
$releaseNumber = $OctopusParameters['Octopus.Release.Number'].Replace(".", "-")
$deploymentId = $OctopusParameters['Octopus.Deployment.Id']

# A unique name based on the Octopus environment, release, and deployment
$uniqueName = "Acme-$environment-$releaseNumber-$deploymentId"

# Create resources in Azure
New-AzureRmResourceGroup -Name $uniqueName -Location "WestUS"
New-AzureRmAppServicePlan -Name $uniqueName -Location "WestUS" -ResourceGroupName $uniqueName -Tier Free
New-AzureRmWebApp -Name $uniqueName -Location "WestUS" -AppServicePlan $uniqueName -ResourceGroupName $uniqueName

# Create new target in Octopus
New-OctopusAzureWebAppTarget -Name $uniqueName -AzureWebApp $uniqueName -AzureResourceGroupName $uniqueName -OctopusAccountIdOrName "my-octopus-azure-serviceprincipal-account" -OctopusRoles "acme-web"

Tearing Down a Test Environment

Building on the Web App example, you may wish to spin up an application and then tear it down at the end of the day. By combining Recurring Deployments and a tear-down script, you can keep your cloud hosting costs down.

Using as little as two lines of PowerShell you can remove all the resources from Azure and Octopus:

Remove-AzureRmResourceGroup -Name "AzureWebAppResourceGroup" -Force
Remove-OctopusTarget -targetIdOrName "AzureWebApp"

Deploying an ARM Template

You can also use the above PowerShell Cmdlets when deploying Azure resources using an ARM template.

Firstly, turn on Custom deployment scripts under Configure Features on your Deploy an Azure Resource Group step.

Configure features on ARM template step

Under the Features section you will have Pre-deployment, Deployment, and Post-deployment scripts.

In the Post-deployment script section, you can access the output paramaters and use those output parameters to run any of the Cmdlets above.

In This Section

The following topics are explained further in this section: