1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-01-17 17:45:03 +02:00

updated all deployments to include conditional logic

This commit is contained in:
Brad Rydzewski 2014-09-07 23:08:35 -07:00
parent e3c87388e4
commit a9f2affd5c
12 changed files with 212 additions and 26 deletions

View File

@ -1,5 +1,9 @@
package condition package condition
import (
"strings"
)
type Condition struct { type Condition struct {
Owner string // Indicates the step should run only for this repo (useful for forks) Owner string // Indicates the step should run only for this repo (useful for forks)
Branch string // Indicates the step should run only for this branch Branch string // Indicates the step should run only for this branch
@ -49,5 +53,13 @@ func (c *Condition) MatchOwner(owner string) bool {
if len(c.Owner) == 0 { if len(c.Owner) == 0 {
return true return true
} }
return c.Owner == owner parts := strings.Split(owner, "/")
switch len(parts) {
case 2:
return c.Owner == parts[0]
case 3:
return c.Owner == parts[1]
default:
return c.Owner == owner
}
} }

View File

@ -0,0 +1,113 @@
package condition
import (
"testing"
)
type Bool bool
func Test_MatchPullRequest(t *testing.T) {
var c = Condition{}
var got, want = c.MatchPullRequest(""), true
if got != want {
t.Errorf("Non-pull requests are always enabled, expected %v, got %v", want, got)
}
got, want = c.MatchPullRequest("65"), false
if got != want {
t.Errorf("Pull requests should be disabled by default, expected %v, got %v", want, got)
}
c.PullRequest = new(bool)
*c.PullRequest = false
got, want = c.MatchPullRequest("65"), false
if got != want {
t.Errorf("Pull requests can be explicity disabled, expected %v, got %v", want, got)
}
c.PullRequest = new(bool)
*c.PullRequest = true
got, want = c.MatchPullRequest("65"), true
if got != want {
t.Errorf("Pull requests can be explicitly enabled, expected %v, got %v", want, got)
}
}
func Test_MatchBranch(t *testing.T) {
var c = Condition{}
var got, want = c.MatchBranch("master"), true
if got != want {
t.Errorf("All branches should be enabled by default, expected %v, got %v", want, got)
}
c.Branch = ""
got, want = c.MatchBranch("master"), true
if got != want {
t.Errorf("Empty branch should match, expected %v, got %v", want, got)
}
c.Branch = "master"
got, want = c.MatchBranch("master"), true
if got != want {
t.Errorf("Branch should match, expected %v, got %v", want, got)
}
c.Branch = "master"
got, want = c.MatchBranch("dev"), false
if got != want {
t.Errorf("Branch should not match, expected %v, got %v", want, got)
}
}
func Test_MatchOwner(t *testing.T) {
var c = Condition{}
var got, want = c.MatchOwner("drone"), true
if got != want {
t.Errorf("All owners should be enabled by default, expected %v, got %v", want, got)
}
c.Owner = ""
got, want = c.MatchOwner("drone"), true
if got != want {
t.Errorf("Empty owner should match, expected %v, got %v", want, got)
}
c.Owner = "drone"
got, want = c.MatchOwner("drone"), true
if got != want {
t.Errorf("Owner should match, expected %v, got %v", want, got)
}
c.Owner = "drone"
got, want = c.MatchOwner("drone/config"), true
if got != want {
t.Errorf("Owner/Repo should match, expected %v, got %v", want, got)
}
c.Owner = "drone"
got, want = c.MatchOwner("github.com/drone/config"), true
if got != want {
t.Errorf("Host/Owner/Repo should match, expected %v, got %v", want, got)
}
c.Owner = "bradrydzewski"
got, want = c.MatchOwner("drone"), false
if got != want {
t.Errorf("Owner should not match, expected %v, got %v", want, got)
}
c.Owner = "drone"
got, want = c.MatchOwner("bradrydzewski/drone"), false
if got != want {
t.Errorf("Owner/Repo should not match, expected %v, got %v", want, got)
}
c.Owner = "drone"
got, want = c.MatchOwner("github.com/bradrydzewski/drone"), false
if got != want {
t.Errorf("Host/Owner/Repo should not match, expected %v, got %v", want, got)
}
}

View File

@ -1,12 +1,15 @@
package deploy package deploy
import ( import (
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
) )
type Bash struct { type Bash struct {
Script []string `yaml:"script,omitempty"` Script []string `yaml:"script,omitempty"`
Command string `yaml:"command,omitempty"` Command string `yaml:"command,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
} }
func (g *Bash) Write(f *buildfile.Buildfile) { func (g *Bash) Write(f *buildfile.Buildfile) {
@ -16,3 +19,7 @@ func (g *Bash) Write(f *buildfile.Buildfile) {
f.WriteCmd(cmd) f.WriteCmd(cmd)
} }
} }
func (g *Bash) GetCondition() *condition.Condition {
return g.Condition
}

View File

@ -2,6 +2,7 @@ package deploy
import ( import (
"fmt" "fmt"
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
) )
@ -13,6 +14,8 @@ type CloudFoundry struct {
Space string `yaml:"space,omitempty"` Space string `yaml:"space,omitempty"`
App string `yaml:"app,omitempty"` App string `yaml:"app,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
} }
func (cf *CloudFoundry) Write(f *buildfile.Buildfile) { func (cf *CloudFoundry) Write(f *buildfile.Buildfile) {
@ -42,3 +45,7 @@ func (cf *CloudFoundry) Write(f *buildfile.Buildfile) {
pushCmd := "cf push %s" pushCmd := "cf push %s"
f.WriteCmd(fmt.Sprintf(pushCmd, cf.App)) f.WriteCmd(fmt.Sprintf(pushCmd, cf.App))
} }
func (cf *CloudFoundry) GetCondition() *condition.Condition {
return cf.Condition
}

View File

@ -1,7 +1,9 @@
package deploy package deploy
import ( import (
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
"github.com/drone/drone/shared/build/repo"
) )
// Deploy stores the configuration details // Deploy stores the configuration details
@ -22,41 +24,44 @@ type Deploy struct {
Bash *Bash `yaml:"bash,omitempty"` Bash *Bash `yaml:"bash,omitempty"`
} }
func (d *Deploy) Write(f *buildfile.Buildfile) { func (d *Deploy) Write(f *buildfile.Buildfile, r *repo.Repo) {
if d.AppFog != nil {
d.AppFog.Write(f) if d.CloudFoundry != nil && match(d.CloudFoundry.GetCondition(), r) {
}
if d.CloudControl != nil {
d.CloudControl.Write(f)
}
if d.CloudFoundry != nil {
d.CloudFoundry.Write(f) d.CloudFoundry.Write(f)
} }
if d.EngineYard != nil { if d.Git != nil && match(d.Git.GetCondition(), r) {
d.EngineYard.Write(f)
}
if d.Git != nil {
d.Git.Write(f) d.Git.Write(f)
} }
if d.Heroku != nil { if d.Heroku != nil && match(d.Heroku.GetCondition(), r) {
d.Heroku.Write(f) d.Heroku.Write(f)
} }
if d.Modulus != nil { if d.Modulus != nil && match(d.Modulus.GetCondition(), r) {
d.Modulus.Write(f) d.Modulus.Write(f)
} }
if d.Nodejitsu != nil { if d.Nodejitsu != nil && match(d.Nodejitsu.GetCondition(), r) {
d.Nodejitsu.Write(f) d.Nodejitsu.Write(f)
} }
if d.Openshift != nil { if d.SSH != nil && match(d.SSH.GetCondition(), r) {
d.Openshift.Write(f)
}
if d.SSH != nil {
d.SSH.Write(f) d.SSH.Write(f)
} }
if d.Tsuru != nil { if d.Tsuru != nil && match(d.Tsuru.GetCondition(), r) {
d.Tsuru.Write(f) d.Tsuru.Write(f)
} }
if d.Bash != nil { if d.Bash != nil && match(d.Bash.GetCondition(), r) {
d.Bash.Write(f) d.Bash.Write(f)
} }
} }
func match(c *condition.Condition, r *repo.Repo) bool {
switch {
case c == nil:
return true
case !c.MatchBranch(r.Branch):
return false
case !c.MatchOwner(r.Name):
return false
case !c.MatchPullRequest(r.PR):
return false
}
return true
}

View File

@ -2,6 +2,7 @@ package deploy
import ( import (
"fmt" "fmt"
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
) )
@ -9,6 +10,8 @@ type Git struct {
Target string `yaml:"target,omitempty"` Target string `yaml:"target,omitempty"`
Force bool `yaml:"force,omitempty"` Force bool `yaml:"force,omitempty"`
Branch string `yaml:"branch,omitempty"` Branch string `yaml:"branch,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
} }
func (g *Git) Write(f *buildfile.Buildfile) { func (g *Git) Write(f *buildfile.Buildfile) {
@ -41,3 +44,7 @@ func (g *Git) Write(f *buildfile.Buildfile) {
f.WriteCmd(fmt.Sprintf("git push deploy $COMMIT:%s", destinationBranch)) f.WriteCmd(fmt.Sprintf("git push deploy $COMMIT:%s", destinationBranch))
} }
} }
func (g *Git) GetCondition() *condition.Condition {
return g.Condition
}

View File

@ -2,6 +2,7 @@ package deploy
import ( import (
"fmt" "fmt"
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
) )
@ -9,6 +10,8 @@ type Heroku struct {
App string `yaml:"app,omitempty"` App string `yaml:"app,omitempty"`
Force bool `yaml:"force,omitempty"` Force bool `yaml:"force,omitempty"`
Branch string `yaml:"branch,omitempty"` Branch string `yaml:"branch,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
} }
func (h *Heroku) Write(f *buildfile.Buildfile) { func (h *Heroku) Write(f *buildfile.Buildfile) {
@ -36,3 +39,7 @@ func (h *Heroku) Write(f *buildfile.Buildfile) {
f.WriteCmd(fmt.Sprintf("git push heroku $COMMIT:master")) f.WriteCmd(fmt.Sprintf("git push heroku $COMMIT:master"))
} }
} }
func (h *Heroku) GetCondition() *condition.Condition {
return h.Condition
}

View File

@ -2,12 +2,15 @@ package deploy
import ( import (
"fmt" "fmt"
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
) )
type Modulus struct { type Modulus struct {
Project string `yaml:"project,omitempty"` Project string `yaml:"project,omitempty"`
Token string `yaml:"token,omitempty"` Token string `yaml:"token,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
} }
func (m *Modulus) Write(f *buildfile.Buildfile) { func (m *Modulus) Write(f *buildfile.Buildfile) {
@ -19,3 +22,7 @@ func (m *Modulus) Write(f *buildfile.Buildfile) {
f.WriteCmdSilent("[ -f /usr/bin/sudo ] && sudo npm install -g modulus") f.WriteCmdSilent("[ -f /usr/bin/sudo ] && sudo npm install -g modulus")
f.WriteCmd(fmt.Sprintf("modulus deploy -p '%s'", m.Project)) f.WriteCmd(fmt.Sprintf("modulus deploy -p '%s'", m.Project))
} }
func (m *Modulus) GetCondition() *condition.Condition {
return m.Condition
}

View File

@ -1,6 +1,7 @@
package deploy package deploy
import ( import (
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
) )
@ -8,6 +9,8 @@ type Nodejitsu struct {
App string `yaml:"app,omitempty"` App string `yaml:"app,omitempty"`
User string `yaml:"user,omitempty"` User string `yaml:"user,omitempty"`
Token string `yaml:"token,omitempty"` Token string `yaml:"token,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
} }
func (n *Nodejitsu) Write(f *buildfile.Buildfile) { func (n *Nodejitsu) Write(f *buildfile.Buildfile) {
@ -20,3 +23,7 @@ func (n *Nodejitsu) Write(f *buildfile.Buildfile) {
f.WriteCmdSilent("[ -f /usr/bin/sudo ] && sudo npm install -g jitsu") f.WriteCmdSilent("[ -f /usr/bin/sudo ] && sudo npm install -g jitsu")
f.WriteCmd("jitsu deploy") f.WriteCmd("jitsu deploy")
} }
func (n *Nodejitsu) GetCondition() *condition.Condition {
return n.Condition
}

View File

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
) )
@ -41,6 +42,8 @@ type SSH struct {
// Cmd is a single command executed at target host after the artifacts // Cmd is a single command executed at target host after the artifacts
// is deployed. // is deployed.
Cmd string `yaml:"cmd,omitempty"` Cmd string `yaml:"cmd,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
} }
// Write down the buildfile // Write down the buildfile
@ -96,3 +99,7 @@ func compress(f *buildfile.Buildfile, files []string) bool {
f.WriteCmdSilent(fmt.Sprintf(cmd, strings.Join(files, " "))) f.WriteCmdSilent(fmt.Sprintf(cmd, strings.Join(files, " ")))
return true return true
} }
func (s *SSH) GetCondition() *condition.Condition {
return s.Condition
}

View File

@ -2,6 +2,7 @@ package deploy
import ( import (
"fmt" "fmt"
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/buildfile"
) )
@ -9,9 +10,11 @@ type Tsuru struct {
Force bool `yaml:"force,omitempty"` Force bool `yaml:"force,omitempty"`
Branch string `yaml:"branch,omitempty"` Branch string `yaml:"branch,omitempty"`
Remote string `yaml:"remote,omitempty"` Remote string `yaml:"remote,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
} }
func (h *Tsuru) Write(f *buildfile.Buildfile) { func (t *Tsuru) Write(f *buildfile.Buildfile) {
// get the current commit hash // get the current commit hash
f.WriteCmdSilent("COMMIT=$(git rev-parse HEAD)") f.WriteCmdSilent("COMMIT=$(git rev-parse HEAD)")
@ -21,9 +24,9 @@ func (h *Tsuru) Write(f *buildfile.Buildfile) {
f.WriteCmdSilent("git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')") f.WriteCmdSilent("git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')")
// add tsuru as a git remote // add tsuru as a git remote
f.WriteCmd(fmt.Sprintf("git remote add tsuru %s", h.Remote)) f.WriteCmd(fmt.Sprintf("git remote add tsuru %s", t.Remote))
switch h.Force { switch t.Force {
case true: case true:
// this is useful when the there are artifacts generated // this is useful when the there are artifacts generated
// by the build script, such as less files converted to css, // by the build script, such as less files converted to css,
@ -36,3 +39,7 @@ func (h *Tsuru) Write(f *buildfile.Buildfile) {
f.WriteCmd(fmt.Sprintf("git push tsuru $COMMIT:master")) f.WriteCmd(fmt.Sprintf("git push tsuru $COMMIT:master"))
} }
} }
func (t *Tsuru) GetCondition() *condition.Condition {
return t.Condition
}

View File

@ -96,7 +96,7 @@ func (b *Build) Write(f *buildfile.Buildfile, r *repo.Repo) {
// write deployment commands // write deployment commands
if b.Deploy != nil { if b.Deploy != nil {
b.Deploy.Write(f) b.Deploy.Write(f, r)
} }
// write exit value // write exit value