Create and deploy a new release on a schedule

Hi there!

So, I have a number of projects that deploy services/apis to a staging environment. What I would like to do is have a recurring schedule that creates a release with the latest build from Main and deploys it.

Using a single API as an example I have setup a new channel which will filter out PR builds (“^$”) and I created a trigger that will run daily to create and deploy a release using this Channel. Unfortunately, this fails as there is a prompted variable that needs to be set and I don’t see a way to supply a variable via a trigger.

Instead, I tried to create a Runbook process that will trigger a deploy on a schedule but this doesn’t seem to have a step to create a new release each time and not just deploy the latest release. Although I think I can pass through a variable.

Any advice for something I might be missing would be great!

Thanks!

Dex

Greetings! Octopus Deploy is written API-first which means anything you can do in the UI you can do with an API call. Using your Runbook idea, you could add a step prior to the deployment step to create a release using the API

$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = "https://YourServer"
$octopusAPIKey = "API-YourAPIKey"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$projectName = "YourProjectName"
$channelName = "YourChannelName"
$spaceName = "YourSpaceName"

# Get space
$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}

# Get project
$project = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/all" -Headers $header) | Where-Object {$_.Name -eq $projectName}

# Get channel
$channel = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)/channels" -Headers $header).Items | Where-Object {$_.Name -eq $channelName}

# Get deployment process template
$template = Invoke-RestMethod -Uri "$octopusURL/api/$($space.id)/deploymentprocesses/deploymentprocess-$($project.id)/template?channel=$($channel.Id)" -Headers $header

# Create release payload
$releaseBody = @{
    ChannelId        = $channel.Id
    ProjectId        = $project.Id
    Version          = $template.NextVersionIncrement
    SelectedPackages = @()
}

# Loop through the deployment process packages and add to release payload
$template.Packages | ForEach-Object {
    $uri = "$octopusURL/api/$($space.id)/feeds/$($_.FeedId)/packages/versions?packageId=$($_.PackageId)&take=1"
    $version = Invoke-RestMethod -Uri $uri -Method GET -Headers $header
    $version = $version.Items[0].Version

    $releaseBody.SelectedPackages += @{
        ActionName           = $_.ActionName
        PackageReferenceName = $_.PackageReferenceName
        Version              = $version
    }
}

# Create the release
$release = Invoke-RestMethod -Uri "$octopusURL/api/$($space.id)/releases" -Method POST -Headers $header -Body ($releaseBody | ConvertTo-Json -depth 10)

# Display created release
$release

Would this work for you?

Regards,

Shawn

Thank you very much for the quick reply. We will definitely give this a go and report back!

Thank you!

Dex

1 Like

We’ve been able to setup the runbook with the “create release” step using the above powershell. When we run this manually all is good and we can see the release being created (we haven’t included the deploy yet). We then published this runbook snapshot so that we could create a trigger but when the trigger runs it fails and when trying to view the task log we get his error:

image

Going to the Task Log folder we can see the following error message:

We are trying to debug, but can see why the snapshot variables aren’t available for the trigger, as it runs fine when we start it manually.

Any help, much appreciated!

Thanks,

Dex

Hey there Dex! That’s a weird one, I’ll admit. Is the Runbook published? Would you be able to make an edit (like add a space to a step name or something just to make the Save button enable) save it and publish that version to see if it does the same thing?

Regards,

Shawn

Hey! Yeah, it was published. We did try deleting the snapshot and re-publishing a new one, we also tried updating the variable snapshot. No luck.

That’s so bizarre! I’d recommend getting in touch with support@octopus.com (I’m on a different team :slight_smile: ) for the quickest resolution to that issue.

Regards

Shawn

Will do, thank you very much for your help though. On the right track for sure