diff --git a/server/datastore/commit.go b/server/datastore/commit.go index 802125efa..8a21a961b 100644 --- a/server/datastore/commit.go +++ b/server/datastore/commit.go @@ -47,6 +47,10 @@ type Commitstore interface { // KillCommits updates all pending or started commits // in the datastore settings the status to killed. KillCommits() error + + // GetCommitBuildNumber retrieves the monotonically increaing build number + // from the commit's repo + GetBuildNumber(commit *model.Commit) (int64, error) } // GetCommit retrieves a commit from the @@ -112,3 +116,9 @@ func DelCommit(c context.Context, commit *model.Commit) error { func KillCommits(c context.Context) error { return FromContext(c).KillCommits() } + +// GetBuildNumber retrieves the monotonically increaing build number +// from the commit's repo +func GetBuildNumber(c context.Context, commit *model.Commit) (int64, error) { + return FromContext(c).GetBuildNumber(commit) +} diff --git a/server/datastore/database/commit.go b/server/datastore/database/commit.go index a34c2662d..d36e02ae4 100644 --- a/server/datastore/database/commit.go +++ b/server/datastore/database/commit.go @@ -1,6 +1,7 @@ package database import ( + "fmt" "time" "github.com/drone/drone/shared/model" @@ -103,6 +104,20 @@ func (db *Commitstore) KillCommits() error { return err } +// GetBuildNumber retrieves the build number for a commit. +func (db *Commitstore) GetBuildNumber(commit *model.Commit) (int64, error) { + row := db.QueryRow(rebind(commitGetBuildNumberStmt), commit.ID, commit.RepoID) + if row == nil { + return 0, fmt.Errorf("Unable to get build number for commit %d", commit.ID) + } + var bn int64 + err := row.Scan(&bn) + if err != nil { + return 0, err + } + return bn, nil +} + // Commit table name in database. const commitTable = "commits" @@ -194,3 +209,12 @@ const commitKillStmt = ` UPDATE commits SET commit_status = 'Killed' WHERE commit_status IN ('Started', 'Pending'); ` + +// SQL statement to retrieve the build number for +// a commit +const commitGetBuildNumberStmt = ` +SELECT COUNT(1) +FROM commits +WHERE commit_id <= ? + AND repo_id = ? +` diff --git a/server/datastore/database/commit_test.go b/server/datastore/database/commit_test.go index d629ee250..8591e18e3 100644 --- a/server/datastore/database/commit_test.go +++ b/server/datastore/database/commit_test.go @@ -69,6 +69,21 @@ func TestCommitstore(t *testing.T) { g.Assert(commit.Updated).Equal(getcommit.Updated) }) + g.It("Should Get the build number", func() { + commit := model.Commit{ + RepoID: 1, + Branch: "foo", + Sha: "85f8c029b902ed9400bc600bac301a0aadb144ac", + Status: model.StatusSuccess, + Created: 1398065343, + Updated: 1398065344, + } + cs.PostCommit(&commit) + bn, err := cs.GetBuildNumber(&commit) + g.Assert(err == nil).IsTrue() + g.Assert(bn).Equal(int64(1)) + }) + g.It("Should Delete a Commit", func() { commit := model.Commit{ RepoID: 1, diff --git a/server/worker/docker/docker.go b/server/worker/docker/docker.go index d2a38514c..4f497fcee 100644 --- a/server/worker/docker/docker.go +++ b/server/worker/docker/docker.go @@ -1,6 +1,7 @@ package docker import ( + "fmt" "log" "path/filepath" "runtime/debug" @@ -76,6 +77,7 @@ func (d *Docker) Do(c context.Context, r *worker.Work) { // mark the build as Started and update the database r.Commit.Status = model.StatusStarted r.Commit.Started = time.Now().UTC().Unix() + datastore.PutCommit(c, r.Commit) // notify all listeners that the build is started @@ -109,6 +111,14 @@ func (d *Docker) Do(c context.Context, r *worker.Work) { } } + // TODO: handle error better? + buildNumber, err := datastore.GetBuildNumber(c, r.Commit) + if err != nil { + log.Printf("Unable to fetch build number, Err: %s", err.Error()) + } + script.Env = append(script.Env, fmt.Sprintf("DRONE_BUILD_NUMBER=%d", buildNumber)) + script.Env = append(script.Env, fmt.Sprintf("CI_BUILD_NUMBER=%d", buildNumber)) + path := r.Repo.Host + "/" + r.Repo.Owner + "/" + r.Repo.Name repo := &repo.Repo{ Name: path, diff --git a/shared/build/build.go b/shared/build/build.go index 9fc284200..e77776e8e 100644 --- a/shared/build/build.go +++ b/shared/build/build.go @@ -492,7 +492,6 @@ func (b *Builder) writeBuildScript(dir string) error { // add environment variables for code coverage // systems, like coveralls. f.WriteEnv("CI_NAME", "DRONE") - f.WriteEnv("CI_BUILD_NUMBER", b.Repo.Commit) f.WriteEnv("CI_BUILD_URL", "") f.WriteEnv("CI_REMOTE", b.Repo.Path) f.WriteEnv("CI_BRANCH", b.Repo.Branch) diff --git a/shared/build/build_test.go b/shared/build/build_test.go index f9930efd3..0efa68fd7 100644 --- a/shared/build/build_test.go +++ b/shared/build/build_test.go @@ -549,7 +549,6 @@ func TestWriteBuildScript(t *testing.T) { f.WriteEnv("DRONE_PR", "123") f.WriteEnv("DRONE_BUILD_DIR", "/var/cache/drone/github.com/drone/drone") f.WriteEnv("CI_NAME", "DRONE") - f.WriteEnv("CI_BUILD_NUMBER", "e7e046b35") f.WriteEnv("CI_BUILD_URL", "") f.WriteEnv("CI_REMOTE", "git://github.com/drone/drone.git") f.WriteEnv("CI_BRANCH", "master")