Skip to content

Commit 289653c

Browse files
committed
build: in GitHub actions output built images in job summary
1 parent 73438a6 commit 289653c

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

cmd/bob/build.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"io"
7+
"log"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -571,11 +572,26 @@ func buildEntry() *cobra.Command {
571572
buildCtx.Debug = true
572573
}
573574

574-
_, err := build(buildCtx)
575+
output, err := build(buildCtx)
575576
if err != nil {
576577
return err
577578
}
578579

580+
// https://github.blog/news-insights/product-news/supercharging-github-actions-with-job-summaries/
581+
if stepSummaryFilename := os.Getenv("GITHUB_STEP_SUMMARY"); stepSummaryFilename != "" && len(output.images) > 0 {
582+
if err := func() error {
583+
stepSummaryFile, err := os.OpenFile(stepSummaryFilename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
584+
if err != nil {
585+
return err
586+
}
587+
defer stepSummaryFile.Close()
588+
589+
return githubStepSummaryWriteImages(stepSummaryFile, output.images)
590+
}(); err != nil {
591+
log.Printf("WARN: %v", err)
592+
}
593+
}
594+
579595
return nil
580596
}())
581597
},

cmd/bob/utils.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"io"
77
"os"
88
"os/exec"
9+
"path"
910
"strings"
1011
"sync"
1112

1213
"github.com/function61/turbobob/pkg/bobfile"
14+
"github.com/function61/turbobob/pkg/dockertag"
1315
)
1416

1517
func passthroughStdoutAndStderr(cmd *exec.Cmd) *exec.Cmd {
@@ -117,3 +119,28 @@ func firstNonEmpty(a, b string) string {
117119
return b
118120
}
119121
}
122+
123+
// write image tags as Markdown to GitHub actions's workflow summary so it's easy from their UI to spot
124+
// which images were published as result of the build.
125+
func githubStepSummaryWriteImages(stepSummaryFile io.Writer, images []imageBuildOutput) error {
126+
withErr := func(err error) error { return fmt.Errorf("githubStepSummaryWriteImages: %w", err) }
127+
128+
lines := []string{}
129+
for _, image := range images {
130+
parsed := dockertag.Parse(image.tag)
131+
if parsed == nil {
132+
return withErr(fmt.Errorf("failed to parse docker tag: %s", image.tag))
133+
}
134+
135+
// "fn61/varasto" => "varasto"
136+
imageBasename := path.Base(parsed.Repository)
137+
138+
lines = append(lines, "## Image: "+imageBasename, "", "```", image.tag, "```", "", "")
139+
}
140+
141+
if _, err := stepSummaryFile.Write([]byte(strings.Join(lines, "\n"))); err != nil {
142+
return withErr(err)
143+
}
144+
145+
return nil
146+
}

cmd/bob/utils_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/function61/gokit/testing/assert"
8+
)
9+
10+
func TestGithubStepSummaryWriteImages(t *testing.T) {
11+
output := &bytes.Buffer{}
12+
13+
assert.Ok(t, githubStepSummaryWriteImages(output, []imageBuildOutput{
14+
{tag: "fn61/varasto:v1.2.3"},
15+
{tag: "fn61/varasto-somethingelse:v1.2.3"},
16+
}))
17+
18+
assert.EqualString(t, output.String(), "## Image: varasto\n\n```\nfn61/varasto:v1.2.3\n```\n\n\n## Image: varasto-somethingelse\n\n```\nfn61/varasto-somethingelse:v1.2.3\n```\n\n")
19+
}

0 commit comments

Comments
 (0)