env.go 5.3 KB

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