github.com/theQRL/go-zond@v0.1.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("CIRCLECI") == "true": 62 commit := os.Getenv("CIRCLE_SHA1") 63 return Environment{ 64 CI: true, 65 Name: "circleci", 66 Repo: os.Getenv("CIRCLE_PROJECT_REPONAME"), 67 Commit: os.Getenv("CIRCLE_SHA1"), 68 Date: getDate(commit), 69 Branch: os.Getenv("CIRCLE_BRANCH"), 70 Tag: os.Getenv("CIRCLE_TAG"), 71 Buildnum: os.Getenv("CIRCLE_BUILD_NUM"), 72 IsPullRequest: os.Getenv("CIRCLE_PR_NUMBER") != "", 73 IsCronJob: false, 74 // IsCronJob: os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True", 75 } 76 77 case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true": 78 commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA") 79 if commit == "" { 80 commit = os.Getenv("TRAVIS_COMMIT") 81 } 82 return Environment{ 83 CI: true, 84 Name: "travis", 85 Repo: os.Getenv("TRAVIS_REPO_SLUG"), 86 Commit: commit, 87 Date: getDate(commit), 88 Branch: os.Getenv("TRAVIS_BRANCH"), 89 Tag: os.Getenv("TRAVIS_TAG"), 90 Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"), 91 IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false", 92 IsCronJob: os.Getenv("TRAVIS_EVENT_TYPE") == "cron", 93 } 94 case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True": 95 commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT") 96 if commit == "" { 97 commit = os.Getenv("APPVEYOR_REPO_COMMIT") 98 } 99 return Environment{ 100 CI: true, 101 Name: "appveyor", 102 Repo: os.Getenv("APPVEYOR_REPO_NAME"), 103 Commit: commit, 104 Date: getDate(commit), 105 Branch: os.Getenv("APPVEYOR_REPO_BRANCH"), 106 Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"), 107 Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"), 108 IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "", 109 IsCronJob: os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True", 110 } 111 default: 112 return LocalEnv() 113 } 114 } 115 116 // LocalEnv returns build environment metadata gathered from git. 117 func LocalEnv() Environment { 118 env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"}) 119 120 head := readGitFile("HEAD") 121 if fields := strings.Fields(head); len(fields) == 2 { 122 head = fields[1] 123 } else { 124 // In this case we are in "detached head" state 125 // see: https://git-scm.com/docs/git-checkout#_detached_head 126 // Additional check required to verify, that file contains commit hash 127 commitRe, _ := regexp.Compile("^([0-9a-f]{40})$") 128 if commit := commitRe.FindString(head); commit != "" && env.Commit == "" { 129 env.Commit = commit 130 } 131 return env 132 } 133 if env.Commit == "" { 134 env.Commit = readGitFile(head) 135 } 136 env.Date = getDate(env.Commit) 137 if env.Branch == "" { 138 if head != "HEAD" { 139 env.Branch = strings.TrimPrefix(head, "refs/heads/") 140 } 141 } 142 if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" { 143 env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD")) 144 } 145 return env 146 } 147 148 func firstLine(s string) string { 149 return strings.Split(s, "\n")[0] 150 } 151 152 func getDate(commit string) string { 153 if commit == "" { 154 return "" 155 } 156 out := RunGit("show", "-s", "--format=%ct", commit) 157 if out == "" { 158 return "" 159 } 160 date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64) 161 if err != nil { 162 panic(fmt.Sprintf("failed to parse git commit date: %v", err)) 163 } 164 return time.Unix(date, 0).Format("20060102") 165 } 166 167 func applyEnvFlags(env Environment) Environment { 168 if !flag.Parsed() { 169 panic("you need to call flag.Parse before Env or LocalEnv") 170 } 171 if *GitCommitFlag != "" { 172 env.Commit = *GitCommitFlag 173 } 174 if *GitBranchFlag != "" { 175 env.Branch = *GitBranchFlag 176 } 177 if *GitTagFlag != "" { 178 env.Tag = *GitTagFlag 179 } 180 if *BuildnumFlag != "" { 181 env.Buildnum = *BuildnumFlag 182 } 183 if *PullRequestFlag { 184 env.IsPullRequest = true 185 } 186 if *CronJobFlag { 187 env.IsCronJob = true 188 } 189 if *UbuntuVersionFlag != "" { 190 env.UbuntuVersion = *UbuntuVersionFlag 191 } 192 return env 193 }