diff --git a/plugin/deploy/tsuru/tsuru.go b/plugin/deploy/tsuru/tsuru.go new file mode 100644 index 00000000..808f8867 --- /dev/null +++ b/plugin/deploy/tsuru/tsuru.go @@ -0,0 +1,50 @@ +package tsuru + +import ( + "fmt" + "github.com/drone/drone/plugin/condition" + "github.com/drone/drone/shared/build/buildfile" +) + +const ( + // Gommand to the current commit hash + CmdRevParse = "COMMIT=$(git rev-parse HEAD)" + + // Command to set the git user and email based on the + // individual that made the commit. + CmdGlobalEmail = "git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')" + CmdGlobalUser = "git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')" +) + +type Tsuru struct { + Force bool `yaml:"force,omitempty"` + Remote string `yaml:"remote,omitempty"` + + Condition *condition.Condition `yaml:"when,omitempty"` +} + +func (t *Tsuru) Write(f *buildfile.Buildfile) { + f.WriteCmdSilent(CmdRevParse) + f.WriteCmdSilent(CmdGlobalUser) + f.WriteCmdSilent(CmdGlobalEmail) + + // add tsuru as a git remote + f.WriteCmd(fmt.Sprintf("git remote add tsuru %s", t.Remote)) + + switch t.Force { + case true: + // this is useful when the there are artifacts generated + // by the build script, such as less files converted to css, + // that need to be deployed to Tsuru. + f.WriteCmd(fmt.Sprintf("git add -A")) + f.WriteCmd(fmt.Sprintf("git commit -m 'adding build artifacts'")) + f.WriteCmd(fmt.Sprintf("git push tsuru HEAD:master --force")) + case false: + // otherwise we just do a standard git push + f.WriteCmd(fmt.Sprintf("git push tsuru $COMMIT:master")) + } +} + +func (t *Tsuru) GetCondition() *condition.Condition { + return t.Condition +} diff --git a/plugin/deploy/tsuru/tsuru_test.go b/plugin/deploy/tsuru/tsuru_test.go new file mode 100644 index 00000000..c456d7d4 --- /dev/null +++ b/plugin/deploy/tsuru/tsuru_test.go @@ -0,0 +1,66 @@ +package tsuru + +import ( + "strings" + "testing" + + "github.com/drone/drone/shared/build/buildfile" + "github.com/franela/goblin" +) + +func Test_Git(t *testing.T) { + + g := goblin.Goblin(t) + g.Describe("Tsuru Deploy", func() { + + g.It("Should set git.config", func() { + b := new(buildfile.Buildfile) + d := Tsuru{ + Remote: "git://foo.com/bar/baz.git", + } + + d.Write(b) + out := b.String() + g.Assert(strings.Contains(out, CmdRevParse)).Equal(true) + g.Assert(strings.Contains(out, CmdGlobalUser)).Equal(true) + g.Assert(strings.Contains(out, CmdGlobalEmail)).Equal(true) + }) + + g.It("Should add remote", func() { + b := new(buildfile.Buildfile) + d := Tsuru{ + Remote: "git://foo.com/bar/baz.git", + } + + d.Write(b) + out := b.String() + g.Assert(strings.Contains(out, "\ngit remote add tsuru git://foo.com/bar/baz.git\n")).Equal(true) + }) + + g.It("Should push to remote", func() { + b := new(buildfile.Buildfile) + d := Tsuru{ + Remote: "git://foo.com/bar/baz.git", + } + + d.Write(b) + out := b.String() + g.Assert(strings.Contains(out, "\ngit push tsuru $COMMIT:master\n")).Equal(true) + }) + + g.It("Should force push to remote", func() { + b := new(buildfile.Buildfile) + d := Tsuru{ + Force: true, + Remote: "git://foo.com/bar/baz.git", + } + + d.Write(b) + out := b.String() + g.Assert(strings.Contains(out, "\ngit add -A\n")).Equal(true) + g.Assert(strings.Contains(out, "\ngit commit -m 'adding build artifacts'\n")).Equal(true) + g.Assert(strings.Contains(out, "\ngit push tsuru HEAD:master --force\n")).Equal(true) + }) + + }) +}