From 9bd7706ca3e414aeaff9c87f7e8cfd3223c30041 Mon Sep 17 00:00:00 2001 From: Kir Shatrov Date: Thu, 20 Mar 2014 00:36:14 +0400 Subject: [PATCH 1/2] Deployment with Bash command Using this piece of YAML: ``` deploy: bash: command: 'bundle exec cap production deploy' ``` Will run Capistrano and deploy your app to production. You can use any other deployment tool instead of Capistrano. --- README.md | 1 + pkg/plugin/deploy/bash.go | 13 +++++++++++++ pkg/plugin/deploy/deployment.go | 4 ++++ 3 files changed, 18 insertions(+) create mode 100644 pkg/plugin/deploy/bash.go diff --git a/README.md b/README.md index 3aa158c68..a8438030f 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,7 @@ Drone currently has these `deploy` and `publish` plugins implemented (more to co - [nodejitsu](#docs) - [ssh](#docs) - [tsuru](#docs) +- [bash](#docs) **publish** - [Amazon s3](#docs) diff --git a/pkg/plugin/deploy/bash.go b/pkg/plugin/deploy/bash.go new file mode 100644 index 000000000..c8b0a193d --- /dev/null +++ b/pkg/plugin/deploy/bash.go @@ -0,0 +1,13 @@ +package deploy + +import ( + "github.com/drone/drone/pkg/build/buildfile" +) + +type Bash struct { + Command string `yaml:"command,omitempty"` +} + +func (g *Bash) Write(f *buildfile.Buildfile) { + f.WriteCmd(g.Command) +} diff --git a/pkg/plugin/deploy/deployment.go b/pkg/plugin/deploy/deployment.go index 44aef7fc1..afb5c8a14 100644 --- a/pkg/plugin/deploy/deployment.go +++ b/pkg/plugin/deploy/deployment.go @@ -19,6 +19,7 @@ type Deploy struct { Openshift *Openshift `yaml:"openshift,omitempty"` SSH *SSH `yaml:"ssh,omitempty"` Tsuru *Tsuru `yaml:"tsuru,omitempty"` + Bash *Bash `yaml:"bash,omitempty"` } func (d *Deploy) Write(f *buildfile.Buildfile) { @@ -55,4 +56,7 @@ func (d *Deploy) Write(f *buildfile.Buildfile) { if d.Tsuru != nil { d.Tsuru.Write(f) } + if d.Bash != nil { + d.Bash.Write(f) + } } From 5567bb679f9c6bfc908207b71b37ec86049ea395 Mon Sep 17 00:00:00 2001 From: Kir Shatrov Date: Thu, 20 Mar 2014 17:18:03 +0400 Subject: [PATCH 2/2] Reworked implementation with test coverage --- pkg/plugin/deploy/bash.go | 9 +++- pkg/plugin/deploy/bash_test.go | 94 ++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 pkg/plugin/deploy/bash_test.go diff --git a/pkg/plugin/deploy/bash.go b/pkg/plugin/deploy/bash.go index c8b0a193d..dc82169f8 100644 --- a/pkg/plugin/deploy/bash.go +++ b/pkg/plugin/deploy/bash.go @@ -5,9 +5,14 @@ import ( ) type Bash struct { - Command string `yaml:"command,omitempty"` + Script []string `yaml:"script,omitempty"` + Command string `yaml:"command,omitempty"` } func (g *Bash) Write(f *buildfile.Buildfile) { - f.WriteCmd(g.Command) + g.Script = append(g.Script, g.Command) + + for _, cmd := range g.Script { + f.WriteCmd(cmd) + } } diff --git a/pkg/plugin/deploy/bash_test.go b/pkg/plugin/deploy/bash_test.go new file mode 100644 index 000000000..f31d9ce05 --- /dev/null +++ b/pkg/plugin/deploy/bash_test.go @@ -0,0 +1,94 @@ +package deploy + +import ( + "strings" + "testing" + + "github.com/drone/drone/pkg/build/buildfile" + + "launchpad.net/goyaml" +) + +// emulate Build struct +type buildWithBash struct { + Deploy *Deploy `yaml:"deploy,omitempty"` +} + +var sampleYmlWithBash = ` +deploy: + bash: + command: 'echo bash_deployed' +` + +var sampleYmlWithScript = ` +deploy: + bash: + script: + - ./bin/deploy.sh + - ./bin/check.sh +` + +var sampleYmlWithBashAndScript = ` +deploy: + bash: + command: ./bin/some_cmd.sh + script: + - ./bin/deploy.sh + - ./bin/check.sh +` + +func setUpWithBash(input string) (string, error) { + var buildStruct buildWithBash + err := goyaml.Unmarshal([]byte(input), &buildStruct) + if err != nil { + return "", err + } + bf := buildfile.New() + buildStruct.Deploy.Write(bf) + return bf.String(), err +} + +func TestBashDeployment(t *testing.T) { + bscr, err := setUpWithBash(sampleYmlWithBash) + if err != nil { + t.Fatalf("Can't unmarshal deploy script: %s", err) + } + + if !strings.Contains(bscr, "echo bash_deployed") { + t.Error("Expect script to contains bash command") + } +} + +func TestBashDeploymentWithScript(t *testing.T) { + bscr, err := setUpWithBash(sampleYmlWithScript) + if err != nil { + t.Fatalf("Can't unmarshal deploy script: %s", err) + } + + if !strings.Contains(bscr, "./bin/deploy.sh") { + t.Error("Expect script to contains bash script") + } + + if !strings.Contains(bscr, "./bin/check.sh") { + t.Error("Expect script to contains bash script") + } +} + +func TestBashDeploymentWithBashAndScript(t *testing.T) { + bscr, err := setUpWithBash(sampleYmlWithBashAndScript) + if err != nil { + t.Fatalf("Can't unmarshal deploy script: %s", err) + } + + if !strings.Contains(bscr, "./bin/deploy.sh") { + t.Error("Expect script to contains bash script") + } + + if !strings.Contains(bscr, "./bin/check.sh") { + t.Error("Expect script to contains bash script") + } + + if !strings.Contains(bscr, "./bin/some_cmd.sh") { + t.Error("Expect script to contains bash script") + } +}