Skip to content

Commit

Permalink
Making deploy as a string to pass environment
Browse files Browse the repository at this point in the history
  • Loading branch information
prabdeb committed Oct 4, 2018
1 parent 61a88a0 commit 502fff6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
6 changes: 3 additions & 3 deletions main.go
Expand Up @@ -67,9 +67,9 @@ func main() {
Usage: "List of environment variables to pass to triggered builds",
EnvVar: "PLUGIN_PARAMS_FROM_ENV",
},
cli.BoolFlag{
cli.StringFlag{
Name: "deploy",
Usage: "Trigger deploy for the respective build",
Usage: "Environment to trigger deploy for the respective build",
EnvVar: "PLUGIN_DEPLOY",
},
}
Expand All @@ -90,7 +90,7 @@ func run(c *cli.Context) error {
LastSuccessful: c.Bool("last-successful"),
Params: c.StringSlice("params"),
ParamsEnv: c.StringSlice("params-from-env"),
Deploy: c.Bool("deploy"),
Deploy: c.String("deploy"),
}

return plugin.Exec()
Expand Down
11 changes: 3 additions & 8 deletions main_test.go
Expand Up @@ -12,16 +12,14 @@ func Test_parseRepoBranch(t *testing.T) {
Owner string
Name string
Branch string
Env string
}{
{"octocat/hello-world", "octocat", "hello-world", "", ""},
{"octocat/hello-world@master", "octocat", "hello-world", "master", ""},
{"octocat/hello-world@master@production", "octocat", "hello-world", "master", "production"},
{"octocat/hello-world", "octocat", "hello-world", ""},
{"octocat/hello-world@master", "octocat", "hello-world", "master"},
}

for _, test := range tests {

owner, name, branch, env := parseRepoBranch(test.Repo)
owner, name, branch := parseRepoBranch(test.Repo)
if owner != test.Owner {
t.Errorf("wanted repository owner %s, got %s", test.Owner, owner)
}
Expand All @@ -31,9 +29,6 @@ func Test_parseRepoBranch(t *testing.T) {
if branch != test.Branch {
t.Errorf("wanted repository branch %s, got %s", test.Branch, branch)
}
if env != test.Env {
t.Errorf("wanted deployment event %s, got %s", test.Env, env)
}
}
}

Expand Down
30 changes: 11 additions & 19 deletions plugin.go
Expand Up @@ -23,7 +23,7 @@ type Plugin struct {
LastSuccessful bool
Params []string
ParamsEnv []string
Deploy bool
Deploy string
}

// Exec runs the plugin
Expand Down Expand Up @@ -76,23 +76,20 @@ func (p *Plugin) Exec() error {
for _, entry := range p.Repos {

// parses the repository name in owner/name@branch format
owner, name, branch, env := parseRepoBranch(entry)
owner, name, branch := parseRepoBranch(entry)
if len(owner) == 0 || len(name) == 0 {
return fmt.Errorf("Error: unable to parse repository name %s.\n", entry)
}

// check for mandatory build no during deploy trigger
if p.Deploy {
if len(p.Deploy) != 0 {
if branch == "" {
return fmt.Errorf("Error: build no must be mentioned for deploy, format repository@build/branch@env")
}
if env == "" {
return fmt.Errorf("Error: environment must be mentioned for deploy, format repository@build/branch@env")
return fmt.Errorf("Error: build no or branch must be mentioned for deploy, format repository@build/branch")
}
if _, err := strconv.Atoi(branch); err != nil && !p.LastSuccessful {
return fmt.Errorf("Error: for deploy build no must be numeric only " +
" or for branch deploy last_successful should be true," +
" format repository@build/branch@env")
" format repository@build/branch")
}
}

Expand All @@ -112,7 +109,7 @@ func (p *Plugin) Exec() error {
// Got a tick, we should check on the build status
case <-tick:
// first handle the deploy trigger
if p.Deploy {
if len(p.Deploy) != 0 {
build, err := client.BuildLast(owner, name, branch)
if p.LastSuccessful {
// Get the last successful build of branch
Expand All @@ -136,7 +133,7 @@ func (p *Plugin) Exec() error {
buildNumber, _ := strconv.Atoi(branch)
build, err = client.Build(owner, name, buildNumber)
if err != nil {
return fmt.Errorf("Error: unable to get requested build %v for deploy for %s", build.Number, entry)
return fmt.Errorf("Error: unable to get requested build %v for deploy for %s", buildNumber, entry)
}
}
if p.Wait && !waiting && (build.Status == drone.StatusRunning || build.Status == drone.StatusPending) {
Expand All @@ -146,14 +143,14 @@ func (p *Plugin) Exec() error {
}
if (build.Status != drone.StatusRunning && build.Status != drone.StatusPending) || !p.Wait {
// start a new deploy
_, err = client.Deploy(owner, name, build.Number, env, params)
_, err = client.Deploy(owner, name, build.Number, p.Deploy, params)
if err != nil {
if waiting {
continue
}
return fmt.Errorf("Error: unable to trigger deploy for %s - err %v", entry, err)
}
fmt.Printf("Starting deploy for %s/%s env - %s build - %d.\n", owner, name, env, build.Number)
fmt.Printf("Starting deploy for %s/%s env - %s build - %d.\n", owner, name, p.Deploy, build.Number)
logParams(params, p.ParamsEnv)
break I
}
Expand Down Expand Up @@ -223,30 +220,25 @@ func (p *Plugin) Exec() error {
return nil
}

func parseRepoBranch(repo string) (string, string, string, string) {
func parseRepoBranch(repo string) (string, string, string) {
var (
owner string
name string
branch string
env string
)

parts := strings.Split(repo, "@")
if len(parts) == 2 {
branch = parts[1]
repo = parts[0]
} else if len(parts) == 3 {
env = parts[2]
branch = parts[1]
repo = parts[0]
}

parts = strings.Split(repo, "/")
if len(parts) == 2 {
owner = parts[0]
name = parts[1]
}
return owner, name, branch, env
return owner, name, branch
}

func parseParams(paramList []string) (map[string]string, error) {
Expand Down

0 comments on commit 502fff6

Please sign in to comment.