Allow publishing from base directory

Some our our respositories have Dockerfiles with commands like ADD . /path. These don't
work if we do - < Dockerfile because the only thing in "." will be the Dockerfile, not
the other things we want. This allows us to support that use case.
This commit is contained in:
Kyle Vigen 2014-06-27 15:22:45 -07:00
parent 022a8b3908
commit b009475ea1
2 changed files with 35 additions and 7 deletions

View file

@ -10,7 +10,8 @@ import (
)
type Docker struct {
// The path to the dockerfile to create the image from
// The path to the dockerfile to create the image from. If the path is empty or no
// path is specified then the docker file will be built from the base directory.
Dockerfile string `yaml:"docker_file"`
// Connection information for the docker server that will build the image
@ -35,9 +36,8 @@ 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, r *repo.Repo) {
if len(d.Dockerfile) == 0 || len(d.Server) == 0 || d.Port == 0 || len(d.DockerVersion) == 0 ||
len(d.RepoBaseName) == 0 || len(d.Username) == 0 || len(d.Password) == 0 ||
len(d.Email) == 0 {
if len(d.Email) == 0 || len(d.Server) == 0 || d.Port == 0 || len(d.DockerVersion) == 0 ||
len(d.RepoBaseName) == 0 || len(d.Username) == 0 || len(d.Password) == 0 {
f.WriteCmdSilent(`echo "Docker Plugin: Missing argument(s)"`)
return
}
@ -51,11 +51,16 @@ func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) {
dockerServerUrl := d.Server + ":" + strconv.Itoa(d.Port)
splitRepoName := strings.Split(r.Name, "/")
dockerRepo := d.RepoBaseName + "/" + splitRepoName[len(splitRepoName) - 1]
dockerPath := "."
if len(d.Dockerfile) != 0 {
dockerPath = fmt.Sprintf("- < %s", d.Dockerfile)
}
// Run the command commands to build and deploy the image. Note that we both create a new image
// tagged with the git hash as well as update the "latest" image.
f.WriteCmd(fmt.Sprintf("docker -H %s build -t %s - < %s", dockerServerUrl, dockerRepo, d.Dockerfile))
f.WriteCmd(fmt.Sprintf("docker -H %s build -t %s:$(git rev-parse --short HEAD) - < %s",
dockerServerUrl, dockerRepo, d.Dockerfile))
f.WriteCmd(fmt.Sprintf("docker -H %s build -t %s %s", dockerServerUrl, dockerRepo, dockerPath))
f.WriteCmd(fmt.Sprintf("docker -H %s build -t %s:$(git rev-parse --short HEAD) %s",
dockerServerUrl, dockerRepo, dockerPath))
// Login and push to index.docker.io
f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s",

View file

@ -77,3 +77,26 @@ func TestValidYaml(t *testing.T) {
t.Fatalf("Response: " + response + " doesn't contain remove image command")
}
}
var withoutDockerFileYaml = `
publish:
docker:
docker_server: server
docker_port: 1000
docker_version: 1.0
repo_base_name: base_repo
username: user
password: password
email: email
`
func TestWithoutDockerFile(t *testing.T) {
response, err := setUpWithDrone(withoutDockerFileYaml)
if err != nil {
t.Fatalf("Can't unmarshal script: %s", err.Error())
}
if !strings.Contains(response, "docker -H server:1000 build -t base_repo/name .") {
t.Fatalf("Response: " + response + " doesn't contain build command")
}
}