env.go 4.2 KB

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