How to cancel an ongoing release apart from manually doing it

usability
(shaik sadiq) #1

Hi Team,

I have a requirement where there is a condition, if the the condition is satisfied the deployment will be succeeded or else the deployment will be failed.

I am using a powershell script to fail a release by using “THROW” command.

But instead of failing the release i need to CANCEL the release

I know that there is an option to change the release state using below script:
$apikey=“xyz”
$header = @{ “X-Octopus-ApiKey” = $apikey }
$body = @{ State = ‘Failed’; Reason = ‘Cos I said so’; } | ConvertTo-Json
Invoke-WebRequest “http://octopus.xyz.com/api/tasks/ServerTasks-23030/state” -Method POST -Headers $header -Body $body

but this works only for already created releases, but my requirement is to “CANCEL” an ongoing release through API or scripts instead of failing it.

As there is an option to cancel the ongoing release manually then there might be a backend script which is doing it.

Can you please help me with that particular script or API for cancelling the release if my condition is not satisfied.

NOTE: AM USING BELOW CONDITION

$A=gc env.txt | select -index 0
if ($A -eq “Yes”)
{
echo “proceeding to next task”
}
else
{
throw “failed, as env is on freeze”
}

By this script my deployment will failed if condition is not satisfied, but i need the release to be cancelled.

It would be of great help if a solution to this problem is provided.

Thanks,
Shaik Sadiq

(Daniel Fischer) #3

Hi Shaik,

Thanks for getting in touch! I think I have a fairly simple solution here for you.

Assuming you know the ServerTask-ID, you can run something like the following script:

# You can this dll from your Octopus Server/Tentacle installation directory or from
# https://www.nuget.org/packages/Octopus.Client/
Add-Type -Path 'C:\MyScripts\Octopus.Client.dll' 

$apikey = 'API-KEY' # Get this from your profile
$octopusURI = 'http://OctopusServer' # Your server address

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = new-object Octopus.Client.OctopusRepository $endpoint

$deployments = $repository.Tasks.get("ServerTasks-5583")

$repository.tasks.Cancel($deployments)

With this command, you can supply the ID for the server task which you would like to cancel, then pass it through $repository.tasks.Cancel() which should immediately cancel the task.

Does this sound like it’s what you are after?

Let me know if this helps, or if you have any further questions here.

Best regards,
Daniel