env.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. )
  31. // Environment contains metadata provided by the build environment.
  32. type Environment struct {
  33. Name string // name of the environment
  34. Repo string // name of GitHub repo
  35. Commit, Branch, Tag string // Git info
  36. Buildnum string
  37. IsPullRequest bool
  38. }
  39. func (env Environment) String() string {
  40. return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t)",
  41. env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
  42. }
  43. // Env returns metadata about the current CI environment, falling back to LocalEnv
  44. // if not running on CI.
  45. func Env() Environment {
  46. switch {
  47. case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true":
  48. return Environment{
  49. Name: "travis",
  50. Repo: os.Getenv("TRAVIS_REPO_SLUG"),
  51. Commit: os.Getenv("TRAVIS_COMMIT"),
  52. Branch: os.Getenv("TRAVIS_BRANCH"),
  53. Tag: os.Getenv("TRAVIS_TAG"),
  54. Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"),
  55. IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false",
  56. }
  57. case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True":
  58. return Environment{
  59. Name: "appveyor",
  60. Repo: os.Getenv("APPVEYOR_REPO_NAME"),
  61. Commit: os.Getenv("APPVEYOR_REPO_COMMIT"),
  62. Branch: os.Getenv("APPVEYOR_REPO_BRANCH"),
  63. Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"),
  64. Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"),
  65. IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "",
  66. }
  67. default:
  68. return LocalEnv()
  69. }
  70. }
  71. // LocalEnv returns build environment metadata gathered from git.
  72. func LocalEnv() Environment {
  73. env := applyEnvFlags(Environment{Name: "local", Repo: "ethereum/go-ethereum"})
  74. if _, err := os.Stat(".git"); err != nil {
  75. return env
  76. }
  77. if env.Commit == "" {
  78. env.Commit = RunGit("rev-parse", "HEAD")
  79. }
  80. if env.Branch == "" {
  81. if b := RunGit("rev-parse", "--abbrev-ref", "HEAD"); b != "HEAD" {
  82. env.Branch = b
  83. }
  84. }
  85. if env.Tag == "" {
  86. env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD"))
  87. }
  88. return env
  89. }
  90. func firstLine(s string) string {
  91. return strings.Split(s, "\n")[0]
  92. }
  93. func applyEnvFlags(env Environment) Environment {
  94. if !flag.Parsed() {
  95. panic("you need to call flag.Parse before Env or LocalEnv")
  96. }
  97. if *GitCommitFlag != "" {
  98. env.Commit = *GitCommitFlag
  99. }
  100. if *GitBranchFlag != "" {
  101. env.Branch = *GitBranchFlag
  102. }
  103. if *GitTagFlag != "" {
  104. env.Tag = *GitTagFlag
  105. }
  106. if *BuildnumFlag != "" {
  107. env.Buildnum = *BuildnumFlag
  108. }
  109. if *PullRequestFlag {
  110. env.IsPullRequest = true
  111. }
  112. return env
  113. }