env.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. package build
  17. import (
  18. "flag"
  19. "fmt"
  20. "os"
  21. "regexp"
  22. "strconv"
  23. "strings"
  24. "time"
  25. )
  26. var (
  27. // These flags override values in build env.
  28. GitCommitFlag = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`)
  29. GitBranchFlag = flag.String("git-branch", "", `Overrides git branch being built`)
  30. GitTagFlag = flag.String("git-tag", "", `Overrides git tag being built`)
  31. BuildnumFlag = flag.String("buildnum", "", `Overrides CI build number`)
  32. PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
  33. CronJobFlag = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
  34. )
  35. // Environment contains metadata provided by the build environment.
  36. type Environment struct {
  37. CI bool
  38. Name string // name of the environment
  39. Repo string // name of GitHub repo
  40. Commit, Date, Branch, Tag string // Git info
  41. Buildnum string
  42. IsPullRequest bool
  43. IsCronJob bool
  44. }
  45. func (env Environment) String() string {
  46. return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)",
  47. env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
  48. }
  49. // Env returns metadata about the current CI environment, falling back to LocalEnv
  50. // if not running on CI.
  51. func Env() Environment {
  52. switch {
  53. case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
  54. commit := os.Getenv("TRAVIS_PULL_REQUEST_SHA")
  55. if commit == "" {
  56. commit = os.Getenv("TRAVIS_COMMIT")
  57. }
  58. return Environment{
  59. CI: true,
  60. Name: "travis",
  61. Repo: os.Getenv("TRAVIS_REPO_SLUG"),
  62. Commit: commit,
  63. Date: getDate(commit),
  64. Branch: os.Getenv("TRAVIS_BRANCH"),
  65. Tag: os.Getenv("TRAVIS_TAG"),
  66. Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"),
  67. IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
  68. IsCronJob: os.Getenv("TRAVIS_EVENT_TYPE") == "cron",
  69. }
  70. case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
  71. commit := os.Getenv("APPVEYOR_PULL_REQUEST_HEAD_COMMIT")
  72. if commit == "" {
  73. commit = os.Getenv("APPVEYOR_REPO_COMMIT")
  74. }
  75. return Environment{
  76. CI: true,
  77. Name: "appveyor",
  78. Repo: os.Getenv("APPVEYOR_REPO_NAME"),
  79. Commit: commit,
  80. Date: getDate(commit),
  81. Branch: os.Getenv("APPVEYOR_REPO_BRANCH"),
  82. Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"),
  83. Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"),
  84. IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
  85. IsCronJob: os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True",
  86. }
  87. default:
  88. return LocalEnv()
  89. }
  90. }
  91. // LocalEnv returns build environment metadata gathered from git.
  92. func LocalEnv() Environment {
  93. env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})
  94. head := readGitFile("HEAD")
  95. if fields := strings.Fields(head); len(fields) == 2 {
  96. head = fields[1]
  97. } else {
  98. // In this case we are in "detached head" state
  99. // see: https://git-scm.com/docs/git-checkout#_detached_head
  100. // Additional check required to verify, that file contains commit hash
  101. commitRe, _ := regexp.Compile("^([0-9a-f]{40})$")
  102. if commit := commitRe.FindString(head); commit != "" && env.Commit == "" {
  103. env.Commit = commit
  104. }
  105. return env
  106. }
  107. if env.Commit == "" {
  108. env.Commit = readGitFile(head)
  109. }
  110. env.Date = getDate(env.Commit)
  111. if env.Branch == "" {
  112. if head != "HEAD" {
  113. env.Branch = strings.TrimPrefix(head, "refs/heads/")
  114. }
  115. }
  116. if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" {
  117. env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
  118. }
  119. return env
  120. }
  121. func firstLine(s string) string {
  122. return strings.Split(s, "\n")[0]
  123. }
  124. func getDate(commit string) string {
  125. if commit == "" {
  126. return ""
  127. }
  128. out := RunGit("show", "-s", "--format=%ct", commit)
  129. if out == "" {
  130. return ""
  131. }
  132. date, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
  133. if err != nil {
  134. panic(fmt.Sprintf("failed to parse git commit date: %v", err))
  135. }
  136. return time.Unix(date, 0).Format("20060102")
  137. }
  138. func applyEnvFlags(env Environment) Environment {
  139. if !flag.Parsed() {
  140. panic("you need to call flag.Parse before Env or LocalEnv")
  141. }
  142. if *GitCommitFlag != "" {
  143. env.Commit = *GitCommitFlag
  144. }
  145. if *GitBranchFlag != "" {
  146. env.Branch = *GitBranchFlag
  147. }
  148. if *GitTagFlag != "" {
  149. env.Tag = *GitTagFlag
  150. }
  151. if *BuildnumFlag != "" {
  152. env.Buildnum = *BuildnumFlag
  153. }
  154. if *PullRequestFlag {
  155. env.IsPullRequest = true
  156. }
  157. if *CronJobFlag {
  158. env.IsCronJob = true
  159. }
  160. return env
  161. }