github.com/ethereum/go-ethereum@v1.16.1/internal/build/env.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package build 18 19 import ( 20 "flag" 21 "fmt" 22 "os" 23 "regexp" 24 "strconv" 25 "strings" 26 "time" 27 ) 28 29 var ( 30 // These flags override values in build env. 31 GitCommitFlag = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`) 32 GitBranchFlag = flag.String("git-branch", "", `Overrides git branch being built`) 33 GitTagFlag = flag.String("git-tag", "", `Overrides git tag being built`) 34 BuildnumFlag = flag.String("buildnum", "", `Overrides CI build number`) 35 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 UbuntuVersionFlag = flag.String("ubuntu", "", `Sets the ubuntu version being built for`) 38 ) 39 40 // Environment contains metadata provided by the build environment. 41 type Environment struct { 42 CI bool 43 Name string // name of the environment 44 Repo string // name of GitHub repo 45 Commit, Date, Branch, Tag string // Git info 46 Buildnum string 47 UbuntuVersion string // Ubuntu version being built for 48 IsPullRequest bool 49 IsCronJob bool 50 } 51 52 func (env Environment) String() string { 53 return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)", 54 env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest) 55 } 56 57 // Env returns metadata about the current CI environment, falling back to LocalEnv 58 // if not running on CI. 59 func Env() Environment { 60 switch { 61 case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true": 62 commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA") 63 if commit == "" { 64 commit = os.Getenv("TRAVIS_COMMIT") 65 } 66 return Environment{ 67 CI: true, 68 Name: "travis", 69 Repo: os.Getenv("TRAVIS_REPO_SLUG"), 70 Commit: commit, 71 Date: getDate(commit), 72 Branch: os.Getenv("TRAVIS_BRANCH"), 73 Tag: os.Getenv("TRAVIS_TAG"), 74 Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"), 75 IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false", 76 IsCronJob: os.Getenv("TRAVIS_EVENT_TYPE") == "cron", 77 } 78 case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True": 79 commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT") 80 if commit == "" { 81 commit = os.Getenv("APPVEYOR_REPO_COMMIT") 82 } 83 return Environment{ 84 CI: true, 85 Name: "appveyor", 86 Repo: os.Getenv("APPVEYOR_REPO_NAME"), 87 Commit: commit, 88 Date: getDate(commit), 89 Branch: os.Getenv("APPVEYOR_REPO_BRANCH"), 90 Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"), 91 Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"), 92 IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "", 93 IsCronJob: os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True", 94 } 95 case os.Getenv("CI") == "true" && os.Getenv("GITHUB_ACTIONS") == "true": 96 commit := os.Getenv("GITHUB_SHA") 97 reftype := os.Getenv("GITHUB_REF_TYPE") 98 isPR := os.Getenv("GITHUB_HEAD_REF") != "" 99 tag := "" 100 branch := "" 101 switch { 102 case isPR: 103 branch = os.Getenv("GITHUB_BASE_REF") 104 case reftype == "branch": 105 branch = os.Getenv("GITHUB_REF_NAME") 106 case reftype == "tag": 107 tag = os.Getenv("GITHUB_REF_NAME") 108 } 109 return Environment{ 110 CI: true, 111 Name: "github-actions", 112 Repo: os.Getenv("GITHUB_REPOSITORY"), 113 Commit: commit, 114 Date: getDate(commit), 115 Branch: branch, 116 Tag: tag, 117 IsPullRequest: isPR, 118 Buildnum: os.Getenv("GITHUB_RUN_ID"), 119 IsCronJob: os.Getenv("GITHUB_EVENT_NAME") == "schedule", 120 } 121 122 default: 123 return LocalEnv() 124 } 125 } 126 127 // LocalEnv returns build environment metadata gathered from git. 128 func LocalEnv() Environment { 129 env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"}) 130 131 head := readGitFile("HEAD") 132 if fields := strings.Fields(head); len(fields) == 2 { 133 head = fields[1] 134 } else { 135 // In this case we are in "detached head" state 136 // see: https://git-scm.com/docs/git-checkout#_detached_head 137 // Additional check required to verify, that file contains commit hash 138 commitRe, _ := regexp.Compile("^([0-9a-f]{40})$") 139 if commit := commitRe.FindString(head); commit != "" && env.Commit == "" { 140 env.Commit = commit 141 env.Date = getDate(env.Commit) 142 } 143 return env 144 } 145 if env.Commit == "" { 146 env.Commit = readGitFile(head) 147 } 148 env.Date = getDate(env.Commit) 149 if env.Branch == "" { 150 if head != "HEAD" { 151 env.Branch = strings.TrimPrefix(head, "refs/heads/") 152 } 153 } 154 if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" { 155 env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD")) 156 } 157 return env 158 } 159 160 func firstLine(s string) string { 161 return strings.Split(s, "\n")[0] 162 } 163 164 func getDate(commit string) string { 165 if commit == "" { 166 return "" 167 } 168 out := RunGit("show", "-s", "--format=%ct", commit) 169 if out == "" { 170 return "" 171 } 172 date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64) 173 if err != nil { 174 panic(fmt.Sprintf("failed to parse git commit date: %v", err)) 175 } 176 return time.Unix(date, 0).Format("20060102") 177 } 178 179 func applyEnvFlags(env Environment) Environment { 180 if !flag.Parsed() { 181 panic("you need to call flag.Parse before Env or LocalEnv") 182 } 183 if *GitCommitFlag != "" { 184 env.Commit = *GitCommitFlag 185 } 186 if *GitBranchFlag != "" { 187 env.Branch = *GitBranchFlag 188 } 189 if *GitTagFlag != "" { 190 env.Tag = *GitTagFlag 191 } 192 if *BuildnumFlag != "" { 193 env.Buildnum = *BuildnumFlag 194 } 195 if *PullRequestFlag { 196 env.IsPullRequest = true 197 } 198 if *CronJobFlag { 199 env.IsCronJob = true 200 } 201 if *UbuntuVersionFlag != "" { 202 env.UbuntuVersion = *UbuntuVersionFlag 203 } 204 return env 205 }