initial working version of plugin

This commit is contained in:
Brad Rydzewski 2015-05-14 22:12:50 -07:00
parent 0f2b6fd359
commit 67806aa90a
4 changed files with 150 additions and 0 deletions

2
.gitignore vendored

@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof
drone-docker

14
Dockerfile Normal file

@ -0,0 +1,14 @@
# Docker image for the docker plugin
#
# docker build --rm=true -t plugins/drone-docker .
FROM ubuntu:14.04
RUN apt-get update -qq \
&& apt-get -y install curl \
&& curl -sSL https://get.docker.com/ubuntu/ | sh \
&& rm -rf /var/lib/apt/lists/*
ADD drone-docker /go/bin/
ENTRYPOINT ["/go/bin/drone-docker"]

@ -1,2 +1,33 @@
# drone-docker
Drone plugin for publishing Docker images
## Docker
Build the Docker container:
```sh
docker build --rm=true -t plugins/drone-docker .
```
Build and Publish a Docker container
```sh
docker run -i --privileged -v $(pwd):/drone/src plugins/drone-docker <<EOF
{
"clone": {
"dir": "/drone/src"
},
"commit" : {
"sha": "9f2849d5",
"branch": "master"
},
"vargs": {
"username": "foo",
"password": "bar",
"email": "foo@bar.com",
"repo": "foo/bar"
}
}
EOF
```

103
main.go Normal file

@ -0,0 +1,103 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
"github.com/drone/drone-plugin-go/plugin"
)
type Docker struct {
Registry string `json:"registry"`
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
Repo string `json:"repo"`
Tag string `json:"tag"`
File string `json:"file"`
}
func main() {
clone := plugin.Clone{}
vargs := Docker{}
plugin.Param("clone", &clone)
plugin.Param("vargs", &vargs)
if err := plugin.Parse(); err != nil {
println(err.Error())
os.Exit(1)
}
// Starts the Docker daemon
go func() {
cmd := exec.Command("docker", "-d")
cmd.Dir = clone.Dir
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
cmd.Run()
}()
// Sleep for a few seconds
time.Sleep(5 * time.Second)
// Set the Registry value
if len(vargs.Registry) == 0 {
vargs.Registry = "https://index.docker.io/v1/"
}
// Set the Dockerfile path
if len(vargs.File) == 0 {
vargs.File = "."
}
// Set the Tag value
switch vargs.Tag {
case "$DRONE_BRANCH":
vargs.Tag = clone.Branch
case "$DRONE_COMMIT":
vargs.Tag = clone.Sha
case "":
vargs.Tag = "latest"
}
vargs.Repo = fmt.Sprintf("%s:%s", vargs.Repo, vargs.Tag)
// Build the container
cmd := exec.Command("docker", "build", "--pull=true", "--rm=true", "-t", vargs.Repo, vargs.File)
cmd.Dir = clone.Dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err := cmd.Run()
if err != nil {
os.Exit(1)
}
// Login to Docker
cmd = exec.Command("docker", "login", "-u", vargs.Username, "-p", vargs.Password, "-e", vargs.Email, "index.docker.io")
cmd.Dir = clone.Dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
os.Exit(1)
}
// Push the container
cmd = exec.Command("docker", "push", vargs.Repo)
cmd.Dir = clone.Dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err = cmd.Run()
if err != nil {
os.Exit(1)
}
}
// Trace writes each command to standard error (preceded by a $ ) before it
// is executed. Used for debugging your build.
func trace(cmd *exec.Cmd) {
fmt.Println("$", strings.Join(cmd.Args, " "))
}