Skip to content

Commit

Permalink
Add GitHub action build reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel committed May 7, 2024
1 parent 60f3625 commit e8d0fe4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func addBuildFlags(cmd *cobra.Command) {
cmd.Flags().StringToString("docker-build-options", nil, "Options passed to all 'docker build' commands")
cmd.Flags().String("report", "", "Generate a HTML report after the build has finished. (e.g. --report myreport.html)")
cmd.Flags().String("report-segment", os.Getenv("LEEWAY_SEGMENT_KEY"), "Report build events to segment using the segment key (defaults to $LEEWAY_SEGMENT_KEY)")
cmd.Flags().Bool("report-github", os.Getenv("GITHUB_OUTPUT") != "", "Report package build success/failure to GitHub Actions using the GITHUB_OUTPUT environment variable")
}

func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemCache) {
Expand Down Expand Up @@ -255,6 +256,11 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemC
} else if segmentkey != "" {
reporter = append(reporter, leeway.NewSegmentReporter(segmentkey))
}
if github, err := cmd.Flags().GetBool("report-github"); err != nil {
log.Fatal(err)
} else if github {
reporter = append(reporter, leeway.NewGitHubReporter())
}

dontTest, err := cmd.Flags().GetBool("dont-test")
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions pkg/leeway/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,3 +621,35 @@ func (sr *SegmentReporter) track(event string, props segment.Properties) {
log.WithField("evt", string(fc)).Debug("reported segment event")
}
}

func NewGitHubReporter() *GitHubActionReporter {
return &GitHubActionReporter{}
}

type GitHubActionReporter struct {
NoopReporter

mu sync.Mutex
}

func (sr *GitHubActionReporter) PackageBuildFinished(pkg *Package, rep *PackageBuildReport) {
sr.mu.Lock()
defer sr.mu.Unlock()

fn, ok := os.LookupEnv("GITHUB_OUTPUT")
if !ok || fn == "" {
return
}
f, err := os.OpenFile(fn, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.WithField("fn", fn).WithError(err).Warn("cannot open GITHUB_OUTPUT file")
return
}
defer f.Close()

var success bool
if rep.Error == nil {
success = true
}
fmt.Fprintf(f, "%s=%v\n", pkg.FilesystemSafeName(), success)
}

0 comments on commit e8d0fe4

Please sign in to comment.