env.go 3.8 KB

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