github.com/jimmyx0x/go-ethereum@v1.10.28/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 ) 38 39 // Environment contains metadata provided by the build environment. 40 type Environment struct { 41 CI bool 42 Name string // name of the environment 43 Repo string // name of GitHub repo 44 Commit, Date, Branch, Tag string // Git info 45 Buildnum string 46 IsPullRequest bool 47 IsCronJob bool 48 } 49 50 func (env Environment) String() string { 51 return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)", 52 env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest) 53 } 54 55 // Env returns metadata about the current CI environment, falling back to LocalEnv 56 // if not running on CI. 57 func Env() Environment { 58 switch { 59 case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true": 60 commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA") 61 if commit == "" { 62 commit = os.Getenv("TRAVIS_COMMIT") 63 } 64 return Environment{ 65 CI: true, 66 Name: "travis", 67 Repo: os.Getenv("TRAVIS_REPO_SLUG"), 68 Commit: commit, 69 Date: getDate(commit), 70 Branch: os.Getenv("TRAVIS_BRANCH"), 71 Tag: os.Getenv("TRAVIS_TAG"), 72 Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"), 73 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 commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT") 78 if commit == "" { 79 commit = os.Getenv("APPVEYOR_REPO_COMMIT") 80 } 81 return Environment{ 82 CI: true, 83 Name: "appveyor", 84 Repo: os.Getenv("APPVEYOR_REPO_NAME"), 85 Commit: commit, 86 Date: getDate(commit), 87 Branch: os.Getenv("APPVEYOR_REPO_BRANCH"), 88 Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"), 89 Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"), 90 IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "", 91 IsCronJob: os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True", 92 } 93 default: 94 return LocalEnv() 95 } 96 } 97 98 // LocalEnv returns build environment metadata gathered from git. 99 func LocalEnv() Environment { 100 env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"}) 101 102 head := readGitFile("HEAD") 103 if fields := strings.Fields(head); len(fields) == 2 { 104 head = fields[1] 105 } else { 106 // In this case we are in "detached head" state 107 // see: https://git-scm.com/docs/git-checkout#_detached_head 108 // Additional check required to verify, that file contains commit hash 109 commitRe, _ := regexp.Compile("^([0-9a-f]{40})$") 110 if commit := commitRe.FindString(head); commit != "" && env.Commit == "" { 111 env.Commit = commit 112 } 113 return env 114 } 115 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 return env 128 } 129 130 func firstLine(s string) string { 131 return strings.Split(s, "\n")[0] 132 } 133 134 func getDate(commit string) string { 135 if commit == "" { 136 return "" 137 } 138 out := RunGit("show", "-s", "--format=%ct", commit) 139 if out == "" { 140 return "" 141 } 142 date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64) 143 if err != nil { 144 panic(fmt.Sprintf("failed to parse git commit date: %v", err)) 145 } 146 return time.Unix(date, 0).Format("20060102") 147 } 148 149 func applyEnvFlags(env Environment) Environment { 150 if !flag.Parsed() { 151 panic("you need to call flag.Parse before Env or LocalEnv") 152 } 153 if *GitCommitFlag != "" { 154 env.Commit = *GitCommitFlag 155 } 156 if *GitBranchFlag != "" { 157 env.Branch = *GitBranchFlag 158 } 159 if *GitTagFlag != "" { 160 env.Tag = *GitTagFlag 161 } 162 if *BuildnumFlag != "" { 163 env.Buildnum = *BuildnumFlag 164 } 165 if *PullRequestFlag { 166 env.IsPullRequest = true 167 } 168 if *CronJobFlag { 169 env.IsCronJob = true 170 } 171 return env 172 }