env.go 4.9 KB

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