Skip to content

Commit 0e6e5bc

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

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

cmd/bob/build.go

+9-1
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,18 @@ 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 := githubStepSummaryWriteImages(stepSummaryFilename, output.images); err != nil {
583+
log.Printf("WARN: %v", err)
584+
}
585+
}
586+
579587
return nil
580588
}())
581589
},

cmd/bob/utils.go

+37
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,38 @@ 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(stepSummaryFilename string, images []imageBuildOutput) error {
126+
withErr := func(err error) error { return fmt.Errorf("githubStepSummaryWriteImages: %w", err) }
127+
128+
stepSummaryFile, err := os.OpenFile(stepSummaryFilename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
129+
if err != nil {
130+
return withErr(err)
131+
}
132+
defer stepSummaryFile.Close()
133+
134+
return githubStepSummaryWriteImagesWithWriter(stepSummaryFile, images)
135+
}
136+
137+
func githubStepSummaryWriteImagesWithWriter(stepSummaryFile io.Writer, images []imageBuildOutput) error {
138+
lines := []string{}
139+
for _, image := range images {
140+
parsed := dockertag.Parse(image.tag)
141+
if parsed == nil {
142+
return fmt.Errorf("failed to parse docker tag: %s", image.tag)
143+
}
144+
145+
// "fn61/varasto" => "varasto"
146+
imageBasename := path.Base(parsed.Repository)
147+
148+
lines = append(lines, "## Image: "+imageBasename, "", "```", image.tag, "```", "", "")
149+
}
150+
151+
if _, err := stepSummaryFile.Write([]byte(strings.Join(lines, "\n"))); err != nil {
152+
return err
153+
}
154+
155+
return nil
156+
}

cmd/bob/utils_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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, githubStepSummaryWriteImagesWithWriter(output, []imageBuildOutput{
14+
{tag: "fn61/varasto:v1.2.3"},
15+
{tag: "fn61/varasto-somethingelse:v1.2.3"},
16+
}))
17+
18+
//nolint:staticcheck
19+
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")
20+
}

0 commit comments

Comments
 (0)