env.go 5.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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"
23
	"regexp"
24
	"strconv"
25
	"strings"
26
	"time"
27 28 29 30 31 32 33 34 35
)

var (
	// These flags override values in build env.
	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`)
36
	CronJobFlag     = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
37 38 39 40
)

// Environment contains metadata provided by the build environment.
type Environment struct {
41
	CI                        bool
42 43 44 45 46 47
	Name                      string // name of the environment
	Repo                      string // name of GitHub repo
	Commit, Date, Branch, Tag string // Git info
	Buildnum                  string
	IsPullRequest             bool
	IsCronJob                 bool
48 49 50
}

func (env Environment) String() string {
51 52
	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)
53 54 55 56 57 58 59
}

// 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":
60 61
		commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA")
		if commit == "" {
62
			commit = os.Getenv("TRAVIS_COMMIT")
63
		}
64
		return Environment{
65
			CI:            true,
66 67
			Name:          "travis",
			Repo:          os.Getenv("TRAVIS_REPO_SLUG"),
68 69
			Commit:        commit,
			Date:          getDate(commit),
70 71 72 73
			Branch:        os.Getenv("TRAVIS_BRANCH"),
			Tag:           os.Getenv("TRAVIS_TAG"),
			Buildnum:      os.Getenv("TRAVIS_BUILD_NUMBER"),
			IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
74
			IsCronJob:     os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
75 76
		}
	case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
77 78
		commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT")
		if commit == "" {
79
			commit = os.Getenv("APPVEYOR_REPO_COMMIT")
80
		}
81
		return Environment{
82
			CI:            true,
83 84
			Name:          "appveyor",
			Repo:          os.Getenv("APPVEYOR_REPO_NAME"),
85 86
			Commit:        commit,
			Date:          getDate(commit),
87
			Branch:        os.Getenv("APPVEYOR_REPO_BRANCH"),
88
			Tag:           os.Getenv("APPVEYOR_REPO_TAG_NAME"),
89 90
			Buildnum:      os.Getenv("APPVEYOR_BUILD_NUMBER"),
			IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
91
			IsCronJob:     os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
92 93 94 95 96 97 98 99 100
		}
	default:
		return LocalEnv()
	}
}

// LocalEnv returns build environment metadata gathered from git.
func LocalEnv() Environment {
	env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})
101 102

	head := readGitFile("HEAD")
103 104
	if fields := strings.Fields(head); len(fields) == 2 {
		head = fields[1]
105
	} else {
106 107 108 109 110 111 112
		// 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
		}
113 114 115
		return env
	}
	if env.Commit == "" {
116
		env.Commit = readGitFile(head)
117
	}
118
	env.Date = getDate(env.Commit)
119
	if env.Branch == "" {
120
		if head != "HEAD" {
121
			env.Branch = strings.TrimPrefix(head, "refs/heads/")
122
		}
123
	}
124
	if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
125
		env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
126
	}
127 128 129
	return env
}

130 131 132 133
func firstLine(s string) string {
	return strings.Split(s, "\n")[0]
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
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")
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
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
	}
168 169 170
	if *CronJobFlag {
		env.IsCronJob = true
	}
171 172
	return env
}