All builds now have a per-repository monotonically increasing build number accessible via DRONE_BUILD_NUMBER
and CI_BUILD_NUMBER env variables.
This commit is contained in:
parent
350d056d34
commit
5f8b026d14
6 changed files with 62 additions and 14 deletions
|
@ -47,6 +47,10 @@ type Commitstore interface {
|
||||||
// KillCommits updates all pending or started commits
|
// KillCommits updates all pending or started commits
|
||||||
// in the datastore settings the status to killed.
|
// in the datastore settings the status to killed.
|
||||||
KillCommits() error
|
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
|
// 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 {
|
func KillCommits(c context.Context) error {
|
||||||
return FromContext(c).KillCommits()
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/drone/drone/shared/model"
|
"github.com/drone/drone/shared/model"
|
||||||
|
@ -103,6 +104,20 @@ func (db *Commitstore) KillCommits() error {
|
||||||
return err
|
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.
|
// Commit table name in database.
|
||||||
const commitTable = "commits"
|
const commitTable = "commits"
|
||||||
|
|
||||||
|
@ -194,3 +209,12 @@ const commitKillStmt = `
|
||||||
UPDATE commits SET commit_status = 'Killed'
|
UPDATE commits SET commit_status = 'Killed'
|
||||||
WHERE commit_status IN ('Started', 'Pending');
|
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 = ?
|
||||||
|
`
|
||||||
|
|
|
@ -76,6 +76,7 @@ func (d *Docker) Do(c context.Context, r *worker.Work) {
|
||||||
// mark the build as Started and update the database
|
// mark the build as Started and update the database
|
||||||
r.Commit.Status = model.StatusStarted
|
r.Commit.Status = model.StatusStarted
|
||||||
r.Commit.Started = time.Now().UTC().Unix()
|
r.Commit.Started = time.Now().UTC().Unix()
|
||||||
|
|
||||||
datastore.PutCommit(c, r.Commit)
|
datastore.PutCommit(c, r.Commit)
|
||||||
|
|
||||||
// notify all listeners that the build is started
|
// notify all listeners that the build is started
|
||||||
|
@ -109,15 +110,22 @@ 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())
|
||||||
|
}
|
||||||
|
|
||||||
path := r.Repo.Host + "/" + r.Repo.Owner + "/" + r.Repo.Name
|
path := r.Repo.Host + "/" + r.Repo.Owner + "/" + r.Repo.Name
|
||||||
repo := &repo.Repo{
|
repo := &repo.Repo{
|
||||||
Name: path,
|
Name: path,
|
||||||
Path: r.Repo.CloneURL,
|
Path: r.Repo.CloneURL,
|
||||||
Branch: r.Commit.Branch,
|
Branch: r.Commit.Branch,
|
||||||
Commit: r.Commit.Sha,
|
Commit: r.Commit.Sha,
|
||||||
PR: r.Commit.PullRequest,
|
PR: r.Commit.PullRequest,
|
||||||
Dir: filepath.Join("/var/cache/drone/src", git.GitPath(script.Git, path)),
|
Dir: filepath.Join("/var/cache/drone/src", git.GitPath(script.Git, path)),
|
||||||
Depth: git.GitDepth(script.Git),
|
Depth: git.GitDepth(script.Git),
|
||||||
|
BuildNumber: buildNumber,
|
||||||
}
|
}
|
||||||
|
|
||||||
priorCommit, _ := datastore.GetCommitPrior(c, r.Commit)
|
priorCommit, _ := datastore.GetCommitPrior(c, r.Commit)
|
||||||
|
|
|
@ -488,11 +488,12 @@ func (b *Builder) writeBuildScript(dir string) error {
|
||||||
f.WriteEnv("DRONE_COMMIT", b.Repo.Commit)
|
f.WriteEnv("DRONE_COMMIT", b.Repo.Commit)
|
||||||
f.WriteEnv("DRONE_PR", b.Repo.PR)
|
f.WriteEnv("DRONE_PR", b.Repo.PR)
|
||||||
f.WriteEnv("DRONE_BUILD_DIR", b.Repo.Dir)
|
f.WriteEnv("DRONE_BUILD_DIR", b.Repo.Dir)
|
||||||
|
f.WriteEnv("DRONE_BUILD_NUMBER", fmt.Sprintf("%d", b.Repo.BuildNumber))
|
||||||
|
|
||||||
// add environment variables for code coverage
|
// add environment variables for code coverage
|
||||||
// systems, like coveralls.
|
// systems, like coveralls.
|
||||||
f.WriteEnv("CI_NAME", "DRONE")
|
f.WriteEnv("CI_NAME", "DRONE")
|
||||||
f.WriteEnv("CI_BUILD_NUMBER", b.Repo.Commit)
|
f.WriteEnv("CI_BUILD_NUMBER", fmt.Sprintf("%d", b.Repo.BuildNumber))
|
||||||
f.WriteEnv("CI_BUILD_URL", "")
|
f.WriteEnv("CI_BUILD_URL", "")
|
||||||
f.WriteEnv("CI_REMOTE", b.Repo.Path)
|
f.WriteEnv("CI_REMOTE", b.Repo.Path)
|
||||||
f.WriteEnv("CI_BRANCH", b.Repo.Branch)
|
f.WriteEnv("CI_BRANCH", b.Repo.Branch)
|
||||||
|
|
|
@ -522,11 +522,12 @@ func TestWriteBuildScript(t *testing.T) {
|
||||||
Hosts: []string{"127.0.0.1"}}
|
Hosts: []string{"127.0.0.1"}}
|
||||||
b.Key = []byte("ssh-rsa AAA...")
|
b.Key = []byte("ssh-rsa AAA...")
|
||||||
b.Repo = &repo.Repo{
|
b.Repo = &repo.Repo{
|
||||||
Path: "git://github.com/drone/drone.git",
|
Path: "git://github.com/drone/drone.git",
|
||||||
Branch: "master",
|
Branch: "master",
|
||||||
Commit: "e7e046b35",
|
Commit: "e7e046b35",
|
||||||
PR: "123",
|
BuildNumber: 0,
|
||||||
Dir: "/var/cache/drone/github.com/drone/drone"}
|
PR: "123",
|
||||||
|
Dir: "/var/cache/drone/github.com/drone/drone"}
|
||||||
b.writeBuildScript(dir)
|
b.writeBuildScript(dir)
|
||||||
|
|
||||||
// persist a dummy build script to disk
|
// persist a dummy build script to disk
|
||||||
|
@ -548,8 +549,9 @@ func TestWriteBuildScript(t *testing.T) {
|
||||||
f.WriteEnv("DRONE_COMMIT", "e7e046b35")
|
f.WriteEnv("DRONE_COMMIT", "e7e046b35")
|
||||||
f.WriteEnv("DRONE_PR", "123")
|
f.WriteEnv("DRONE_PR", "123")
|
||||||
f.WriteEnv("DRONE_BUILD_DIR", "/var/cache/drone/github.com/drone/drone")
|
f.WriteEnv("DRONE_BUILD_DIR", "/var/cache/drone/github.com/drone/drone")
|
||||||
|
f.WriteEnv("DRONE_BUILD_NUMBER", "0")
|
||||||
f.WriteEnv("CI_NAME", "DRONE")
|
f.WriteEnv("CI_NAME", "DRONE")
|
||||||
f.WriteEnv("CI_BUILD_NUMBER", "e7e046b35")
|
f.WriteEnv("CI_BUILD_NUMBER", "0")
|
||||||
f.WriteEnv("CI_BUILD_URL", "")
|
f.WriteEnv("CI_BUILD_URL", "")
|
||||||
f.WriteEnv("CI_REMOTE", "git://github.com/drone/drone.git")
|
f.WriteEnv("CI_REMOTE", "git://github.com/drone/drone.git")
|
||||||
f.WriteEnv("CI_BRANCH", "master")
|
f.WriteEnv("CI_BRANCH", "master")
|
||||||
|
|
|
@ -40,6 +40,9 @@ type Repo struct {
|
||||||
|
|
||||||
// (optional) The depth of the `git clone` command.
|
// (optional) The depth of the `git clone` command.
|
||||||
Depth int
|
Depth int
|
||||||
|
|
||||||
|
// The monotonically increasing build number for this repo
|
||||||
|
BuildNumber int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRemote returns true if the Repository is located
|
// IsRemote returns true if the Repository is located
|
||||||
|
|
Loading…
Reference in a new issue