github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/internal/build/env.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package build 13 14 import ( 15 "flag" 16 "fmt" 17 "os" 18 "strings" 19 ) 20 21 var ( 22 // These flags override values in build env. 23 GitCommitFlag = flag.String("git-commit", "", `Overrides git commit hash embedded into executables`) 24 GitBranchFlag = flag.String("git-branch", "", `Overrides git branch being built`) 25 GitTagFlag = flag.String("git-tag", "", `Overrides git tag being built`) 26 BuildnumFlag = flag.String("buildnum", "", `Overrides CI build number`) 27 PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`) 28 CronJobFlag = flag.Bool("cron-job", false, `Overrides cron job status of the build`) 29 ) 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 IsCronJob bool 39 } 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 46 // Env returns metadata about the current CI environment, falling back to LocalEnv 47 // if not running on CI. 48 func Env() Environment { 49 switch { 50 case os.Getenv("CI") == "true" && os.Getenv("TRAVIS") == "true": 51 return Environment{ 52 Name: "travis", 53 Repo: os.Getenv("TRAVIS_REPO_SLUG"), 54 Commit: os.Getenv("TRAVIS_COMMIT"), 55 Branch: os.Getenv("TRAVIS_BRANCH"), 56 Tag: os.Getenv("TRAVIS_TAG"), 57 Buildnum: os.Getenv("TRAVIS_BUILD_NUMBER"), 58 IsPullRequest: os.Getenv("TRAVIS_PULL_REQUEST") != "false", 59 IsCronJob: os.Getenv("TRAVIS_EVENT_TYPE") == "cron", 60 } 61 case os.Getenv("CI") == "True" && os.Getenv("APPVEYOR") == "True": 62 return Environment{ 63 Name: "appveyor", 64 Repo: os.Getenv("APPVEYOR_REPO_NAME"), 65 Commit: os.Getenv("APPVEYOR_REPO_COMMIT"), 66 Branch: os.Getenv("APPVEYOR_REPO_BRANCH"), 67 Tag: os.Getenv("APPVEYOR_REPO_TAG_NAME"), 68 Buildnum: os.Getenv("APPVEYOR_BUILD_NUMBER"), 69 IsPullRequest: os.Getenv("APPVEYOR_PULL_REQUEST_NUMBER") != "", 70 IsCronJob: os.Getenv("APPVEYOR_SCHEDULED_BUILD") == "True", 71 } 72 default: 73 return LocalEnv() 74 } 75 } 76 77 // LocalEnv returns build environment metadata gathered from git. 78 func LocalEnv() Environment { 79 env := applyEnvFlags(Environment{Name: "local", Repo: "Sberex/go-sberex"}) 80 81 head := readGitFile("HEAD") 82 if splits := strings.Split(head, " "); len(splits) == 2 { 83 head = splits[1] 84 } else { 85 return env 86 } 87 if env.Commit == "" { 88 env.Commit = readGitFile(head) 89 } 90 if env.Branch == "" { 91 if head != "HEAD" { 92 env.Branch = strings.TrimPrefix(head, "refs/heads/") 93 } 94 } 95 if info, err := os.Stat(".git/objects"); err == nil && info.IsDir() && env.Tag == "" { 96 env.Tag = firstLine(RunGit("tag", "-l", "--points-at", "HEAD")) 97 } 98 return env 99 } 100 101 func firstLine(s string) string { 102 return strings.Split(s, "\n")[0] 103 } 104 105 func applyEnvFlags(env Environment) Environment { 106 if !flag.Parsed() { 107 panic("you need to call flag.Parse before Env or LocalEnv") 108 } 109 if *GitCommitFlag != "" { 110 env.Commit = *GitCommitFlag 111 } 112 if *GitBranchFlag != "" { 113 env.Branch = *GitBranchFlag 114 } 115 if *GitTagFlag != "" { 116 env.Tag = *GitTagFlag 117 } 118 if *BuildnumFlag != "" { 119 env.Buildnum = *BuildnumFlag 120 } 121 if *PullRequestFlag { 122 env.IsPullRequest = true 123 } 124 if *CronJobFlag { 125 env.IsCronJob = true 126 } 127 return env 128 }