Skip to content

Commit 7d286cd

Browse files
geofflamrockclaude
andcommitted
BMBB-666: Add Set-EnvironmentState/set_environmentstate wrapper functions
Add PowerShell and Bash wrapper functions that emit the new set-environmentstate service message (key/value/sensitive/type), mirroring the existing Set-OctopusVariable/set_octopusvariable conventions. Type is fixed to "State" for this function. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 54f0611 commit 7d286cd

8 files changed

Lines changed: 111 additions & 0 deletions

File tree

source/Calamari.Common/Features/Scripting/Bash/Bootstrap.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,37 @@ function set_octopusvariable
113113
echo $MESSAGE
114114
}
115115

116+
# ---------------------------------------------------------------------------
117+
# Function for setting environment state
118+
# Accepts 3 arguments:
119+
# string: value of the key of the environment state
120+
# string: value of the value of the environment state
121+
# string: optional '-sensitive' to make the value sensitive
122+
# ---------------------------------------------------------------------------
123+
function set_environmentstate
124+
{
125+
MESSAGE="##octopus[set-environmentstate"
126+
127+
if [ -n "$1" ]
128+
then
129+
MESSAGE="$MESSAGE key='$(encode_servicemessagevalue "$1")'"
130+
fi
131+
132+
if [ -n "$2" ]
133+
then
134+
MESSAGE="$MESSAGE value='$(encode_servicemessagevalue "$2")'"
135+
fi
136+
137+
if [ ! -z "${3:-}" ] && [ "$3" = "-sensitive" ]
138+
then
139+
MESSAGE="$MESSAGE sensitive='$(encode_servicemessagevalue "True")'"
140+
fi
141+
142+
MESSAGE="$MESSAGE type='$(encode_servicemessagevalue "State")']"
143+
144+
echo $MESSAGE
145+
}
146+
116147
# -----------------------------------------------------------------------------
117148
# Function to create a new octopus artifact
118149
# Accepts 2 arguments:

source/Calamari.Common/Features/Scripting/WindowsPowerShell/Bootstrap.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,23 @@ function Set-OctopusVariable([string]$name, [string]$value, [switch]$sensitive)
165165
}
166166
}
167167

168+
function Set-EnvironmentState([string]$key, [string]$value, [switch]$sensitive)
169+
{
170+
$key = Convert-ServiceMessageValue($key)
171+
$value = Convert-ServiceMessageValue($value)
172+
$type = Convert-ServiceMessageValue("State")
173+
$trueEncoded = Convert-ServiceMessageValue("True")
174+
175+
If ($sensitive)
176+
{
177+
Write-Host "##octopus[set-environmentstate key='$( $key )' value='$( $value )' sensitive='$( $trueEncoded )' type='$( $type )']"
178+
}
179+
Else
180+
{
181+
Write-Host "##octopus[set-environmentstate key='$( $key )' value='$( $value )' type='$( $type )']"
182+
}
183+
}
184+
168185
function Convert-ToServiceMessageParameter([string]$name, [string]$value)
169186
{
170187
$value = Convert-ServiceMessageValue($value)

source/Calamari.Testing/LogParser/ScriptServiceMessageNames.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ public static class SetVariable
1212
public const string SensitiveAttribute = "sensitive";
1313
}
1414

15+
public static class EnvironmentState
16+
{
17+
public const string Name = "set-environmentstate";
18+
public const string KeyAttribute = "key";
19+
public const string ValueAttribute = "value";
20+
public const string SensitiveAttribute = "sensitive";
21+
public const string TypeAttribute = "type";
22+
}
23+
1524
public static class StdOutBehaviour
1625
{
1726
public const string Ignore = "stdout-ignore";

source/Calamari.Tests/Fixtures/Bash/BashFixture.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@ public void ShouldPrintSensitiveVariable(FeatureToggle? featureToggle)
4646
});
4747
}
4848

49+
[TestCase(FeatureToggle.BashParametersArrayFeatureToggle)]
50+
[TestCase(null)]
51+
[RequiresBashDotExeIfOnWindows]
52+
public void ShouldSetEnvironmentState(FeatureToggle? featureToggle)
53+
{
54+
var (output, _) = RunScript("set-environment-state.sh", new Dictionary<string, string>().AddFeatureToggleToDictionary(new List<FeatureToggle?>{featureToggle}));
55+
56+
Assert.Multiple(() =>
57+
{
58+
output.AssertSuccess();
59+
output.AssertOutput("##octopus[set-environmentstate key='TXlLZXk=' value='TXlWYWx1ZQ==' type='U3RhdGU=']");
60+
});
61+
}
62+
63+
[TestCase(FeatureToggle.BashParametersArrayFeatureToggle)]
64+
[TestCase(null)]
65+
[RequiresBashDotExeIfOnWindows]
66+
public void ShouldSetSensitiveEnvironmentState(FeatureToggle? featureToggle)
67+
{
68+
var (output, _) = RunScript("set-sensitive-environment-state.sh", new Dictionary<string, string>().AddFeatureToggleToDictionary(new List<FeatureToggle?>{featureToggle}));
69+
70+
Assert.Multiple(() =>
71+
{
72+
output.AssertSuccess();
73+
output.AssertOutput("##octopus[set-environmentstate key='U2VjcmV0S2V5' value='U2VjcmV0IFZhbHVl' sensitive='VHJ1ZQ==' type='U3RhdGU=']");
74+
});
75+
}
76+
4977
[TestCase(FeatureToggle.BashParametersArrayFeatureToggle)]
5078
[TestCase(null)]
5179
[RequiresBashDotExeIfOnWindows]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
set_environmentstate "MyKey" "MyValue"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
set_environmentstate "SecretKey" "Secret Value" -sensitive

source/Calamari.Tests/Fixtures/PowerShell/PowerShellFixtureBase.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,24 @@ public void ShouldSetSensitiveVariables()
383383
AssertPowerShellEdition(output);
384384
}
385385

386+
[Test]
387+
public void ShouldWriteServiceMessageForEnvironmentState()
388+
{
389+
var (output, _) = RunPowerShellScript("CanSetEnvironmentState.ps1");
390+
output.AssertSuccess();
391+
output.AssertOutput("##octopus[set-environmentstate key='TXlLZXk=' value='TXlWYWx1ZQ==' type='U3RhdGU=']");
392+
AssertPowerShellEdition(output);
393+
}
394+
395+
[Test]
396+
public void ShouldWriteServiceMessageForSensitiveEnvironmentState()
397+
{
398+
var (output, _) = RunPowerShellScript("CanSetEnvironmentState.ps1");
399+
output.AssertSuccess();
400+
output.AssertOutput("##octopus[set-environmentstate key='U2VjcmV0S2V5' value='U2VjcmV0IFZhbHVl' sensitive='VHJ1ZQ==' type='U3RhdGU=']");
401+
AssertPowerShellEdition(output);
402+
}
403+
386404
[Test]
387405
public void ShouldSetActionIndexedOutputVariables()
388406
{
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
Set-EnvironmentState -Key "MyKey" -Value "MyValue"
3+
4+
Set-EnvironmentState -Key "SecretKey" -Value "Secret Value" -Sensitive

0 commit comments

Comments
 (0)