-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvvars.go
107 lines (95 loc) · 3.02 KB
/
envvars.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Package metadata provides access to the metadata provided by concourse in the environment of
// the resources.
//
// See https://concourse-ci.org/implementing-resource-types.html#resource-metadata
package metadata
import (
"encoding/json"
"os"
)
// BuildID returns the ID of the current build
func BuildID() string {
return os.Getenv("BUILD_ID")
}
// BuildName returns the name of the current build
func BuildName() string {
return os.Getenv("BUILD_NAME")
}
// BuildJobName returns the job name of the current build
func BuildJobName() string {
return os.Getenv("BUILD_JOB_NAME")
}
// BuildPipelineName returns the pipeline name of the current build
func BuildPipelineName() string {
return os.Getenv("BUILD_PIPELINE_NAME")
}
// BuildPipelineInstanceVarsRaw returns the instance variables for the current pipeline as string
func BuildPipelineInstanceVarsRaw() string {
return os.Getenv("BUILD_PIPELINE_INSTANCE_VARS")
}
// BuildPipelineInstanceVars returns the instance variables for the current pipeline as a map
func BuildPipelineInstanceVars() map[string]any {
res := make(map[string]any)
// we ignore any error here. As we trust concourse to deliver correct json, and if it does not
// there is nothing we can do.
_ = json.Unmarshal([]byte(BuildPipelineInstanceVarsRaw()), &res)
return res
}
// BuildTeamName returns the name of the current team
func BuildTeamName() string {
return os.Getenv("BUILD_TEAM_NAME")
}
// BuildCreatedBy returns the username that created the build
func BuildCreatedBy() string {
return os.Getenv("BUILD_CREATED_BY")
}
// ConcourseURL returns the external URL of the concourse instance. The original variable name is `ATC_EXTERNAL_URL`
func ConcourseURL() string {
return os.Getenv("ATC_EXTERNAL_URL")
}
// BuildURL returns the full URL to the build
func BuildURL() string {
return os.ExpandEnv("${ATC_EXTERNAL_URL}/teams/${BUILD_TEAM_NAME}/pipelines/${BUILD_PIPELINE_NAME}" +
"/jobs/${BUILD_JOB_NAME}/builds/${BUILD_NAME}")
}
// ExpandEnv is mainly a wrapper around os.ExpandEnv, but limited to the
// variables that are provided by concourse.
//
// The following variables are expanded
// - BUILD_ID
// - BUILD_NAME
// - BUILD_JOB_NAME
// - BUILD_PIPELINE_NAME
// - BUILD_PIPELINE_INSTANCE_VARS
// - BUILD_TEAM_NAME
// - BUILD_CREATED_BY
// - ATC_EXTERNAL_URL (CONCOURSE_URL is also supported)
// - BUILD_URL (see BuildURL)
//
// See https://concourse-ci.org/implementing-resource-types.html#resource-metadata
func ExpandEnv(s string) string {
return os.Expand(s, func(s string) string {
switch s {
case "BUILD_ID":
return BuildID()
case "BUILD_NAME":
return BuildName()
case "BUILD_JOB_NAME":
return BuildJobName()
case "BUILD_PIPELINE_NAME":
return BuildPipelineName()
case "BUILD_PIPELINE_INSTANCE_VARS":
return BuildPipelineInstanceVarsRaw()
case "BUILD_TEAM_NAME":
return BuildTeamName()
case "BUILD_CREATED_BY":
return BuildCreatedBy()
case "ATC_EXTERNAL_URL", "CONCOURSE_URL":
return ConcourseURL()
case "BUILD_URL":
return BuildURL()
default:
return ""
}
})
}