Skip to content

Commit

Permalink
send the build error message to segment for analytics (#3680)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feroze Mohideen authored Sep 28, 2023
1 parent cfe06cd commit 9717f53
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions cli/cmd/v2/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,53 +150,61 @@ func Apply(ctx context.Context, inp ApplyInput) error {
eventID, _ := createBuildEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID)

if commitSHA == "" {
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID)
return errors.New("Build is required but commit SHA cannot be identified. Please set the PORTER_COMMIT_SHA environment variable or run apply in git repository with access to the git CLI.")
err := errors.New("Build is required but commit SHA cannot be identified. Please set the PORTER_COMMIT_SHA environment variable or run apply in git repository with access to the git CLI.")
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID, err)
return err
}

buildSettings, err := buildSettingsFromBase64AppProto(base64AppProto)
if err != nil {
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID)
return fmt.Errorf("error building settings from base64 app proto: %w", err)
err := fmt.Errorf("error getting build settings from base64 app proto: %w", err)
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID, err)
return err
}

currentAppRevisionResp, err := client.CurrentAppRevision(ctx, cliConf.Project, cliConf.Cluster, appName, deploymentTargetID)
if err != nil {
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID)
return fmt.Errorf("error getting current app revision: %w", err)
err := fmt.Errorf("error getting current app revision: %w", err)
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID, err)
return err
}

if currentAppRevisionResp == nil {
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID)
return errors.New("current app revision is nil")
err := errors.New("current app revision is nil")
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID, err)
return err
}

appRevision := currentAppRevisionResp.AppRevision
if appRevision.B64AppProto == "" {
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID)
return errors.New("current app revision b64 app proto is empty")
err := errors.New("current app revision b64 app proto is empty")
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID, err)
return err
}

currentImageTag, err := imageTagFromBase64AppProto(appRevision.B64AppProto)
if err != nil {
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID)
return fmt.Errorf("error getting image tag from current app revision: %w", err)
err := fmt.Errorf("error getting image tag from current app revision: %w", err)
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID, err)
return err
}

buildSettings.CurrentImageTag = currentImageTag
buildSettings.ProjectID = cliConf.Project

buildEnv, err := client.GetBuildEnv(ctx, cliConf.Project, cliConf.Cluster, appName, appRevision.ID)
if err != nil {
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID)
return fmt.Errorf("error getting build env: %w", err)
err := fmt.Errorf("error getting build env: %w", err)
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID, err)
return err
}
buildSettings.Env = buildEnv.BuildEnvVariables

err = build(ctx, client, buildSettings)
if err != nil {
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID)
return fmt.Errorf("error building app: %w", err)
err := fmt.Errorf("error building app: %w", err)
_ = reportBuildFailure(ctx, client, appName, cliConf, deploymentTargetID, applyResp.AppRevisionId, eventID, err)
return err
}

color.New(color.FgGreen).Printf("Successfully built image (tag: %s)\n", buildSettings.ImageTag) // nolint:errcheck,gosec
Expand Down Expand Up @@ -476,9 +484,15 @@ func updateEnvGroupsInProto(ctx context.Context, base64AppProto string, envGroup
return editedB64AppProto, nil
}

func reportBuildFailure(ctx context.Context, client api.Client, appName string, cliConf config.CLIConfig, deploymentTargetID string, appRevisionID string, eventID string) error {
func reportBuildFailure(ctx context.Context, client api.Client, appName string, cliConf config.CLIConfig, deploymentTargetID string, appRevisionID string, eventID string, buildError error) error {
buildMetadata := make(map[string]interface{})
buildMetadata["end_time"] = time.Now().UTC()

// the below is a temporary solution until we can report build errors via telemetry from the CLI
errorStringMap := make(map[string]string)
errorStringMap["build-error"] = fmt.Sprintf("%+v", buildError)
buildMetadata["errors"] = errorStringMap

err := updateExistingEvent(ctx, client, appName, cliConf.Project, cliConf.Cluster, deploymentTargetID, types.PorterAppEventType_Build, eventID, types.PorterAppEventStatus_Failed, buildMetadata)
if err != nil {
return err
Expand Down

0 comments on commit 9717f53

Please sign in to comment.