go.undefinedlabs.com/scopeagent@v0.4.2/agent/ci.go (about) 1 package agent 2 3 import ( 4 "fmt" 5 "os" 6 "regexp" 7 "strings" 8 9 "go.undefinedlabs.com/scopeagent/tags" 10 ) 11 12 var branchRefRegex = regexp.MustCompile(`(?m)^refs\/heads\/(.*)|refs\/(.*)$`) 13 14 func getCIMetadata() map[string]interface{} { 15 ciMetadata := map[string]interface{}{tags.CI: false} 16 17 if _, set := os.LookupEnv("TRAVIS"); set { 18 ciMetadata[tags.CI] = true 19 ciMetadata[tags.CIProvider] = "Travis" 20 ciMetadata[tags.CIBuildId] = os.Getenv("TRAVIS_BUILD_ID") 21 ciMetadata[tags.CIBuildNumber] = os.Getenv("TRAVIS_BUILD_NUMBER") 22 ciMetadata[tags.CIBuildUrl] = os.Getenv("TRAVIS_BUILD_WEB_URL") 23 ciMetadata[tags.Repository] = fmt.Sprintf( 24 "https://github.com/%s.git", 25 os.Getenv("TRAVIS_REPO_SLUG"), 26 ) 27 ciMetadata[tags.Commit] = os.Getenv("TRAVIS_COMMIT") 28 if branch, ok := os.LookupEnv("TRAVIS_PULL_REQUEST_BRANCH"); ok && branch != "" { 29 ciMetadata[tags.Branch] = branch 30 } else { 31 ciMetadata[tags.Branch] = os.Getenv("TRAVIS_BRANCH") 32 } 33 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("TRAVIS_BUILD_DIR") 34 } else if _, set := os.LookupEnv("CIRCLECI"); set { 35 ciMetadata[tags.CI] = true 36 ciMetadata[tags.CIProvider] = "CircleCI" 37 ciMetadata[tags.CIBuildNumber] = os.Getenv("CIRCLE_BUILD_NUM") 38 ciMetadata[tags.CIBuildUrl] = os.Getenv("CIRCLE_BUILD_URL") 39 ciMetadata[tags.Repository] = os.Getenv("CIRCLE_REPOSITORY_URL") 40 ciMetadata[tags.Commit] = os.Getenv("CIRCLE_SHA1") 41 ciMetadata[tags.Branch] = os.Getenv("CIRCLE_BRANCH") 42 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("CIRCLE_WORKING_DIRECTORY") 43 } else if _, set := os.LookupEnv("JENKINS_URL"); set { 44 ciMetadata[tags.CI] = true 45 ciMetadata[tags.CIProvider] = "Jenkins" 46 ciMetadata[tags.CIBuildId] = os.Getenv("BUILD_ID") 47 ciMetadata[tags.CIBuildNumber] = os.Getenv("BUILD_NUMBER") 48 ciMetadata[tags.CIBuildUrl] = os.Getenv("BUILD_URL") 49 ciMetadata[tags.Repository] = os.Getenv("GIT_URL") 50 ciMetadata[tags.Commit] = os.Getenv("GIT_COMMIT") 51 branch := os.Getenv("GIT_BRANCH") 52 if strings.Index(branch, "origin/") == 0 { 53 // Removes the origin/ prefix 54 branch = branch[7:] 55 } 56 ciMetadata[tags.Branch] = branch 57 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("WORKSPACE") 58 } else if _, set := os.LookupEnv("GITLAB_CI"); set { 59 ciMetadata[tags.CI] = true 60 ciMetadata[tags.CIProvider] = "gitLab" 61 ciMetadata[tags.CIBuildId] = os.Getenv("CI_JOB_ID") 62 ciMetadata[tags.CIBuildUrl] = os.Getenv("CI_JOB_URL") 63 ciMetadata[tags.Repository] = os.Getenv("CI_REPOSITORY_URL") 64 ciMetadata[tags.Commit] = os.Getenv("CI_COMMIT_SHA") 65 if branch, ok := os.LookupEnv("CI_COMMIT_BRANCH"); ok && branch != "" { 66 ciMetadata[tags.Branch] = branch 67 } else { 68 ciMetadata[tags.Branch] = os.Getenv("CI_COMMIT_REF_NAME") 69 } 70 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("CI_PROJECT_DIR") 71 } else if _, set := os.LookupEnv("APPVEYOR"); set { 72 buildId := os.Getenv("APPVEYOR_BUILD_ID") 73 ciMetadata[tags.CI] = true 74 ciMetadata[tags.CIProvider] = "AppVeyor" 75 ciMetadata[tags.CIBuildId] = buildId 76 ciMetadata[tags.CIBuildNumber] = os.Getenv("APPVEYOR_BUILD_NUMBER") 77 ciMetadata[tags.CIBuildUrl] = fmt.Sprintf( 78 "https://ci.appveyor.com/project/%s/builds/%s", 79 os.Getenv("APPVEYOR_PROJECT_SLUG"), 80 buildId, 81 ) 82 ciMetadata[tags.Repository] = os.Getenv("APPVEYOR_REPO_NAME") 83 ciMetadata[tags.Commit] = os.Getenv("APPVEYOR_REPO_COMMIT") 84 if branch, ok := os.LookupEnv("APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH"); ok && branch != "" { 85 ciMetadata[tags.Branch] = branch 86 } else { 87 ciMetadata[tags.Branch] = os.Getenv("APPVEYOR_REPO_BRANCH") 88 } 89 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("APPVEYOR_BUILD_FOLDER") 90 } else if _, set := os.LookupEnv("TF_BUILD"); set { 91 buildId := os.Getenv("Build.BuildId") 92 ciMetadata[tags.CI] = true 93 ciMetadata[tags.CIProvider] = "Azure Pipelines" 94 ciMetadata[tags.CIBuildId] = buildId 95 ciMetadata[tags.CIBuildNumber] = os.Getenv("Build.BuildNumber") 96 ciMetadata[tags.CIBuildUrl] = fmt.Sprintf( 97 "%s/%s/_build/results?buildId=%s&_a=summary", 98 os.Getenv("System.TeamFoundationCollectionUri"), 99 os.Getenv("System.TeamProject"), 100 buildId, 101 ) 102 ciMetadata[tags.Repository] = os.Getenv("Build.Repository.Uri") 103 ciMetadata[tags.Commit] = os.Getenv("Build.SourceVersion") 104 if branch, ok := os.LookupEnv("Build.SourceBranchName"); ok && branch != "" { 105 ciMetadata[tags.Branch] = branch 106 } else { 107 ciMetadata[tags.Branch] = os.Getenv("Build.SourceBranch") 108 } 109 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("Build.SourcesDirectory") 110 } else if sha, set := os.LookupEnv("BITBUCKET_COMMIT"); set { 111 ciMetadata[tags.CI] = true 112 ciMetadata[tags.CIProvider] = "Bitbucket Pipelines" 113 ciMetadata[tags.CIBuildNumber] = os.Getenv("BITBUCKET_BUILD_NUMBER") 114 ciMetadata[tags.Repository] = os.Getenv("BITBUCKET_GIT_SSH_ORIGIN") 115 ciMetadata[tags.Commit] = sha 116 ciMetadata[tags.Branch] = os.Getenv("BITBUCKET_BRANCH") 117 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("BITBUCKET_CLONE_DIR") 118 } else if sha, set := os.LookupEnv("GITHUB_SHA"); set { 119 repo := os.Getenv("GITHUB_REPOSITORY") 120 ciMetadata[tags.CI] = true 121 ciMetadata[tags.CIProvider] = "GitHub" 122 ciMetadata[tags.CIBuildUrl] = fmt.Sprintf( 123 "https://github.com/%s/commit/%s/checks", 124 repo, 125 sha, 126 ) 127 ciMetadata[tags.Repository] = fmt.Sprintf( 128 "https://github.com/%s.git", 129 repo, 130 ) 131 ciMetadata[tags.Commit] = sha 132 ciMetadata[tags.Branch] = os.Getenv("GITHUB_REF") 133 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("GITHUB_WORKSPACE") 134 ciMetadata[tags.CIBuildId] = os.Getenv("GITHUB_RUN_ID") 135 ciMetadata[tags.CIBuildNumber] = os.Getenv("GITHUB_RUN_NUMBER") 136 } else if _, set := os.LookupEnv("TEAMCITY_VERSION"); set { 137 buildId := os.Getenv("BUILD_ID") 138 ciMetadata[tags.CI] = true 139 ciMetadata[tags.CIProvider] = "TeamCity" 140 ciMetadata[tags.Repository] = os.Getenv("BUILD_VCS_URL") 141 ciMetadata[tags.Commit] = os.Getenv("BUILD_VCS_NUMBER") 142 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("BUILD_CHECKOUTDIR") 143 ciMetadata[tags.CIBuildId] = buildId 144 ciMetadata[tags.CIBuildNumber] = os.Getenv("BUILD_NUMBER") 145 ciMetadata[tags.CIBuildUrl] = fmt.Sprintf( 146 "%s/viewLog.html?buildId=%s", 147 os.Getenv("SERVER_URL"), 148 buildId, 149 ) 150 } else if _, set := os.LookupEnv("BUILDKITE"); set { 151 ciMetadata[tags.CI] = true 152 ciMetadata[tags.CIProvider] = "Buildkite" 153 ciMetadata[tags.CIBuildId] = os.Getenv("BUILDKITE_BUILD_ID") 154 ciMetadata[tags.CIBuildNumber] = os.Getenv("BUILDKITE_BUILD_NUMBER") 155 ciMetadata[tags.CIBuildUrl] = os.Getenv("BUILDKITE_BUILD_URL") 156 ciMetadata[tags.Repository] = os.Getenv("BUILDKITE_REPO") 157 ciMetadata[tags.Commit] = os.Getenv("BUILDKITE_COMMIT") 158 ciMetadata[tags.Branch] = os.Getenv("BUILDKITE_BRANCH") 159 ciMetadata[tags.SourceRoot] = getSourceRootFromEnv("BUILDKITE_BUILD_CHECKOUT_PATH") 160 } 161 162 if branchValue, ok := ciMetadata[tags.Branch]; ok { 163 match := branchRefRegex.FindStringSubmatch(branchValue.(string)) 164 if len(match) == 3 { 165 if len(match[1]) > 0 { 166 ciMetadata[tags.Branch] = match[1] 167 } else { 168 ciMetadata[tags.Branch] = match[2] 169 } 170 } 171 } 172 173 return ciMetadata 174 }