Azure - Create Container App Environment

Octopus.Script exported 2023-07-05 by twerthi belongs to ‘Azure’ category.

Creates a Container App Environment if it doesn’t exist. An output variable called ManagedEnvironmentId is created which holds the Id.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Azure Resource Group Name

Template.Azure.ResourceGroup.Name =

Provide the resource group name to create the environment in.

Azure Account Subscription Id

Template.Azure.Account.SubscriptionId =

The subscription ID of the Azure account to use. This value can be retrieved from an Azure Account variable type. Add an Azure Account to your project , then assign the SubscriptionNumber property to for this entry. Leave blank to use the Managed Identity.

For example, if your Azure Account variable is called MyAccount, the value for this input would be #{MyAccount.SubscriptionNumber}

Azure Account Client Id

Template.Azure.Account.ClientId =

The subscription ID of the Azure account to use. This value can be retrieved from an Azure Account variable type. Add an Azure Account to your project , then assign the Client property to for this entry. Leave blank to use the Managed Identity.

For example, if your Azure Account variable is called MyAccount, the value for this input would be #{MyAccount.Client}

Azure Account Tenant Id

Template.Azure.Account.TenantId =

The subscription ID of the Azure account to use. This value can be retrieved from an Azure Account variable type. Add an Azure Account to your project , then assign the TenantId property to for this entry. If blank, it will use the Managed Identity tenant.

For example, if your Azure Account variable is called MyAccount, the value for this input would be #{MyAccount.TenantId}

Azure Account Password

Template.Azure.Account.Password =

The subscription ID of the Azure account to use. This value can be retrieved from an Azure Account variable type. Add an Azure Account to your project , then assign the Password property to for this entry. Leave blank to use the Managed Identity.

For example, if your Azure Account variable is called MyAccount, the value for this input would be #{MyAccount.Password}

Container App Environment Name

Template.ContainerApp.Environment.Name =

The name of the container app environment to create.

Azure Location

Template.Azure.Location.Name =

The location in which to create the container app environment.

Script body

Steps based on this template will execute the following PowerShell script.

# Define functions
function Get-ModuleInstalled
{
    # Define parameters
    param(
        $PowerShellModuleName
    )

    # Check to see if the module is installed
    if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName))
    {
        # It is installed
        return $true
    }
    else
    {
        # Module not installed
        return $false
    }
}

function Get-NugetPackageProviderNotInstalled
{
	# See if the nuget package provider has been installed
    return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))
}

function Install-PowerShellModule
{
    # Define parameters
    param(
        $PowerShellModuleName,
        $LocalModulesPath
    )

	# Check to see if the package provider has been installed
    if ((Get-NugetPackageProviderNotInstalled) -ne $false)
    {
    	# Display that we need the nuget package provider
        Write-Host "Nuget package provider not found, installing ..."
        
        # Install Nuget package provider
        Install-PackageProvider -Name Nuget -Force
    }

	# Save the module in the temporary location
    Write-Host "Saving module $PowerShellModuleName to temporary folder ..."
    Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force
    Write-Host "Save successful!"
}

# Check to see if $IsWindows is available
# Check to see if $IsWindows is available
if ($null -eq $IsWindows)
{
     Write-Host "Determining Operating System..."
    $IsWindows = ([System.Environment]::OSVersion.Platform -eq "Win32NT")
    $IsLinux = ([System.Environment]::OSVersion.Platform -eq "Unix")
}

# Check to see if it's running on Windows
if ($IsWindows)
{
	# Disable the progress bar so downloading files via Invoke-WebRequest are faster
    $ProgressPreference = 'SilentlyContinue'
}

if ($PSEdition -eq "Core") {
    $PSStyle.OutputRendering = "PlainText"
}

# Define PowerShell Modules path
$LocalModules = (New-Item "$PWD/Modules" -ItemType Directory -Force).FullName
$env:PSModulePath = "$LocalModules$([IO.Path]::PathSeparator)$env:PSModulePath"
$azureModule = "Az.App"

# Get variables
$templateAzureAccountClient = $OctopusParameters['Template.Azure.Account.ClientId']
$templateAzureAccountPassword = $OctopusParameters['Template.Azure.Account.Password']
$templateAzureAccountTenantId = $OctopusParameters['Template.Azure.Account.TenantId']
$templateAzureResourceGroup = $OctopusParameters['Template.Azure.ResourceGroup.Name']
$templateAzureSubscriptionId = $OctopusParameters['Template.Azure.Account.SubscriptionId']
$templateEnvironmentName = $OctopusParameters['Template.ContainerApp.Environment.Name']
$templateAzureLocation = $OctopusParameters['Template.Azure.Location.Name']

# Check for required PowerShell module
Write-Host "Checking for module $azureModule ..."

if ((Get-ModuleInstalled -PowerShellModuleName $azureModule) -eq $false)
{
	# Install the module
    Install-PowerShellModule -PowerShellModuleName $azureModule -LocalModulesPath $LocalModules
}

# Import the necessary module
Write-Host "Importing module $azureModule ..."
Import-Module $azureModule

# Check to see if the account was specified
if (![string]::IsNullOrWhitespace($templateAzureAccountClient))
{
	# Login using the provided account
    Write-Host "Logging in as specified account ..."
    
	# Create credential object for az module
	$securePassword = ConvertTo-SecureString $templateAzureAccountPassword -AsPlainText -Force
	$azureCredentials = New-Object System.Management.Automation.PSCredential ($templateAzureAccountClient, $securePassword)  

    Connect-AzAccount -Credential $azureCredentials -ServicePrincipal -Tenant $templateAzureAccountTenantId | Out-Null
    
    Write-Host "Login successful!"
}
else
{
	Write-Host "Using machine Managed Identity ..."
    Connect-AzAccount -Identity | Out-Null
    
    # Get Identity context
    $identityContext = Get-AzContext
    
    # Set variables
    $templateAzureSubscriptionId = $identityContext.Subscription
    
    if ([string]::IsNullOrWhitespace($templateAzureAccountTenantId))
    {
    	$templateAzureAccountTenantId = $identityContext.Tenant
    }
    
    Set-AzContext -Tenant $templateAzureAccountTenantId | Out-Null

	Write-Host "Successfully set context for Managed Identity!"
}

# Check to see if Container App Environment already exists
Write-Host "Getting list of existing environments ..."
$existingEnvironments = Get-AzContainerAppManagedEnv -ResourceGroupName $templateAzureResourceGroup -SubscriptionId $templateAzureSubscriptionId
$managedEnvironment = $null

if (($null -ne $existingEnvironments) -and ($null -ne ($existingEnvironments | Where-Object {$_.Name -eq $templateEnvironmentName})))
{
	Write-Host "Environment $templateEnvironmentName already exists."
    $managedEnvironment = $existingEnvironments | Where-Object {$_.Name -eq $templateEnvironmentName}
}
else
{
	Write-Host "Environment $templateEnvironmentName not found, creating ..."
    $managedEnvironment = New-AzContainerAppManagedEnv -EnvName $templateEnvironmentName -ResourceGroupName $templateAzureResourceGroup -Location $templateAzureLocation -AppLogConfigurationDestination "" # Empty AppLogConfigurationDestination is workaround for properties issue caused by marking this as required
}

# Set output variable
Write-Host "Setting output variable ManagedEnvironmentId to $($managedEnvironment.Id)"
Set-OctopusVariable -name "ManagedEnvironmentId" -value "$($managedEnvironment.Id)"

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "9b4b9fdc-2f97-4507-8df5-a0c1dd7464a5",
  "Name": "Azure - Create Container App Environment",
  "Description": "Creates a Container App Environment if it doesn't exist.  An output variable called `ManagedEnvironmentId` is created which holds the Id.",
  "Version": 4,
  "ExportedAt": "2023-07-05T15:56:04.248Z",
  "ActionType": "Octopus.Script",
  "Author": "twerthi",
  "Packages": [],
  "Parameters": [
    {
      "Id": "4e3ee370-7f62-4d00-a8c2-bb8717b5d681",
      "Name": "Template.Azure.ResourceGroup.Name",
      "Label": "Azure Resource Group Name",
      "HelpText": "Provide the resource group name to create the environment in.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "11ccbfe4-f170-4d25-bc55-e52327680613",
      "Name": "Template.Azure.Account.SubscriptionId",
      "Label": "Azure Account Subscription Id",
      "HelpText": "The subscription ID of the Azure account to use.  This value can be retrieved from an Azure Account variable type.  Add an Azure Account to your project , then assign the `SubscriptionNumber` property to for this entry.  Leave blank to use the Managed Identity.\n\nFor example, if your Azure Account variable is called MyAccount, the value for this input would be `#{MyAccount.SubscriptionNumber}`",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "e1cf9fa6-e8b9-475a-9800-ef688f1e7ad5",
      "Name": "Template.Azure.Account.ClientId",
      "Label": "Azure Account Client Id",
      "HelpText": "The subscription ID of the Azure account to use.  This value can be retrieved from an Azure Account variable type.  Add an Azure Account to your project , then assign the `Client` property to for this entry.   Leave blank to use the Managed Identity.\n\nFor example, if your Azure Account variable is called MyAccount, the value for this input would be `#{MyAccount.Client}`",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "4dbcd5a4-6d41-4ae0-ac69-76e27fc6bd28",
      "Name": "Template.Azure.Account.TenantId",
      "Label": "Azure Account Tenant Id",
      "HelpText": "The subscription ID of the Azure account to use.  This value can be retrieved from an Azure Account variable type.  Add an Azure Account to your project , then assign the `TenantId` property to for this entry.   If blank, it will use the Managed Identity tenant.\n\nFor example, if your Azure Account variable is called MyAccount, the value for this input would be `#{MyAccount.TenantId}`",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "99a141f5-2fa1-40b9-b04e-8d37fe259a27",
      "Name": "Template.Azure.Account.Password",
      "Label": "Azure Account Password",
      "HelpText": "The subscription ID of the Azure account to use.  This value can be retrieved from an Azure Account variable type.  Add an Azure Account to your project , then assign the `Password` property to for this entry.   Leave blank to use the Managed Identity.\n\nFor example, if your Azure Account variable is called MyAccount, the value for this input would be `#{MyAccount.Password}`",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    },
    {
      "Id": "66f5ee93-3ba9-44e7-8a04-50535b1907cb",
      "Name": "Template.ContainerApp.Environment.Name",
      "Label": "Container App Environment Name",
      "HelpText": "The name of the container app environment to create.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "92daa091-2b90-4615-9a2e-ffc52275ddb4",
      "Name": "Template.Azure.Location.Name",
      "Label": "Azure Location",
      "HelpText": "The location in which to create the container app environment.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "# Define functions\nfunction Get-ModuleInstalled\n{\n    # Define parameters\n    param(\n        $PowerShellModuleName\n    )\n\n    # Check to see if the module is installed\n    if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName))\n    {\n        # It is installed\n        return $true\n    }\n    else\n    {\n        # Module not installed\n        return $false\n    }\n}\n\nfunction Get-NugetPackageProviderNotInstalled\n{\n\t# See if the nuget package provider has been installed\n    return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))\n}\n\nfunction Install-PowerShellModule\n{\n    # Define parameters\n    param(\n        $PowerShellModuleName,\n        $LocalModulesPath\n    )\n\n\t# Check to see if the package provider has been installed\n    if ((Get-NugetPackageProviderNotInstalled) -ne $false)\n    {\n    \t# Display that we need the nuget package provider\n        Write-Host \"Nuget package provider not found, installing ...\"\n        \n        # Install Nuget package provider\n        Install-PackageProvider -Name Nuget -Force\n    }\n\n\t# Save the module in the temporary location\n    Write-Host \"Saving module $PowerShellModuleName to temporary folder ...\"\n    Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force\n    Write-Host \"Save successful!\"\n}\n\n# Check to see if $IsWindows is available\n# Check to see if $IsWindows is available\nif ($null -eq $IsWindows)\n{\n     Write-Host \"Determining Operating System...\"\n    $IsWindows = ([System.Environment]::OSVersion.Platform -eq \"Win32NT\")\n    $IsLinux = ([System.Environment]::OSVersion.Platform -eq \"Unix\")\n}\n\n# Check to see if it's running on Windows\nif ($IsWindows)\n{\n\t# Disable the progress bar so downloading files via Invoke-WebRequest are faster\n    $ProgressPreference = 'SilentlyContinue'\n}\n\nif ($PSEdition -eq \"Core\") {\n    $PSStyle.OutputRendering = \"PlainText\"\n}\n\n# Define PowerShell Modules path\n$LocalModules = (New-Item \"$PWD/Modules\" -ItemType Directory -Force).FullName\n$env:PSModulePath = \"$LocalModules$([IO.Path]::PathSeparator)$env:PSModulePath\"\n$azureModule = \"Az.App\"\n\n# Get variables\n$templateAzureAccountClient = $OctopusParameters['Template.Azure.Account.ClientId']\n$templateAzureAccountPassword = $OctopusParameters['Template.Azure.Account.Password']\n$templateAzureAccountTenantId = $OctopusParameters['Template.Azure.Account.TenantId']\n$templateAzureResourceGroup = $OctopusParameters['Template.Azure.ResourceGroup.Name']\n$templateAzureSubscriptionId = $OctopusParameters['Template.Azure.Account.SubscriptionId']\n$templateEnvironmentName = $OctopusParameters['Template.ContainerApp.Environment.Name']\n$templateAzureLocation = $OctopusParameters['Template.Azure.Location.Name']\n\n# Check for required PowerShell module\nWrite-Host \"Checking for module $azureModule ...\"\n\nif ((Get-ModuleInstalled -PowerShellModuleName $azureModule) -eq $false)\n{\n\t# Install the module\n    Install-PowerShellModule -PowerShellModuleName $azureModule -LocalModulesPath $LocalModules\n}\n\n# Import the necessary module\nWrite-Host \"Importing module $azureModule ...\"\nImport-Module $azureModule\n\n# Check to see if the account was specified\nif (![string]::IsNullOrWhitespace($templateAzureAccountClient))\n{\n\t# Login using the provided account\n    Write-Host \"Logging in as specified account ...\"\n    \n\t# Create credential object for az module\n\t$securePassword = ConvertTo-SecureString $templateAzureAccountPassword -AsPlainText -Force\n\t$azureCredentials = New-Object System.Management.Automation.PSCredential ($templateAzureAccountClient, $securePassword)  \n\n    Connect-AzAccount -Credential $azureCredentials -ServicePrincipal -Tenant $templateAzureAccountTenantId | Out-Null\n    \n    Write-Host \"Login successful!\"\n}\nelse\n{\n\tWrite-Host \"Using machine Managed Identity ...\"\n    Connect-AzAccount -Identity | Out-Null\n    \n    # Get Identity context\n    $identityContext = Get-AzContext\n    \n    # Set variables\n    $templateAzureSubscriptionId = $identityContext.Subscription\n    \n    if ([string]::IsNullOrWhitespace($templateAzureAccountTenantId))\n    {\n    \t$templateAzureAccountTenantId = $identityContext.Tenant\n    }\n    \n    Set-AzContext -Tenant $templateAzureAccountTenantId | Out-Null\n\n\tWrite-Host \"Successfully set context for Managed Identity!\"\n}\n\n# Check to see if Container App Environment already exists\nWrite-Host \"Getting list of existing environments ...\"\n$existingEnvironments = Get-AzContainerAppManagedEnv -ResourceGroupName $templateAzureResourceGroup -SubscriptionId $templateAzureSubscriptionId\n$managedEnvironment = $null\n\nif (($null -ne $existingEnvironments) -and ($null -ne ($existingEnvironments | Where-Object {$_.Name -eq $templateEnvironmentName})))\n{\n\tWrite-Host \"Environment $templateEnvironmentName already exists.\"\n    $managedEnvironment = $existingEnvironments | Where-Object {$_.Name -eq $templateEnvironmentName}\n}\nelse\n{\n\tWrite-Host \"Environment $templateEnvironmentName not found, creating ...\"\n    $managedEnvironment = New-AzContainerAppManagedEnv -EnvName $templateEnvironmentName -ResourceGroupName $templateAzureResourceGroup -Location $templateAzureLocation -AppLogConfigurationDestination \"\" # Empty AppLogConfigurationDestination is workaround for properties issue caused by marking this as required\n}\n\n# Set output variable\nWrite-Host \"Setting output variable ManagedEnvironmentId to $($managedEnvironment.Id)\"\nSet-OctopusVariable -name \"ManagedEnvironmentId\" -value \"$($managedEnvironment.Id)\""
  },
  "Category": "Azure",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/azure-create-containerapp-environment.json",
  "Website": "/step-templates/9b4b9fdc-2f97-4507-8df5-a0c1dd7464a5",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////AHjXf7vrv931QJrh7/f8EIDaIIncMJHfYKvmz+b3n8zw3+76j8Ttr9XycLPpUKLkkKvYFAAABGZJREFUeNrsnNmCqjoQRc1MEiD8/9cer7Yt2KBJZQC8ez07sKlKTQlcLgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzoUSnt8YxXlFuGHSbIaxvj+fip4btkLn1blkWLaF5v03yLhLOYlVuGYfMOMZzNGxCOzhjTJqFkXnjq3Dr1yyvPI3hGl3Ih3zzHHNKudRstRhX5O58vIcShY67Gq6EPIESlzUWvazaGAOGbvU7ArDu/g8M4o8opDZWvbvPzlL/MMBE8jT9T9W7PbAJlHPTBFRf9yVTEcs63msXz2UHLSgf650G/d5t+wjbxxB2UCMqGrk8/LFSD7uJMeNt5bcJCyQZyAe5Fo9KYf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