jenkins.go 1.56 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Package jenkins implements some helper functions to use during
// tests. Many times certain facilities are not available, or tests
// must run differently.
package jenkins

import (
	"os"
	"strings"
)

// EnvVar is a type to use travis-only env var names with
// the type system.
type EnvVar string

// Environment variables that Jenkins uses.
const (
	VarBuildNumber    EnvVar = "BUILD_NUMBER"
	VarBuildId        EnvVar = "BUILD_ID"
	VarBuildUrl       EnvVar = "BUILD_URL"
	VarNodeName       EnvVar = "NODE_NAME"
	VarJobName        EnvVar = "JOB_NAME"
	VarBuildTag       EnvVar = "BUILD_TAG"
	VarJenkinsUrl     EnvVar = "JENKINS_URL"
	VarExecutorNumber EnvVar = "EXECUTOR_NUMBER"
	VarJavaHome       EnvVar = "JAVA_HOME"
	VarWorkspace      EnvVar = "WORKSPACE"
	VarSvnRevision    EnvVar = "SVN_REVISION"
	VarCvsBranch      EnvVar = "CVS_BRANCH"
	VarGitCommit      EnvVar = "GIT_COMMIT"
	VarGitUrl         EnvVar = "GIT_URL"
	VarGitBranch      EnvVar = "GIT_BRANCH"
)

// IsRunning attempts to determine whether this process is
// running on Jenkins CI. This is done by checking any of the
// following:
//
//  JENKINS_URL is set
//  BuildTag has prefix "jenkins-"
//
func IsRunning() bool {
	return len(Env(VarJenkinsUrl)) > 0 || strings.HasPrefix(Env(VarBuildTag), "jenkins-")
}

// Env returns the value of a travis env variable.
func Env(v EnvVar) string {
	return os.Getenv(string(v))
}

// JobName returns the jenkins JOB_NAME of this build.
func JobName() string {
	return Env(VarJobName)
}

// BuildTag returns the jenkins BUILD_TAG.
func BuildTag() string {
	return Env(VarBuildTag)
}