2016-09-30 22:03:08 +02:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-08-08 12:05:35 +03:00
|
|
|
"regexp"
|
2019-05-08 08:44:28 -05:00
|
|
|
"strconv"
|
2016-11-27 18:40:28 +01:00
|
|
|
"strings"
|
2019-05-08 08:44:28 -05:00
|
|
|
"time"
|
2016-09-30 22:03:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// These flags override values in build env.
|
2023-08-12 00:21:46 +02:00
|
|
|
GitCommitFlag = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
|
|
|
|
GitBranchFlag = flag.String("git-branch", "", `Overrides git branch being built`)
|
|
|
|
GitTagFlag = flag.String("git-tag", "", `Overrides git tag being built`)
|
|
|
|
BuildnumFlag = flag.String("buildnum", "", `Overrides CI build number`)
|
|
|
|
PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
|
|
|
|
CronJobFlag = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
|
|
|
|
UbuntuVersionFlag = flag.String("ubuntu", "", `Sets the ubuntu version being built for`)
|
2016-09-30 22:03:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Environment contains metadata provided by the build environment.
|
|
|
|
type Environment struct {
|
2021-05-04 13:01:20 +02:00
|
|
|
CI bool
|
2019-05-08 08:44:28 -05:00
|
|
|
Name string // name of the environment
|
|
|
|
Repo string // name of GitHub repo
|
|
|
|
Commit, Date, Branch, Tag string // Git info
|
|
|
|
Buildnum string
|
2023-08-12 00:21:46 +02:00
|
|
|
UbuntuVersion string // Ubuntu version being built for
|
2019-05-08 08:44:28 -05:00
|
|
|
IsPullRequest bool
|
|
|
|
IsCronJob bool
|
2016-09-30 22:03:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (env Environment) String() string {
|
2019-05-08 08:44:28 -05:00
|
|
|
return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)",
|
|
|
|
env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
|
2016-09-30 22:03:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Env returns metadata about the current CI environment, falling back to LocalEnv
|
|
|
|
// if not running on CI.
|
|
|
|
func Env() Environment {
|
|
|
|
switch {
|
|
|
|
case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
|
2019-05-08 08:44:28 -05:00
|
|
|
commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA")
|
|
|
|
if commit == "" {
|
2019-05-27 18:41:47 +03:00
|
|
|
commit = os.Getenv("TRAVIS_COMMIT")
|
2019-05-08 08:44:28 -05:00
|
|
|
}
|
2016-09-30 22:03:08 +02:00
|
|
|
return Environment{
|
2021-05-04 13:01:20 +02:00
|
|
|
CI: true,
|
2016-09-30 22:03:08 +02:00
|
|
|
Name: "travis",
|
|
|
|
Repo: os.Getenv("TRAVIS_REPO_SLUG"),
|
2019-05-08 08:44:28 -05:00
|
|
|
Commit: commit,
|
|
|
|
Date: getDate(commit),
|
2016-09-30 22:03:08 +02:00
|
|
|
Branch: os.Getenv("TRAVIS_BRANCH"),
|
|
|
|
Tag: os.Getenv("TRAVIS_TAG"),
|
|
|
|
Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"),
|
|
|
|
IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
|
2017-04-06 13:53:33 +03:00
|
|
|
IsCronJob: os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
|
2016-09-30 22:03:08 +02:00
|
|
|
}
|
|
|
|
case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
|
2019-05-08 08:44:28 -05:00
|
|
|
commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT")
|
|
|
|
if commit == "" {
|
2019-05-27 18:41:47 +03:00
|
|
|
commit = os.Getenv("APPVEYOR_REPO_COMMIT")
|
2019-05-08 08:44:28 -05:00
|
|
|
}
|
2016-09-30 22:03:08 +02:00
|
|
|
return Environment{
|
2021-05-04 13:01:20 +02:00
|
|
|
CI: true,
|
2016-09-30 22:03:08 +02:00
|
|
|
Name: "appveyor",
|
|
|
|
Repo: os.Getenv("APPVEYOR_REPO_NAME"),
|
2019-05-08 08:44:28 -05:00
|
|
|
Commit: commit,
|
|
|
|
Date: getDate(commit),
|
2016-09-30 22:03:08 +02:00
|
|
|
Branch: os.Getenv("APPVEYOR_REPO_BRANCH"),
|
2016-10-03 17:19:43 +02:00
|
|
|
Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"),
|
2016-09-30 22:03:08 +02:00
|
|
|
Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"),
|
|
|
|
IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
|
2017-04-06 13:53:33 +03:00
|
|
|
IsCronJob: os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
|
2016-09-30 22:03:08 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return LocalEnv()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalEnv returns build environment metadata gathered from git.
|
|
|
|
func LocalEnv() Environment {
|
|
|
|
env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})
|
2017-11-12 22:51:19 +02:00
|
|
|
|
|
|
|
head := readGitFile("HEAD")
|
2019-05-08 08:44:28 -05:00
|
|
|
if fields := strings.Fields(head); len(fields) == 2 {
|
|
|
|
head = fields[1]
|
2017-11-13 03:00:18 +07:00
|
|
|
} else {
|
2019-08-08 12:05:35 +03:00
|
|
|
// In this case we are in "detached head" state
|
|
|
|
// see: https://git-scm.com/docs/git-checkout#_detached_head
|
|
|
|
// Additional check required to verify, that file contains commit hash
|
|
|
|
commitRe, _ := regexp.Compile("^([0-9a-f]{40})$")
|
|
|
|
if commit := commitRe.FindString(head); commit != "" && env.Commit == "" {
|
|
|
|
env.Commit = commit
|
2024-08-19 13:35:04 +02:00
|
|
|
env.Date = getDate(env.Commit)
|
2019-08-08 12:05:35 +03:00
|
|
|
}
|
2016-09-30 22:03:08 +02:00
|
|
|
return env
|
|
|
|
}
|
|
|
|
if env.Commit == "" {
|
2017-11-12 22:51:19 +02:00
|
|
|
env.Commit = readGitFile(head)
|
2016-09-30 22:03:08 +02:00
|
|
|
}
|
2019-05-08 08:44:28 -05:00
|
|
|
env.Date = getDate(env.Commit)
|
2016-09-30 22:03:08 +02:00
|
|
|
if env.Branch == "" {
|
2017-11-13 03:00:18 +07:00
|
|
|
if head != "HEAD" {
|
2018-02-12 10:18:35 +01:00
|
|
|
env.Branch = strings.TrimPrefix(head, "refs/heads/")
|
2016-10-03 10:22:14 +02:00
|
|
|
}
|
2016-09-30 22:03:08 +02:00
|
|
|
}
|
2017-11-13 03:00:18 +07:00
|
|
|
if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
|
2016-11-27 18:40:28 +01:00
|
|
|
env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
|
2016-11-21 12:44:42 +01:00
|
|
|
}
|
2016-09-30 22:03:08 +02:00
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
2016-11-27 18:40:28 +01:00
|
|
|
func firstLine(s string) string {
|
|
|
|
return strings.Split(s, "\n")[0]
|
|
|
|
}
|
|
|
|
|
2019-05-08 08:44:28 -05:00
|
|
|
func getDate(commit string) string {
|
|
|
|
if commit == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
out := RunGit("show", "-s", "--format=%ct", commit)
|
|
|
|
if out == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to parse git commit date: %v", err))
|
|
|
|
}
|
|
|
|
return time.Unix(date, 0).Format("20060102")
|
|
|
|
}
|
|
|
|
|
2016-09-30 22:03:08 +02:00
|
|
|
func applyEnvFlags(env Environment) Environment {
|
|
|
|
if !flag.Parsed() {
|
|
|
|
panic("you need to call flag.Parse before Env or LocalEnv")
|
|
|
|
}
|
|
|
|
if *GitCommitFlag != "" {
|
|
|
|
env.Commit = *GitCommitFlag
|
|
|
|
}
|
|
|
|
if *GitBranchFlag != "" {
|
|
|
|
env.Branch = *GitBranchFlag
|
|
|
|
}
|
|
|
|
if *GitTagFlag != "" {
|
|
|
|
env.Tag = *GitTagFlag
|
|
|
|
}
|
|
|
|
if *BuildnumFlag != "" {
|
|
|
|
env.Buildnum = *BuildnumFlag
|
|
|
|
}
|
|
|
|
if *PullRequestFlag {
|
|
|
|
env.IsPullRequest = true
|
|
|
|
}
|
2017-04-06 13:53:33 +03:00
|
|
|
if *CronJobFlag {
|
|
|
|
env.IsCronJob = true
|
|
|
|
}
|
2023-08-12 10:32:50 +02:00
|
|
|
if *UbuntuVersionFlag != "" {
|
|
|
|
env.UbuntuVersion = *UbuntuVersionFlag
|
|
|
|
}
|
2016-09-30 22:03:08 +02:00
|
|
|
return env
|
|
|
|
}
|