Pack, push & release using REST

usability
(Octopus) #1

I am trying to replace the octo.exe with REST calls. The two octo commands are as follows:

octo.exe pack --id=<id> --format=Zip --version=<version> --basePath=<basePath> --outFolder=<outfolder>
octo.exe push --package=<path\to\package.zip> --server=<server> --apiKey=<apikey>

After pushing I want to start a release – also with the REST API.

I have been looking at the Swagger interface and can successfully do a number of GETs to receive information about the project I want to release.
I’ve also looked/searched in the examples at https://github.com/OctopusDeploy/OctopusDeploy-Api, but I haven’t found good starting points for the REST calls I want to make.

Any help would be appreciated!

(Shawn Sesna) #3

Greetings Octopus2! Have a look at this git repo, https://github.com/OctopusDeploy/OctopusDeploy-Api/tree/master/REST/PowerShell. Let me know if this doesn’t have what you’re looking for :slight_smile:

Happy deployments!

(Octopus) #4

Thank you for the link, but do you have a more specific reference/PowerShell file?
I haven’t had much luck grep’ing through this repo.

(Shawn Sesna) #5

For pushing, take a look at https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/REST/PowerShell/Packages/PushPackage.ps1

For creating a release, try https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/REST/PowerShell/Releases/CreateRelease.ps1

Octopus REST doesn’t have any methods for performing a pack operation, those will need to be done via octo.exe or nuget.exe.

Please let me know if you need anything else :slight_smile:

1 Like
(Octopus) #6

Thanks! One small follow-up: I don’t understand the push example well enough to replicate it in a different language. Quite a lot seems to happen in $packageFileStream – more than in a usual POST :slight_smile:

Unfortunately, the /api/packages/raw endpoint is not documented in the Swagger I have.

Is there somewhere I can read more about how to push?

P.s.: I cannot find a “mark as resolved button”.

(Shawn Sesna) #7

You don’t have this in your swagger?

I’ve pasted the code and added some comments. I’m hoping that they help and not insult your intelligence :slight_smile:

# Create web request object
$webRequest = [System.Net.HttpWebRequest]::Create($packageUrl);

# Configure web request object
$webRequest.AllowWriteStreamBuffering = $false
$webRequest.SendChunked = $true
$webRequest.Accept = "application/json";
$webRequest.ContentType = "application/json";
$webRequest.Method = "POST";
$webRequest.Headers["X-Octopus-ApiKey"] = $apiKey;

# Create FileStream object for package that we're uploading
$packageFileStream = new-object IO.FileStream $packageFilePath,'Open','Read','Read'

# Not sure why this is indented - define text string and append the ticks (https://docs.microsoft.com/en-us/dotnet/api/system.datetime.ticks?view=netframework-4.8)
    $boundary = "----------------------------" + [System.DateTime]::Now.Ticks.ToString("x");

# Convert the $boundary string variable into a byte array of ASCII encoding
    $boundarybytes = [System.Text.Encoding]::ASCII.GetBytes("`r`n--" + $boundary + "`r`n")

# Set the content type, defining what the boundary is
    $webRequest.ContentType = "multipart/form-data; boundary=" + $boundary;

# Write the boundary byte array to the web request stream
    $webRequest.GetRequestStream().Write($boundarybytes, 0, $boundarybytes.Length);

# Define header, include filename of package in header
    $header = "Content-Disposition: form-data; filename="""+ [System.IO.Path]::GetFileName($packageFilePath) +"""`r`nContent-Type: application/octet-stream`r`n`r`n";

# Convert header string into byte array using ASCII encoding
    $headerbytes = [System.Text.Encoding]::ASCII.GetBytes($header);

# Write header to stream
    $webRequest.GetRequestStream().Write($headerbytes, 0, $headerbytes.Length);

# Copy contents of file stream object into the web request stream object
    $packageFileStream.CopyTo($webRequest.GetRequestStream());

# Write boundary byte array to request stream
    $webRequest.GetRequestStream().Write($boundarybytes, 0, $boundarybytes.Length);

# Flush and close the web request stream
    $webRequest.GetRequestStream().Flush();
    $webRequest.GetRequestStream().Close();

# Close filestream
    $packageFileStream.Close();
    $packageFileStream.Dispose();

# Get response from web server
$webResponse = $webRequest.GetResponse();
Write-Host $webResponse.StatusCode $webResponse.StatusDescription;  
$webResponse.Dispose();
(Octopus) #8

Thank you for the detailed explanations! I will give it a try :slight_smile:

I also have the green “POST /api/packages/raw”, but when I click on it there is just a note saying that documentation will be added in a future version.

(Shawn Sesna) #9

You’re very welcome :slight_smile: Please let us know if we can be of further assistance.