Showing posts with label DevOps. Show all posts
Showing posts with label DevOps. Show all posts

Wednesday, June 15, 2022

Powershell snippets for Octopus Deploy

Extract url to triggering build from an Octopus Deploy release

Our build pipelines create releases in Octopus Deploy and send along the commit information. It is possible to find the buildid and url to the build from the Octopus.Release.Notes variable.


if ($OctopusParameters["Octopus.Release.Notes"] -match "Build \[(.+)\]\((?<url>.+)\)") 

{

   $buildUrl = $Matches.url

   $buildId=($buildUrl -split "=")[-1]

} else {

   $buildUrl="Build info missing. Manually created release?"

   $buildId ="Unkown"

}


Octopus.Deployment.Changes contains change information and a markdown version of that information is in  Octopus.Deployment.ChangesMarkdown.


Parameter validation in step templates

Validate parameters in step template and create variables from the parameters.Inside the foreach, the first line checks 'required' parameters have a value.
The second if statetment checks for '#{' in the parameter value.
The last line creates a variable with the name of the parameter and sets the value.
{
    # Check for required variables
    if ( @("pShareName","pPhysicalPath") -contains $pName) { if (!$OctopusParameters[$pName]) {Write-Warning "Parameter $pName cannot be empty!"; exit 1}}
    # Check for #{ in any variables
    if ($OctopusParameters[$pName] -and $OctopusParameters[$pName].indexOf("#{") -ne -1) { Write-Warning $("Parameter {0} contains '{1}'! Check variable exists and is scoped properly." -f $pName,$OctopusParameters[$pName]); exit 1 }
    # set a local variable
    Set-Variable -Name $pName -Value $OctopusParameters[$pName]
}

Wednesday, May 11, 2022

Copy variables from one Octopus Deploy process to another

Cloning Octopus Deploy steps from one process to another does not copy any variables between the two, for understandable reasons, and there is no inbuilt method for cloning variables.

This powershell script:

  • gets the project variables from an Octopus Deploy process
  • presents the variables in a Powershell gridview
  • adds the variables selected in the gridview to the target Octopus Deploy process - there is no logic checking if the variables already exist

$sourceProjectName="API.SourceProject"
$targetProjectName="API.TargetProject"

# OD API KEY
$ODAPIKey = "API-PUT-YOUR-KEY-HERE"
$ODUrl = "http://od.somecompany.org"

$credential = "?apikey=$ODAPIKey"

# for all projects
$ODProjectQuery = "$ODUrl/api/projects/all$credential"

$headers = @{
 "X-Octopus-ApiKey"="$ODAPIKey"
 "accept"="application/json"
}

function putData ($link, $body)
{
    $QueryString = "{0}{1}" -f $ODUrl, $link
    #UTF-8 conversion is required to handle international letters like ö å ñ
    $body_utf8=([System.Text.Encoding]::UTF8.GetBytes($($body | ConvertTo-Json -Depth 15)))
    $requestResponse=Invoke-WebRequest -uri $QueryString -Method Put -Body $body_utf8 -ContentType "application/json" -Headers $headers
    Write-Host "Update Status: $($requestResponse.StatusCode) $($requestResponse.StatusDescription)"
}

function getData ($link)
{
    # Create querystring from partial link
    $QueryString = "{0}{1}{2}" -f $ODUrl, $link, $credential
    Invoke-RestMethod -uri $QueryString -Method Get
}

try {

    #Get a list of all projects
    $projects = Invoke-RestMethod -uri $ODProjectQuery -Method get
       
    # Select Source Project
    $sourceProject=$projects | Where-Object { $_.Name -eq $sourceProjectName}

    # Get variables
    $sourceVars=getData $sourceProject.Links.Variables

    # Display variables in gridview and save selected variables
    $importVars = $sourceVars.Variables | Select-Object -Property * -ExcludeProperty Id | Out-GridView -PassThru -Title "Select variables to copy to target project"
    # write out selected variables to output
    $importVars | ConvertTo-Json    

    # Get Target project
    $targetProject=$projects | Where-Object { $_.Name -eq $targetProjectName}
   
    # get target variables
    $targetVariables = getData $targetProject.Links.Variables

    Write-Host "Target variable version pre-update: $($targetVariables.Version)"

    # Add selected variables to target variables
    $targetVariables.Variables += $importVars

    # Send updated variable list back to target OD process
    putData $targetProject.Links.Variables $targetVariables
}
catch
{
    Write-Host $_.Exception.Message
    Write-Host $_.Exception.Response.StatusDescription
    Write-Host $_.ErrorDetails
}