func (w *worker) runBuild(task *BuildTask, buf io.Writer) (bool, error) { repo := &r.Repo{ Path: task.Repo.URL, Branch: task.Commit.Branch, Commit: task.Commit.Hash, PR: task.Commit.PullRequest, Dir: filepath.Join("/var/cache/drone/src", task.Repo.Slug), Depth: git.GitDepth(task.Script.Git), } return w.runner.Run( task.Script, repo, []byte(task.Repo.PrivateKey), buf, ) }
// execute will execute the build task and persist // the results to the datastore. func (b *BuildTask) execute() error { // we need to be sure that we can recover // from any sort panic that could occur // to avoid brining down the entire application defer func() { if e := recover(); e != nil { b.Build.Finished = time.Now().UTC() b.Commit.Finished = time.Now().UTC() b.Build.Duration = b.Build.Finished.Unix() - b.Build.Started.Unix() b.Commit.Duration = b.Build.Finished.Unix() - b.Build.Started.Unix() b.Commit.Status = "Error" b.Build.Status = "Error" database.SaveBuild(b.Build) database.SaveCommit(b.Commit) } }() // update commit and build status b.Commit.Status = "Started" b.Build.Status = "Started" b.Build.Started = time.Now().UTC() b.Commit.Started = time.Now().UTC() // persist the commit to the database if err := database.SaveCommit(b.Commit); err != nil { return err } // persist the build to the database if err := database.SaveBuild(b.Build); err != nil { return err } // get settings settings, _ := database.GetSettings() // notification context context := ¬ify.Context{ Repo: b.Repo, Commit: b.Commit, Host: settings.URL().String(), } // send all "started" notifications if b.Script.Notifications != nil { b.Script.Notifications.Send(context) } // Send "started" notification to Github if err := updateGitHubStatus(b.Repo, b.Commit); err != nil { log.Printf("error updating github status: %s\n", err.Error()) } // make sure a channel exists for the repository, // the commit, and the commit output (TODO) reposlug := fmt.Sprintf("%s/%s/%s", b.Repo.Host, b.Repo.Owner, b.Repo.Name) commitslug := fmt.Sprintf("%s/%s/%s/commit/%s", b.Repo.Host, b.Repo.Owner, b.Repo.Name, b.Commit.Hash) consoleslug := fmt.Sprintf("%s/%s/%s/commit/%s/builds/%s", b.Repo.Host, b.Repo.Owner, b.Repo.Name, b.Commit.Hash, b.Build.Slug) channel.Create(reposlug) channel.Create(commitslug) channel.CreateStream(consoleslug) // notify the channels that the commit and build started channel.SendJSON(reposlug, b.Commit) channel.SendJSON(commitslug, b.Build) var buf = &bufferWrapper{channel: consoleslug} // append private parameters to the environment // variable section of the .drone.yml file if b.Repo.Params != nil { for k, v := range b.Repo.Params { b.Script.Env = append(b.Script.Env, k+"="+v) } } // execute the build builder := bldr.Builder{} builder.Build = b.Script builder.Repo = &r.Repo{Path: b.Repo.URL, Branch: b.Commit.Branch, Commit: b.Commit.Hash, PR: b.Commit.PullRequest, Dir: filepath.Join("/var/cache/drone/src", b.Repo.Slug), Depth: git.GitDepth(b.Script.Git)} builder.Key = []byte(b.Repo.PrivateKey) builder.Stdout = buf builder.Timeout = 300 * time.Minute defer func() { // update the status of the commit using the // GitHub status API. if err := updateGitHubStatus(b.Repo, b.Commit); err != nil { log.Printf("error updating github status: %s\n", err.Error()) } }() buildErr := builder.Run() b.Build.Finished = time.Now().UTC() b.Commit.Finished = time.Now().UTC() b.Build.Duration = b.Build.Finished.UnixNano() - b.Build.Started.UnixNano() b.Commit.Duration = b.Build.Finished.UnixNano() - b.Build.Started.UnixNano() b.Commit.Status = "Success" b.Build.Status = "Success" b.Build.Stdout = buf.buf.String() // if exit code != 0 set to failure if builder.BuildState == nil || builder.BuildState.ExitCode != 0 { b.Commit.Status = "Failure" b.Build.Status = "Failure" if buildErr != nil && b.Build.Stdout == "" { // TODO: If you wanted to have very friendly error messages, you could do that here b.Build.Stdout = buildErr.Error() + "\n" } } // persist the build to the database if err := database.SaveBuild(b.Build); err != nil { return err } // persist the commit to the database if err := database.SaveCommit(b.Commit); err != nil { return err } // notify the channels that the commit and build finished channel.SendJSON(reposlug, b.Commit) channel.SendJSON(commitslug, b.Build) channel.Close(consoleslug) // send all "finished" notifications if b.Script.Notifications != nil { b.Script.Notifications.Send(context) } return nil }