Merge pull request #90 from drone-plugins/feature/add_proxy_values

Adding proxy values to build args
This commit is contained in:
Bugagazavr 2016-10-31 22:46:26 +02:00 committed by GitHub
commit 916c3279f2

View File

@ -108,6 +108,9 @@ func (p Plugin) Exec() error {
fmt.Println("Registry credentials not provided. Guest mode enabled.") fmt.Println("Registry credentials not provided. Guest mode enabled.")
} }
// add proxy build args
addProxyBuildArgs(&p.Build)
var cmds []*exec.Cmd var cmds []*exec.Cmd
cmds = append(cmds, commandVersion()) // docker version cmds = append(cmds, commandVersion()) // docker version
cmds = append(cmds, commandInfo()) // docker info cmds = append(cmds, commandInfo()) // docker info
@ -180,6 +183,7 @@ func commandBuild(build Build) *exec.Cmd {
"-f", build.Dockerfile, "-f", build.Dockerfile,
"-t", build.Name, "-t", build.Name,
) )
for _, arg := range build.Args { for _, arg := range build.Args {
cmd.Args = append(cmd.Args, "--build-arg", arg) cmd.Args = append(cmd.Args, "--build-arg", arg)
} }
@ -187,6 +191,49 @@ func commandBuild(build Build) *exec.Cmd {
return cmd return cmd
} }
// helper function to add proxy values from the environment
func addProxyBuildArgs(build *Build) {
addProxyValue(build, "http_proxy")
addProxyValue(build, "https_proxy")
addProxyValue(build, "no_proxy")
}
// helper function to add the upper and lower case version of a proxy value.
func addProxyValue(build *Build, key string) {
value := getProxyValue(key)
if len(value) > 0 && !hasProxyBuildArg(build, key) {
build.Args = append(build.Args, fmt.Sprintf("%s=%s", key, value))
build.Args = append(build.Args, fmt.Sprintf("%s=%s", strings.ToUpper(key), value))
}
}
// helper function to get a proxy value from the environment.
//
// assumes that the upper and lower case versions of are the same.
func getProxyValue(key string) string {
value := os.Getenv(key)
if len(value) > 0 {
return value
}
return os.Getenv(strings.ToUpper(key))
}
// helper function that looks to see if a proxy value was set in the build args.
func hasProxyBuildArg(build *Build, key string) bool {
keyUpper := strings.ToUpper(key)
for _, s := range build.Args {
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) {
return true
}
}
return false
}
// helper function to create the docker tag command. // helper function to create the docker tag command.
func commandTag(build Build, tag string) *exec.Cmd { func commandTag(build Build, tag string) *exec.Cmd {
var ( var (