-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOctopus_UpdateVariable.ps1
More file actions
82 lines (68 loc) · 2.48 KB
/
Octopus_UpdateVariable.ps1
File metadata and controls
82 lines (68 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<#
.DESCRIPTION
Script to update Octopus variable via Octopus API.
Can be run manually or automated through an Orchestration tool/build system
such as TFS/VSTS, Jenkins, TeamCity, etc.
.EXAMPLE
Example build step using TFS/VSTS to update BuildID Octopus variable with build ID system variable value
1. Add VSTS/TFS PowerShell script build step
2. Add APIKey as secret build definition variable
3. Pass as arguments in build step: BuildID $(Build.BuildID) $(APIKey) OctopusProjectName "http://MyOctopusInstance/octopus/"
#>
Param
(
# Variable name to update
[string(Mandatory=$true)]$VarName,
# New value for variable
[string(Mandatory=$true)]$Newvalue,
# Octopus APIKey (should be encrypted build variable)
[string(Mandatory=$true)]$APIKey,
# Octopus Project Name
[string(Mandatory=$true)]$ProjectName,
# URL to your Octopus server instance
[string(Mandatory=$true)]$OctopusURL
)
Function UpdateOctopusVariable
{
Begin
{
<#
Load required assemblies. They can be found in the Octopus Tentacle MSI: https://octopus.com/downloads
These should be copied to your build server(s) if running via TFS/VSTS build.
#>
Add-Type -Path "$env:PROGRAMFILES\Octopus\Newtonsoft.Json.dll"
Add-Type -Path "$env:PROGRAMFILES\Octopus\Octopus.Client.dll"
# Connection data
$endpoint = new-object Octopus.Client.OctopusServerEndpoint ($OctopusURL, $APIKey)
$repository = new-object Octopus.Client.OctopusRepository $endpoint
}
Process
{
# Get project
$project = $repository.Projects.FindByName($ProjectName)
# Get project's variable set
$variableset = $repository.VariableSets.Get($project.links.variables)
# Get variable to update
$variable = $variableset.Variables | ?{$_.name -eq $VarName}
# Update variable
$variable.Value = $newvalue
}
End
{
# Save variable set
$repository.VariableSets.Modify($variableset)
}
}
Try
{
write-host "Begin updating variable: $VarName value:$Newvalue "
UpdateOctopusVariable
write-host "Done updating variable: $VarName value:$Newvalue "
}
Catch
{
$result += $actionFailed
$result += "`r`n Exception Type: $($_.Exception.GetType().FullName)"
$result += "`r`n Exception Message: $($_.Exception.Message)"
Exit 1
}