mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-24 01:10:31 +00:00
Merge pull request #591 from scrapinghub/docker-host
Use well known DOCKER_HOST syntax for Docker publish
This commit is contained in:
commit
45f7dfff30
2 changed files with 67 additions and 62 deletions
|
@ -2,7 +2,6 @@ package publish
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/drone/drone/plugin/condition"
|
||||
"github.com/drone/drone/shared/build/buildfile"
|
||||
|
@ -14,8 +13,8 @@ type Docker struct {
|
|||
Dockerfile string `yaml:"docker_file"`
|
||||
|
||||
// Connection information for the docker server that will build the image
|
||||
DockerServer string `yaml:"docker_server"`
|
||||
DockerServerPort int `yaml:"docker_port"`
|
||||
// Same format than DOCKER_HOST envvar, i.e.: tcp://172.16.1.1:2375
|
||||
DockerHost string `yaml:"docker_host"`
|
||||
// The Docker client version to download. This must match the docker version on the server
|
||||
DockerVersion string `yaml:"docker_version"`
|
||||
|
||||
|
@ -45,14 +44,10 @@ type Docker struct {
|
|||
// 3. Push that docker image to index.docker.io.
|
||||
// 4. Delete the docker image on the server it was build on so we conserve disk space.
|
||||
func (d *Docker) Write(f *buildfile.Buildfile) {
|
||||
if len(d.DockerServer) == 0 || d.DockerServerPort == 0 || len(d.DockerVersion) == 0 ||
|
||||
len(d.ImageName) == 0 {
|
||||
if len(d.DockerHost) == 0 || len(d.DockerVersion) == 0 || len(d.ImageName) == 0 {
|
||||
f.WriteCmdSilent(`echo -e "Docker Plugin: Missing argument(s)\n\n"`)
|
||||
if len(d.DockerServer) == 0 {
|
||||
f.WriteCmdSilent(`echo -e "\tdocker_server not defined in yaml"`)
|
||||
}
|
||||
if d.DockerServerPort == 0 {
|
||||
f.WriteCmdSilent(`echo -e "\tdocker_port not defined in yaml"`)
|
||||
if len(d.DockerHost) == 0 {
|
||||
f.WriteCmdSilent(`echo -e "\tdocker_host not defined in yaml"`)
|
||||
}
|
||||
if len(d.DockerVersion) == 0 {
|
||||
f.WriteCmdSilent(`echo -e "\tdocker_version not defined in yaml"`)
|
||||
|
@ -66,9 +61,6 @@ func (d *Docker) Write(f *buildfile.Buildfile) {
|
|||
f.WriteCmd("wget -qO- https://get.docker.io/builds/Linux/x86_64/docker-" +
|
||||
d.DockerVersion + ".tgz |sudo tar zxf - -C /")
|
||||
|
||||
// Format our Build Server Endpoint
|
||||
dockerServerUrl := d.DockerServer + ":" + strconv.Itoa(d.DockerServerPort)
|
||||
|
||||
dockerPath := "."
|
||||
if len(d.Dockerfile) != 0 {
|
||||
dockerPath = fmt.Sprintf("- < %s", d.Dockerfile)
|
||||
|
@ -82,36 +74,29 @@ func (d *Docker) Write(f *buildfile.Buildfile) {
|
|||
} else {
|
||||
imageTag = "$(git rev-parse --short HEAD)"
|
||||
}
|
||||
f.WriteCmd(fmt.Sprintf("docker -H %s build -t %s:%s %s", dockerServerUrl, d.ImageName, imageTag, dockerPath))
|
||||
f.WriteCmd("export DOCKER_HOST=" + d.DockerHost)
|
||||
f.WriteCmd(fmt.Sprintf("docker build -t %s:%s %s", d.ImageName, imageTag, dockerPath))
|
||||
|
||||
// Login?
|
||||
if d.RegistryLogin == true {
|
||||
// Are we logging in to a custom Registry?
|
||||
if len(d.RegistryLoginUrl) > 0 {
|
||||
f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s %s",
|
||||
dockerServerUrl, d.Username, d.Password, d.Email, d.RegistryLoginUrl))
|
||||
} else {
|
||||
// Assume index.docker.io
|
||||
f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s",
|
||||
dockerServerUrl, d.Username, d.Password, d.Email))
|
||||
}
|
||||
f.WriteCmdSilent(fmt.Sprintf("docker login -u %s -p %s -e %s %s",
|
||||
d.Username, d.Password, d.Email, d.RegistryLoginUrl))
|
||||
}
|
||||
// Push the tagged image only - Do not push all image tags
|
||||
f.WriteCmd(fmt.Sprintf("docker push %s:%s", d.ImageName, imageTag))
|
||||
|
||||
// Are we overriding the "latest" tag?
|
||||
if d.PushLatest {
|
||||
f.WriteCmd(fmt.Sprintf("docker -H %s tag %s:%s %s:latest",
|
||||
dockerServerUrl, d.ImageName, imageTag, d.ImageName))
|
||||
f.WriteCmd(fmt.Sprintf("docker tag %s:%s %s:latest",
|
||||
d.ImageName, imageTag, d.ImageName))
|
||||
f.WriteCmd(fmt.Sprintf("docker push %s:latest", d.ImageName))
|
||||
}
|
||||
|
||||
f.WriteCmd(fmt.Sprintf("docker -H %s push %s", dockerServerUrl, d.ImageName))
|
||||
|
||||
// Delete the image from the docker server we built on.
|
||||
if !d.KeepBuild {
|
||||
f.WriteCmd(fmt.Sprintf("docker -H %s rmi %s:%s",
|
||||
dockerServerUrl, d.ImageName, imageTag))
|
||||
f.WriteCmd(fmt.Sprintf("docker rmi %s:%s", d.ImageName, imageTag))
|
||||
if d.PushLatest {
|
||||
f.WriteCmd(fmt.Sprintf("docker -H %s rmi %s:latest",
|
||||
dockerServerUrl, d.ImageName))
|
||||
f.WriteCmd(fmt.Sprintf("docker rmi %s:latest", d.ImageName))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,25 +24,48 @@ func setUpWithDrone(input string) (string, error) {
|
|||
return bf.String(), err
|
||||
}
|
||||
|
||||
// DockerHost and version test (no auth)
|
||||
var dockerHostYaml = `
|
||||
publish:
|
||||
docker:
|
||||
docker_host: tcp://server:1000
|
||||
docker_version: 1.3.0
|
||||
image_name: registry/image
|
||||
`
|
||||
func TestDockerHost(t *testing.T) {
|
||||
response, err := setUpWithDrone(dockerHostYaml)
|
||||
t.Log(privateRegistryNoAuthYaml)
|
||||
if err != nil {
|
||||
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
||||
}
|
||||
expected := "export DOCKER_HOST=tcp://server:1000"
|
||||
if !strings.Contains(response, expected) {
|
||||
t.Fatalf("Response: " + response + " doesn't export correct " +
|
||||
"DOCKER_HOST envvar: expected " + expected + "\n\n")
|
||||
}
|
||||
expected = "https://get.docker.io/builds/Linux/x86_64/docker-1.3.0.tgz"
|
||||
if !strings.Contains(response, expected) {
|
||||
t.Fatalf("Response: " + response + " doesn't download from:" + expected + "\n\n")
|
||||
}
|
||||
}
|
||||
|
||||
// Private Registry Test (no auth)
|
||||
var privateRegistryNoAuthYaml = `
|
||||
publish:
|
||||
docker:
|
||||
dockerfile: file_path
|
||||
docker_server: server
|
||||
docker_port: 1000
|
||||
docker_host: tcp://server:1000
|
||||
docker_version: 1.0
|
||||
registry_login: false
|
||||
image_name: registry/image
|
||||
`
|
||||
|
||||
func TestPrivateRegistryNoAuth(t *testing.T) {
|
||||
response, err := setUpWithDrone(privateRegistryNoAuthYaml)
|
||||
t.Log(privateRegistryNoAuthYaml)
|
||||
if err != nil {
|
||||
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD)") {
|
||||
if !strings.Contains(response, "docker build -t registry/image:$(git rev-parse --short HEAD)") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain registry in image-names: expected registry/image\n\n")
|
||||
}
|
||||
}
|
||||
|
@ -52,8 +75,7 @@ var privateRegistryAuthYaml = `
|
|||
publish:
|
||||
docker:
|
||||
dockerfile: file_path
|
||||
docker_server: server
|
||||
docker_port: 1000
|
||||
docker_host: tcp://server:1000
|
||||
docker_version: 1.0
|
||||
registry_login_url: https://registry:8000/v1/
|
||||
registry_login: true
|
||||
|
@ -69,12 +91,12 @@ func TestPrivateRegistryAuth(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 login -u username -p password -e email@example.com https://registry:8000/v1/") {
|
||||
t.Log("\n\n\n\ndocker -H server:1000 login -u username -p xxxxxxxx -e email@example.com https://registry:8000/v1/\n\n\n\n")
|
||||
if !strings.Contains(response, "docker login -u username -p password -e email@example.com https://registry:8000/v1/") {
|
||||
t.Log("\n\n\n\ndocker login -u username -p xxxxxxxx -e email@example.com https://registry:8000/v1/\n\n\n\n")
|
||||
t.Fatalf("Response: " + response + " doesn't contain private registry login\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD) .") {
|
||||
t.Log("docker -H server:1000 build -t registry/image:$(git rev-parse --short HEAD) .")
|
||||
if !strings.Contains(response, "docker build -t registry/image:$(git rev-parse --short HEAD) .") {
|
||||
t.Log("docker build -t registry/image:$(git rev-parse --short HEAD) .")
|
||||
t.Fatalf("Response: " + response + " doesn't contain registry in image-names\n\n")
|
||||
}
|
||||
}
|
||||
|
@ -83,8 +105,7 @@ func TestPrivateRegistryAuth(t *testing.T) {
|
|||
var overrideLatestTagYaml = `
|
||||
publish:
|
||||
docker:
|
||||
docker_server: server
|
||||
docker_port: 1000
|
||||
docker_host: tcp://server:1000
|
||||
docker_version: 1.0
|
||||
username: username
|
||||
password: password
|
||||
|
@ -99,20 +120,22 @@ func TestOverrideLatestTag(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 build -t username/image:$(git rev-parse --short HEAD) .") {
|
||||
if !strings.Contains(response, "docker build -t username/image:$(git rev-parse --short HEAD) .") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain the git-ref tagged image\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 tag username/image:$(git rev-parse --short HEAD) username/image:latest") {
|
||||
if !strings.Contains(response, "docker tag username/image:$(git rev-parse --short HEAD) username/image:latest") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain 'latest' tag command\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker push username/image:latest") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain push command\n\n")
|
||||
}
|
||||
}
|
||||
|
||||
// Keep builds Test
|
||||
var keepBuildsYaml = `
|
||||
publish:
|
||||
docker:
|
||||
docker_server: server
|
||||
docker_port: 1000
|
||||
docker_host: tcp://server:1000
|
||||
docker_version: 1.0
|
||||
keep_build: true
|
||||
username: username
|
||||
|
@ -127,7 +150,7 @@ func TestKeepBuilds(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
||||
}
|
||||
if strings.Contains(response, "docker -H server:1000 rmi") {
|
||||
if strings.Contains(response, "docker rmi") {
|
||||
t.Fatalf("Response: " + response + " incorrectly instructs the docker server to remove the builds when it shouldn't\n\n")
|
||||
}
|
||||
}
|
||||
|
@ -136,8 +159,7 @@ func TestKeepBuilds(t *testing.T) {
|
|||
var customTagYaml = `
|
||||
publish:
|
||||
docker:
|
||||
docker_server: server
|
||||
docker_port: 1000
|
||||
docker_host: tcp://server:1000
|
||||
docker_version: 1.0
|
||||
custom_tag: release-0.1
|
||||
username: username
|
||||
|
@ -155,10 +177,10 @@ func TestCustomTag(t *testing.T) {
|
|||
if strings.Contains(response, "$(git rev-parse --short HEAD)") {
|
||||
t.Fatalf("Response: " + response + " is tagging images from git-refs when it should use a custom tag\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 build -t username/image:release-0.1") {
|
||||
if !strings.Contains(response, "docker build -t username/image:release-0.1") {
|
||||
t.Fatalf("Response: " + response + " isn't tagging images using our custom tag\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 push username/image") {
|
||||
if !strings.Contains(response, "docker push username/image:release-0.1") {
|
||||
t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n")
|
||||
}
|
||||
}
|
||||
|
@ -184,8 +206,7 @@ var validYaml = `
|
|||
publish:
|
||||
docker:
|
||||
docker_file: file_path
|
||||
docker_server: server
|
||||
docker_port: 1000
|
||||
docker_host: tcp://server:1000
|
||||
docker_version: 1.0
|
||||
username: user
|
||||
password: password
|
||||
|
@ -202,19 +223,19 @@ func TestValidYaml(t *testing.T) {
|
|||
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
||||
}
|
||||
|
||||
if !strings.Contains(response, "docker -H server:1000 tag user/image:$(git rev-parse --short HEAD) user/image:latest") {
|
||||
if !strings.Contains(response, "docker tag user/image:$(git rev-parse --short HEAD) user/image:latest") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain tag command for latest\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 build -t user/image:$(git rev-parse --short HEAD) - <") {
|
||||
if !strings.Contains(response, "docker build -t user/image:$(git rev-parse --short HEAD) - <") {
|
||||
t.Fatalf("Response: " + response + "doesn't contain build command for commit hash\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 login -u user -p password -e email") {
|
||||
if !strings.Contains(response, "docker login -u user -p password -e email") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain login command\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 push user/image") {
|
||||
if !strings.Contains(response, "docker push user/image:$(git rev-parse --short HEAD)") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain push command\n\n")
|
||||
}
|
||||
if !strings.Contains(response, "docker -H server:1000 rmi user/image:"+
|
||||
if !strings.Contains(response, "docker rmi user/image:"+
|
||||
"$(git rev-parse --short HEAD)") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain remove image command\n\n")
|
||||
}
|
||||
|
@ -223,8 +244,7 @@ func TestValidYaml(t *testing.T) {
|
|||
var withoutDockerFileYaml = `
|
||||
publish:
|
||||
docker:
|
||||
docker_server: server
|
||||
docker_port: 1000
|
||||
docker_host: tcp://server:1000
|
||||
docker_version: 1.0
|
||||
image_name: user/image
|
||||
username: user
|
||||
|
@ -239,7 +259,7 @@ func TestWithoutDockerFile(t *testing.T) {
|
|||
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
|
||||
}
|
||||
|
||||
if !strings.Contains(response, "docker -H server:1000 build -t user/image:$(git rev-parse --short HEAD) .") {
|
||||
if !strings.Contains(response, "docker build -t user/image:$(git rev-parse --short HEAD) .") {
|
||||
t.Fatalf("Response: " + response + " doesn't contain build command\n\n")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue