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