Attaching Artifact link to an email

Hello,
In Step 1 I created an artifact, “test.txt”. In Step 2 I want to send that file or a link to that file to an email address. Does anyone have any idea how to do that?
Thank you

Hi @chikov2,

Thanks for getting in touch!

The best way to do this would likely be to leverage a script that collects the artifact download URLs and stores them in an output variable.
That variable can then be used as content in a Send an E-mail step.

We have documentation on downloading runbook artifacts that has a script that can be run to collect these resources.
This script can be edited to instead gather the download URLs for these artifacts into a list.

The script below is the edited version I’ve used previously to gather the artifact URLs into an output variable.

$ErrorActionPreference = "Stop";
# Define working variables
$octopusURL = "https://OctopusURL"
$octopusAPIKey = "API-XXXXXXXXXXXXXXXXXXXXX"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = "Default"
$projectName = "projectWithRunbooks"
$runbookName = "Runbook Name"
$environmentName = "Development"

# Get space
$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces?partialName=$([uri]::EscapeDataString($spaceName))&skip=0&take=100" -Headers $header
$space = $spaces.Items | Where-Object { $_.Name -eq $spaceName }

# Get environment
$environments = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/environments?partialName=$([uri]::EscapeDataString($environmentName))&skip=0&take=100" -Headers $header
$environment = $environments.Items | Where-Object { $_.Name -eq $environmentName }

# Get project
$projects = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects?partialName=$([uri]::EscapeDataString($projectName))&skip=0&take=100" -Headers $header
$project = $projects.Items | Where-Object { $_.Name -eq $projectName }

# Get runbook
$runbooks = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)/runbooks?partialName=$([uri]::EscapeDataString($runbookName))&skip=0&take=100" -Headers $header
$runbook = $runbooks.Items | Where-Object { $_.Name -eq $runbookName }

# Get latest runbook run to that environment
$tasks = Invoke-RestMethod -Uri "$octopusURL/api/tasks?skip=0&runbook=$($runbook.Id)&project=$($project.Id)&spaces=$($space.Id)&environment=$($environment.Id)&includeSystem=false" -Headers $header
$task = $tasks.Items | Where-Object {$_.State -eq "Success"} | Select-Object -First 1

$artifacts = Invoke-RestMethod -Method Get -Uri "$OctopusUrl/api/$($space.Id)/artifacts?regarding=$($task.Id)" -Headers $header
$artifactsList = @()

foreach($item in $artifacts.Items){
$artifactsList = $artifactsList += ($OctopusURL + $item.Links.Self + "/content")
}

Set-OctopusVariable -name "artifactsEndpoint" -value $artifactsList

Assuming that the script above runs in a step named Collect Artifacts, we can then retrieve the URLs in a subsequent step via the following:

$OctopusParameters["Octopus.Action[Collect Artifacts].Output.artifactsEndpoint"]

I hope this helps! Please don’t hesitate to get back in touch if you have any questions on the above or concerns.

Regards,
Paul

1 Like

Thank you.
I was able to do it slightly shorter way since I was the one that created the artifact in the previous step.

$octopusAPIKey = "API KEY"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$task_id = $OctopusParameters["Octopus.RunbookRun.Id"]
$artifacts = Invoke-RestMethod -Method Get -Uri "https://<our Octopus>/api/Spaces-1/artifacts/?regarding=$($task_id)" -Headers $header
$artifact = $artifacts.Items | Where-Object {$_.Filename -eq "FileName.txt"}
Set-OctopusVariable -name "FileNameDescripton" -value $artifact.Id

Seems to work. Please let me know if that looks ok.

If that’s working for what you need, I don’t see any problems with it. :slight_smile:

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