Skip to content

Commit b3883e8

Browse files
authored
Switch to environment files for SetOutput and SaveState (#49)
1 parent a0bd34b commit b3883e8

File tree

2 files changed

+74
-27
lines changed

2 files changed

+74
-27
lines changed

actions.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ var (
3939
)
4040

4141
const (
42-
addMaskCmd = "add-mask"
43-
setOutputCmd = "set-output"
44-
saveStateCmd = "save-state"
42+
addMaskCmd = "add-mask"
4543

46-
pathCmd = "path" // used when issuing the file command
44+
envCmd = "env"
45+
outputCmd = "output"
46+
pathCmd = "path"
47+
stateCmd = "state"
4748

48-
envCmd = "env" // used when issuing the file command
49-
envCmdMsgFmt = "%s<<%s" + EOF + "%s" + EOF + "%s" // ${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}
50-
envCmdDelimiter = "_GitHubActionsFileCommandDelimeter_"
49+
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
50+
multiLineFileDelim = "_GitHubActionsFileCommandDelimeter_"
51+
multilineFileCmd = "%s<<" + multiLineFileDelim + EOF + "%s" + EOF + multiLineFileDelim // ${name}<<${delimiter}${os.EOL}${convertedVal}${os.EOL}${delimiter}
5152

5253
addMatcherCmd = "add-matcher"
5354
removeMatcherCmd = "remove-matcher"
@@ -196,14 +197,15 @@ func (c *Action) AddPath(p string) {
196197

197198
// SaveState saves state to be used in the "finally" post job entry point. It
198199
// panics if it cannot write to the output stream.
200+
//
201+
// On 2022-10-11, GitHub deprecated "::save-state name=<k>::<v>" in favor of
202+
// [environment files].
203+
//
204+
// [environment files]: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
199205
func (c *Action) SaveState(k, v string) {
200-
// ::save-state name=<k>::<v>
201-
c.IssueCommand(&Command{
202-
Name: saveStateCmd,
203-
Message: v,
204-
Properties: CommandProperties{
205-
"name": k,
206-
},
206+
c.IssueFileCommand(&Command{
207+
Name: stateCmd,
208+
Message: fmt.Sprintf(multilineFileCmd, k, v),
207209
})
208210
}
209211

@@ -275,20 +277,21 @@ func (c *Action) AddStepSummaryTemplate(tmpl string, data any) error {
275277
func (c *Action) SetEnv(k, v string) {
276278
c.IssueFileCommand(&Command{
277279
Name: envCmd,
278-
Message: fmt.Sprintf(envCmdMsgFmt, k, envCmdDelimiter, v, envCmdDelimiter),
280+
Message: fmt.Sprintf(multilineFileCmd, k, v),
279281
})
280282
}
281283

282284
// SetOutput sets an output parameter. It panics if it cannot write to the
283285
// output stream.
286+
//
287+
// On 2022-10-11, GitHub deprecated "::set-output name=<k>::<v>" in favor of
288+
// [environment files].
289+
//
290+
// [environment files]: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
284291
func (c *Action) SetOutput(k, v string) {
285-
// ::set-output name=<k>::<v>
286-
c.IssueCommand(&Command{
287-
Name: setOutputCmd,
288-
Message: v,
289-
Properties: CommandProperties{
290-
"name": k,
291-
},
292+
c.IssueFileCommand(&Command{
293+
Name: outputCmd,
294+
Message: fmt.Sprintf(multilineFileCmd, k, v),
292295
})
293296
}
294297

actions_test.go

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,32 @@ func TestAction_SaveState(t *testing.T) {
183183
t.Parallel()
184184

185185
var b bytes.Buffer
186-
a := New(WithWriter(&b))
186+
file, err := os.CreateTemp("", "")
187+
if err != nil {
188+
t.Fatalf("unable to create a temp env file: %s", err)
189+
}
190+
defer os.Remove(file.Name())
191+
192+
fakeGetenvFunc := newFakeGetenvFunc(t, "GITHUB_STATE", file.Name())
193+
194+
a := New(WithWriter(&b), WithGetenv(fakeGetenvFunc))
187195
a.SaveState("key", "value")
196+
a.SaveState("key2", "value2")
197+
198+
// expect an empty stdout buffer
199+
if got, want := b.String(), ""; got != want {
200+
t.Errorf("expected %q to be %q", got, want)
201+
}
202+
203+
// expect the command to be written to the file.
204+
data, err := io.ReadAll(file)
205+
if err != nil {
206+
t.Errorf("unable to read temp env file: %s", err)
207+
}
188208

189-
if got, want := b.String(), "::save-state name=key::value"+EOF; got != want {
209+
want := "key<<_GitHubActionsFileCommandDelimeter_" + EOF + "value" + EOF + "_GitHubActionsFileCommandDelimeter_" + EOF
210+
want += "key2<<_GitHubActionsFileCommandDelimeter_" + EOF + "value2" + EOF + "_GitHubActionsFileCommandDelimeter_" + EOF
211+
if got := string(data); got != want {
190212
t.Errorf("expected %q to be %q", got, want)
191213
}
192214
}
@@ -314,8 +336,8 @@ func TestAction_SetEnv(t *testing.T) {
314336
if err != nil {
315337
t.Fatalf("unable to create a temp env file: %s", err)
316338
}
317-
318339
defer os.Remove(file.Name())
340+
319341
fakeGetenvFunc := newFakeGetenvFunc(t, "GITHUB_ENV", file.Name())
320342
a := New(WithWriter(&b), WithGetenv(fakeGetenvFunc))
321343
a.SetEnv("key", "value")
@@ -343,10 +365,32 @@ func TestAction_SetOutput(t *testing.T) {
343365
t.Parallel()
344366

345367
var b bytes.Buffer
346-
a := New(WithWriter(&b))
368+
file, err := os.CreateTemp("", "")
369+
if err != nil {
370+
t.Fatalf("unable to create a temp env file: %s", err)
371+
}
372+
defer os.Remove(file.Name())
373+
374+
fakeGetenvFunc := newFakeGetenvFunc(t, "GITHUB_OUTPUT", file.Name())
375+
376+
a := New(WithWriter(&b), WithGetenv(fakeGetenvFunc))
347377
a.SetOutput("key", "value")
378+
a.SetOutput("key2", "value2")
379+
380+
// expect an empty stdout buffer
381+
if got, want := b.String(), ""; got != want {
382+
t.Errorf("expected %q to be %q", got, want)
383+
}
384+
385+
// expect the command to be written to the file.
386+
data, err := io.ReadAll(file)
387+
if err != nil {
388+
t.Errorf("unable to read temp env file: %s", err)
389+
}
348390

349-
if got, want := b.String(), "::set-output name=key::value"+EOF; got != want {
391+
want := "key<<_GitHubActionsFileCommandDelimeter_" + EOF + "value" + EOF + "_GitHubActionsFileCommandDelimeter_" + EOF
392+
want += "key2<<_GitHubActionsFileCommandDelimeter_" + EOF + "value2" + EOF + "_GitHubActionsFileCommandDelimeter_" + EOF
393+
if got := string(data); got != want {
350394
t.Errorf("expected %q to be %q", got, want)
351395
}
352396
}

0 commit comments

Comments
 (0)