-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_gitlab.go
62 lines (50 loc) · 1.72 KB
/
env_gitlab.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
package beluga
import (
"net/url"
"regexp"
)
// See https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
var gitLabVarMatcher = regexp.MustCompile(`(?i)\$(\$|([a-z0-9_\-]+)|{([a-z0-9_\-]+)})`)
func gitlabEnvRead(e Environment) error {
if e["GITLAB_CI"] == "" {
return nil
}
if e[varDefaultBranch] == "" {
e[varDefaultBranch] = e["CI_DEFAULT_BRANCH"]
}
var environment = envReview
env := parseVersion(e["CI_COMMIT_TAG"])
if env[varVersion] != "" {
// A version has been tagged
environment = envProduction
} else if e["CI_COMMIT_REF_NAME"] == e[varDefaultBranch] {
environment = envStaging
}
var stackName string
if e["CI_PROJECT_PATH_SLUG"] != "" && e["CI_ENVIRONMENT_SLUG"] != "" {
stackName = e["CI_PROJECT_PATH_SLUG"] + "-" + e["CI_ENVIRONMENT_SLUG"]
}
env.MergeMissing(Environment{
varEnvironment: environment,
varRegistry: e["CI_REGISTRY"],
varRegistryUsername: e.Get("CI_REGISTRY_USER", "gitlab-ci-token"),
varRegistryPassword: e["CI_REGISTRY_PASSWORD"],
varStackName: stackName,
varImagesTemplate: `{{.Env.CI_REGISTRY_IMAGE}}:{{if .Env.CI_COMMIT_TAG}}{{.Env.CI_COMMIT_TAG}} {{.Env.CI_REGISTRY_IMAGE}}:latest{{else}}{{.Env.CI_COMMIT_REF_NAME}}{{end}}`,
})
e.MergeMissing(env)
// We'll do domain last, so it can include all other vars up to this point.
domain := gitlabDomain(e)
e.MergeMissing(Environment{varDomain: domain})
return nil
}
func gitlabDomain(e Environment) string {
// If a global variable is used for the job>environment>url field, its
// variables will not be expanded as expected. This works around that.
s := e.expand(gitLabVarMatcher, e["CI_ENVIRONMENT_URL"])
u, err := url.Parse(s)
if err != nil {
return ""
}
return u.Hostname()
}