github.com/unclejack/drone@v0.2.1-0.20140918182345-831b034aa33b/pkg/plugin/publish/docker.go (about) 1 package publish 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/drone/drone/pkg/build/buildfile" 8 "github.com/drone/drone/pkg/build/repo" 9 ) 10 11 type Docker struct { 12 // The path to the dockerfile to create the image from. If the path is empty or no 13 // path is specified then the docker file will be built from the base directory. 14 Dockerfile string `yaml:"docker_file"` 15 16 // Connection information for the docker server that will build the image 17 DockerServer string `yaml:"docker_server"` 18 DockerServerPort int `yaml:"docker_port"` 19 // The Docker client version to download. This must match the docker version on the server 20 DockerVersion string `yaml:"docker_version"` 21 22 // Optional Arguments to allow finer-grained control of registry 23 // endpoints 24 RegistryLoginUrl string `yaml:"registry_login_url"` 25 ImageName string `yaml:"image_name"` 26 RegistryLogin bool `yaml:"registry_login"` 27 28 // Authentication credentials for index.docker.io 29 Username string `yaml:"username"` 30 Password string `yaml:"password"` 31 Email string `yaml:"email"` 32 33 // Keep the build on the Docker host after pushing? 34 KeepBuild bool `yaml:"keep_build"` 35 // Do we want to override "latest" automatically with this build? 36 PushLatest bool `yaml:"push_latest"` 37 CustomTag string `yaml:"custom_tag"` 38 Branch string `yaml:"branch"` 39 } 40 41 // Write adds commands to the buildfile to do the following: 42 // 1. Install the docker client in the Drone container. 43 // 2. Build a docker image based on the dockerfile defined in the config. 44 // 3. Push that docker image to index.docker.io. 45 // 4. Delete the docker image on the server it was build on so we conserve disk space. 46 func (d *Docker) Write(f *buildfile.Buildfile, r *repo.Repo) { 47 if len(d.DockerServer) == 0 || d.DockerServerPort == 0 || len(d.DockerVersion) == 0 || 48 len(d.ImageName) == 0 { 49 f.WriteCmdSilent(`echo -e "Docker Plugin: Missing argument(s)"\n\n`) 50 if len(d.DockerServer) == 0 { f.WriteCmdSilent(`echo -e "\tdocker_server not defined in yaml`) } 51 if d.DockerServerPort == 0 { f.WriteCmdSilent(`echo -e "\tdocker_port not defined in yaml`) } 52 if len(d.DockerVersion) == 0 { f.WriteCmdSilent(`echo -e "\tdocker_version not defined in yaml`) } 53 if len(d.ImageName) == 0 { f.WriteCmdSilent(`echo -e "\timage_name not defined in yaml`) } 54 return 55 } 56 57 // Ensure correct apt-get has the https method-driver as per (http://askubuntu.com/questions/165676/) 58 f.WriteCmd("sudo apt-get install apt-transport-https") 59 60 // Install Docker on the container 61 f.WriteCmd("sudo sh -c \"echo deb https://get.docker.io/ubuntu docker main\\ > " + 62 "/etc/apt/sources.list.d/docker.list\"") 63 f.WriteCmd("sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys " + 64 "36A1D7869245C8950F966E92D8576A8BA88D21E9") 65 f.WriteCmd("sudo apt-get update") 66 f.WriteCmd("sudo apt-get --yes install lxc-docker-" + d.DockerVersion) 67 68 // Format our Build Server Endpoint 69 dockerServerUrl := d.DockerServer + ":" + strconv.Itoa(d.DockerServerPort) 70 71 dockerPath := "." 72 if len(d.Dockerfile) != 0 { 73 dockerPath = fmt.Sprintf("- < %s", d.Dockerfile) 74 } 75 76 // Run the command commands to build and deploy the image. 77 // Are we setting a custom tag, or do we use the git hash? 78 imageTag := "" 79 if len(d.CustomTag) > 0 { 80 imageTag = d.CustomTag 81 } else { 82 imageTag = "$(git rev-parse --short HEAD)" 83 } 84 f.WriteCmd(fmt.Sprintf("docker -H %s build -t %s:%s %s", dockerServerUrl, d.ImageName, imageTag, dockerPath)) 85 86 // Login? 87 if d.RegistryLogin == true { 88 // Are we logging in to a custom Registry? 89 if len(d.RegistryLoginUrl) > 0 { 90 f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s %s", 91 dockerServerUrl, d.Username, d.Password, d.Email, d.RegistryLoginUrl)) 92 } else { 93 // Assume index.docker.io 94 f.WriteCmdSilent(fmt.Sprintf("docker -H %s login -u %s -p %s -e %s", 95 dockerServerUrl, d.Username, d.Password, d.Email)) 96 } 97 } 98 99 // Are we overriding the "latest" tag? 100 if d.PushLatest { 101 f.WriteCmd(fmt.Sprintf("docker -H %s tag %s:%s %s:latest", 102 dockerServerUrl, d.ImageName, imageTag, d.ImageName)) 103 } 104 105 f.WriteCmd(fmt.Sprintf("docker -H %s push %s", dockerServerUrl, d.ImageName)) 106 107 // Delete the image from the docker server we built on. 108 if ! d.KeepBuild { 109 f.WriteCmd(fmt.Sprintf("docker -H %s rmi %s:%s", 110 dockerServerUrl, d.ImageName, imageTag)) 111 if d.PushLatest { 112 f.WriteCmd(fmt.Sprintf("docker -H %s rmi %s:latest", 113 dockerServerUrl, d.ImageName)) 114 } 115 } 116 }