Docker publish: added tags field to allow for a single build to be tagged with multiple

tags. Only push tags that are applied to the repo. Change custom_tag to just tag.
This commit is contained in:
epipho 2014-10-12 01:39:02 -04:00
parent 45f7dfff30
commit 2bc2d45612
2 changed files with 126 additions and 58 deletions

View file

@ -30,10 +30,9 @@ type Docker struct {
Email string `yaml:"email"` Email string `yaml:"email"`
// Keep the build on the Docker host after pushing? // Keep the build on the Docker host after pushing?
KeepBuild bool `yaml:"keep_build"` KeepBuild bool `yaml:"keep_build"`
// Do we want to override "latest" automatically with this build? Tag string `yaml:"tag"`
PushLatest bool `yaml:"push_latest"` Tags []string `yaml:"tags"`
CustomTag string `yaml:"custom_tag"`
Condition *condition.Condition `yaml:"when,omitempty"` Condition *condition.Condition `yaml:"when,omitempty"`
} }
@ -67,36 +66,42 @@ func (d *Docker) Write(f *buildfile.Buildfile) {
} }
// Run the command commands to build and deploy the image. // Run the command commands to build and deploy the image.
// Are we setting a custom tag, or do we use the git hash?
imageTag := "" // Add the single tag if one exists
if len(d.CustomTag) > 0 { if len(d.Tag) > 0 {
imageTag = d.CustomTag d.Tags = append(d.Tags, d.Tag)
} else {
imageTag = "$(git rev-parse --short HEAD)"
} }
// If no tags are specified, use the commit hash
if len(d.Tags) == 0 {
d.Tags = append(d.Tags, "$(git rev-parse --short HEAD)")
}
// There is always at least 1 tag
buildImageTag := d.Tags[0]
// Export docker host once
f.WriteCmd("export DOCKER_HOST=" + d.DockerHost) f.WriteCmd("export DOCKER_HOST=" + d.DockerHost)
f.WriteCmd(fmt.Sprintf("docker build -t %s:%s %s", d.ImageName, imageTag, dockerPath))
// Build the image
f.WriteCmd(fmt.Sprintf("docker build -t %s:%s %s", d.ImageName, buildImageTag, dockerPath))
// Login? // Login?
if d.RegistryLogin == true { if d.RegistryLogin == true {
f.WriteCmdSilent(fmt.Sprintf("docker login -u %s -p %s -e %s %s", f.WriteCmdSilent(fmt.Sprintf("docker login -u %s -p %s -e %s %s",
d.Username, d.Password, d.Email, d.RegistryLoginUrl)) 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? // Tag and push all tags
if d.PushLatest { for _, tag := range d.Tags {
f.WriteCmd(fmt.Sprintf("docker tag %s:%s %s:latest", f.WriteCmd(fmt.Sprintf("docker tag %s:%s %s:%s", d.ImageName, buildImageTag, d.ImageName, tag))
d.ImageName, imageTag, d.ImageName)) f.WriteCmd(fmt.Sprintf("docker push %s:%s", d.ImageName, tag))
f.WriteCmd(fmt.Sprintf("docker push %s:latest", d.ImageName))
} }
// Delete the image from the docker server we built on. // Remove tags after pushing unless keepBuild is set
if !d.KeepBuild { if !d.KeepBuild {
f.WriteCmd(fmt.Sprintf("docker rmi %s:%s", d.ImageName, imageTag)) for _, tag := range d.Tags {
if d.PushLatest { f.WriteCmd(fmt.Sprintf("docker rmi %s:%s", d.ImageName, tag))
f.WriteCmd(fmt.Sprintf("docker rmi %s:latest", d.ImageName))
} }
} }
} }

View file

@ -32,6 +32,7 @@ publish:
docker_version: 1.3.0 docker_version: 1.3.0
image_name: registry/image image_name: registry/image
` `
func TestDockerHost(t *testing.T) { func TestDockerHost(t *testing.T) {
response, err := setUpWithDrone(dockerHostYaml) response, err := setUpWithDrone(dockerHostYaml)
t.Log(privateRegistryNoAuthYaml) t.Log(privateRegistryNoAuthYaml)
@ -59,6 +60,7 @@ publish:
registry_login: false registry_login: false
image_name: registry/image image_name: registry/image
` `
func TestPrivateRegistryNoAuth(t *testing.T) { func TestPrivateRegistryNoAuth(t *testing.T) {
response, err := setUpWithDrone(privateRegistryNoAuthYaml) response, err := setUpWithDrone(privateRegistryNoAuthYaml)
t.Log(privateRegistryNoAuthYaml) t.Log(privateRegistryNoAuthYaml)
@ -101,36 +103,6 @@ func TestPrivateRegistryAuth(t *testing.T) {
} }
} }
// Override "latest" Test
var overrideLatestTagYaml = `
publish:
docker:
docker_host: tcp://server:1000
docker_version: 1.0
username: username
password: password
email: email@example.com
image_name: username/image
push_latest: true
`
func TestOverrideLatestTag(t *testing.T) {
response, err := setUpWithDrone(overrideLatestTagYaml)
t.Log(overrideLatestTagYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
}
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 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 // Keep builds Test
var keepBuildsYaml = ` var keepBuildsYaml = `
publish: publish:
@ -161,14 +133,14 @@ publish:
docker: docker:
docker_host: tcp://server:1000 docker_host: tcp://server:1000
docker_version: 1.0 docker_version: 1.0
custom_tag: release-0.1 tag: release-0.1
username: username username: username
password: password password: password
email: email@example.com email: email@example.com
image_name: username/image image_name: username/image
` `
func TestCustomTag(t *testing.T) { func TestSingleTag(t *testing.T) {
response, err := setUpWithDrone(customTagYaml) response, err := setUpWithDrone(customTagYaml)
t.Log(customTagYaml) t.Log(customTagYaml)
if err != nil { if err != nil {
@ -183,6 +155,9 @@ func TestCustomTag(t *testing.T) {
if !strings.Contains(response, "docker push username/image:release-0.1") { if !strings.Contains(response, "docker push username/image:release-0.1") {
t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n") t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n")
} }
if !strings.Contains(response, "docker rmi username/image:release-0.1") {
t.Fatalf("Response: " + response + " doesn't remove custom tagged image\n\n")
}
} }
var missingFieldsYaml = ` var missingFieldsYaml = `
@ -191,6 +166,98 @@ publish:
dockerfile: file dockerfile: file
` `
var multipleTagsYaml = `
publish:
docker:
docker_host: tcp://server:1000
docker_version: 1.0
tags: [release-0.2, release-latest]
username: username
password: password
email: email@example.com
image_name: username/image
`
func TestTagsNoSingle(t *testing.T) {
response, err := setUpWithDrone(multipleTagsYaml)
t.Log(multipleTagsYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n", err.Error())
}
if strings.Contains(response, "$(git rev-parse --short HEAD)") {
t.Fatalf("Response: " + response + " is tagging images from git-refs when it should using custom tag\n\n")
}
if !strings.Contains(response, "docker build -t username/image:release-0.2") {
t.Fatalf("Response: " + response + " isn't tagging images using our first custom tag\n\n")
}
if !strings.Contains(response, "docker tag username/image:release-0.2 username/image:release-latest") {
t.Fatalf("Response: " + response + " isn't tagging images using our second custom tag\n\n")
}
if !strings.Contains(response, "docker push username/image:release-0.2") {
t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n")
}
if !strings.Contains(response, "docker rmi username/image:release-0.2") {
t.Fatalf("Response: " + response + " doesn't remove custom tagged image\n\n")
}
if !strings.Contains(response, "docker push username/image:release-latest") {
t.Fatalf("Response: " + response + " doesn't push the second custom tagged image\n\n")
}
if !strings.Contains(response, "docker rmi username/image:release-latest") {
t.Fatalf("Response: " + response + " doesn't remove second custom tagged image\n\n")
}
}
var bothTagsYaml = `
publish:
docker:
docker_host: tcp://server:1000
docker_version: 1.0
tag: release-0.2
tags: [release-0.3, release-latest]
username: username
password: password
email: email@example.com
image_name: username/image
`
func TestTagsWithSingle(t *testing.T) {
response, err := setUpWithDrone(bothTagsYaml)
t.Log(bothTagsYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s\n", err.Error())
}
if strings.Contains(response, "$(git rev-parse --short HEAD)") {
t.Fatalf("Response: " + response + " is tagging images from git-refs when it should using custom tag\n\n")
}
if !strings.Contains(response, "docker build -t username/image:release-0.3") {
t.Fatalf("Response: " + response + " isn't tagging images using our first custom tag\n\n")
}
if !strings.Contains(response, "docker tag username/image:release-0.3 username/image:release-0.2") {
t.Fatalf("Response: " + response + " isn't tagging images using our second custom tag\n\n")
}
if !strings.Contains(response, "docker tag username/image:release-0.3 username/image:release-latest") {
t.Fatalf("Response: " + response + " isn't tagging images using our third custom tag\n\n")
}
if !strings.Contains(response, "docker push username/image:release-0.2") {
t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n")
}
if !strings.Contains(response, "docker rmi username/image:release-0.2") {
t.Fatalf("Response: " + response + " doesn't remove custom tagged image\n\n")
}
if !strings.Contains(response, "docker push username/image:release-0.3") {
t.Fatalf("Response: " + response + " doesn't push the custom tagged image\n\n")
}
if !strings.Contains(response, "docker rmi username/image:release-0.3") {
t.Fatalf("Response: " + response + " doesn't remove custom tagged image\n\n")
}
if !strings.Contains(response, "docker push username/image:release-latest") {
t.Fatalf("Response: " + response + " doesn't push the second custom tagged image\n\n")
}
if !strings.Contains(response, "docker rmi username/image:release-latest") {
t.Fatalf("Response: " + response + " doesn't remove second custom tagged image\n\n")
}
}
func TestMissingFields(t *testing.T) { func TestMissingFields(t *testing.T) {
response, err := setUpWithDrone(missingFieldsYaml) response, err := setUpWithDrone(missingFieldsYaml)
t.Log(missingFieldsYaml) t.Log(missingFieldsYaml)
@ -212,7 +279,6 @@ publish:
password: password password: password
email: email email: email
image_name: user/image image_name: user/image
push_latest: true
registry_login: true registry_login: true
` `
@ -223,9 +289,6 @@ func TestValidYaml(t *testing.T) {
t.Fatalf("Can't unmarshal script: %s\n\n", err.Error()) t.Fatalf("Can't unmarshal script: %s\n\n", err.Error())
} }
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 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") t.Fatalf("Response: " + response + "doesn't contain build command for commit hash\n\n")
} }