Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub action build reporter #178

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading