Updating Octopus Release Variables using Script

(shaik sadiq) #1

Hi Team,

I am trying to update the release variables using script rather then manually clicking the UPDATE VARIABLES option.

The powershell script is as follows:

$apiKey = "xyz"
$OctopusURL = "http://serverurl"
 
$Header =  @{ "X-Octopus-ApiKey" = $apiKey }
 
$ProjectName = "Sample-WebApplication"

$releaseNumber = "0.0.138"

#Getting the project
$project = Invoke-WebRequest -Uri "$OctopusURL/api/projects/$ProjectName" -Headers $Header| ConvertFrom-Json

#Getting the project's release
$release = Invoke-WebRequest -Uri "$OctopusURL/api/projects/$($Project.id)/releases" -Headers $Header| ConvertFrom-Json | select -ExpandProperty Items | ?{$_.version -eq $releaseNumber}

#Updating Release's variable snapshot
Invoke-WebRequest -Uri ("$OctopusURL" + "$($release.Links.self)/snapshot-variables") -Method Post -Headers $Header

When I try to execute the below script i am getting the beow error:

Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed.
At line:20 char:1
+ Invoke-WebRequest -Uri ("$OctopusURL" + "$($release.Links.self)/snaps ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Can you please help me out in this, or is there any other way to update variables with out manually doing it inside each release.

Thanks,
Sadiq

(Mark Siedle) #4

Hi Sadiq,

Thanks for getting in touch.

This is an interesting one. We have not been able to reproduce. Your script works for us!

For example, this was the script we used:

$apiKey = "API-1234"
$OctopusURL = "http://YOUR_SERVER"
 
$header =  @{ "X-Octopus-ApiKey" = $apiKey }
 
$projectSlug = "testing-testing-123"

$releaseNumber = "0.0.2"

#Getting the project
$project = Invoke-WebRequest -Uri "$OctopusURL/api/projects/$projectSlug" -Headers $Header| ConvertFrom-Json
if (!$project) {
    Write-Host("Failed to find project with name $($projectSlug).")
    Exit
}

#Getting the project's release
$release = Invoke-WebRequest -Uri "$OctopusURL/api/projects/$($project.id)/releases" -Headers $header| ConvertFrom-Json | select -ExpandProperty Items | ?{$_.version -eq $releaseNumber}
if (!$release) {
    Write-Host("Failed to find release $($releaseNumber).")
    Exit
}

#Updating Release's variable snapshot
Invoke-WebRequest -Uri ("$OctopusURL" + "$($release.Links.self)/snapshot-variables") -Method Post -Headers $Header

The only way we were able to get the same error message was if we changed Post to Put on this line:

Invoke-WebRequest -Uri ("$OctopusURL" + "$($release.Links.self)/snapshot-variables") -Method Post -Headers $Header

I’m wondering if, for some environmental reason, your server may not like POST requests from PowerShell like this. If you change that to a Put request, do you see any difference?

Alternatively, you can also try using our OctopusClient library with PowerShell.

An example of that would look something like this:

Add-Type -Path 'C:\PathTo\Octopus.Client.dll'

$apikey = 'API-1234' # Get this from your profile
$octopusURI = 'http://YOUR_SERVER' # Your Octopus Server address

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$projectName = "Testing testing 123"
$project = $repository.Projects.FindByName($projectName);
if (!$project) {
    Write-Host("Failed to find project with name $($projectName).")
    Exit
}

$releaseVersion = "0.0.2"
$release = $repository.Projects.GetReleaseByVersion($project, $releaseVersion);
if (!$release) {
    Write-Host("Failed to find release with version $($releaseVersion).")
    Exit
}

# Update snapshot
$repository.Releases.SnapshotVariables($release)
Write-Host("Success")

Ultimately that should be invoking the same requests behind the scenes, so we’d be interested to see if you get the same error when using OctopusClients.

Please let me know how you go.

Cheers
Mark

(system) closed #5

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.