Skip to content

Instantly share code, notes, and snippets.

@Dalmirog-zz
Created March 23, 2016 21:41
Show Gist options
  • Save Dalmirog-zz/0bb2eccdc6c03dc7f90c to your computer and use it in GitHub Desktop.
Save Dalmirog-zz/0bb2eccdc6c03dc7f90c to your computer and use it in GitHub Desktop.
Download Artifact from same release by name
<#
This script will list all the artifacts related to this release and then It'll download
the one declared on the variable $ArtifactName to the path $downloadPath
Lets say you create an artifact when you deploy to Staging and you want to download
it during your deployment to Production. This script will use the current release ID at the time of the Prod deployment (which would be shared
between your deployment to Stage and to Prod) and then It'll use the octopus API to fetch the artifact.
#>
##Config (What you need to touch)
$OctopusAPIkey = "" #Your Octopus API Key. Needed to use the API and download the artifact. eg "API-6VIL2YUR2XKKMHFXEGDEXFYNG"
$downloadPath = "" #Path to download the artifact. eg "C:\Download"
$artifactname = "" #exact Name that was given to the artifact during a previous deployment. eg "Myartifact.txt"
##Process (what you shouldn't need to touch)
$OctopusURL = $OctopusParameters['Octopus.Web.BaseURL'] # This variable will automatically grab the Octopus URL from the depoyment variables
$releaseID = ($OctopusParameters['Octopus.Web.ReleaseLink']).Split("/")[-1] #Using an Octopus variable to get the ID of the current relase
#Function to get all artifacts from the current release
function Get-AllArtifacts($OctopusAPIkey,$OctopusURL,$releaseID){
$List = @()
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$List = (Invoke-WebRequest -Uri "$OctopusURL/api/artifacts?regarding=$releaseID" -Headers $header -usebasicparsing).content | ConvertFrom-Json | select -ExpandProperty items
return $List
}
#Function to download a single artifact
function Download-Artifact($OctopusAPIkey,$OctopusURL,$artifactURL,$artifactFileName,$DownloadPath){
$WebClient = New-Object System.Net.WebClient
$WebClient.Headers.Add("X-Octopus-ApiKey",$OctopusAPIkey)
$WebClient.DownloadFile("$OctopusURL/$artifactURL","$DownloadPath\$artifactFileName")
return "$DownloadPath\$artifactFileName"
}
#Getting list of all artifacts
$AllArtifacts = Get-AllArtifacts -OctopusAPIkey $OctopusAPIkey -OctopusURL $OctopusURL -releaseID $releaseID
#Filtering the wanted artifact
$artifact = $AllArtifacts | ?{$_.filename -eq $ArtifactName}
#downloading the artifact
Download-Artifact -OctopusAPIkey $OctopusAPIkey -OctopusURL $OctopusURL -artifactURL $artifact.Links.Content -artifactFileName $artifact.Filename -DownloadPath $downloadPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment