Skip to content

Commit 3499506

Browse files
mavimoappleboy
authored andcommitted
exit after first error (#123)
Closes #121 WIP
1 parent 6c0b475 commit 3499506

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func main() {
9292
Usage: "execute commands",
9393
EnvVar: "PLUGIN_SCRIPT,SSH_SCRIPT",
9494
},
95+
cli.BoolFlag{
96+
Name: "script.stop",
97+
Usage: "stop script after first failure",
98+
EnvVar: "PLUGIN_SCRIPT_STOP",
99+
},
95100
cli.StringFlag{
96101
Name: "proxy.ssh-key",
97102
Usage: "private ssh key of proxy",
@@ -197,6 +202,7 @@ func run(c *cli.Context) error {
197202
Timeout: c.Duration("timeout"),
198203
CommandTimeout: c.Int("command.timeout"),
199204
Script: c.StringSlice("script"),
205+
ScriptStop: c.Bool("script.stop"),
200206
Secrets: c.StringSlice("secrets"),
201207
Envs: c.StringSlice("envs"),
202208
Debug: c.Bool("debug"),

plugin.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package main
22

33
import (
44
"fmt"
5+
"io"
56
"os"
67
"strconv"
78
"strings"
89
"sync"
910
"time"
1011

1112
"github.com/appleboy/easyssh-proxy"
12-
"io"
1313
)
1414

1515
const (
@@ -31,6 +31,7 @@ type (
3131
Timeout time.Duration
3232
CommandTimeout int
3333
Script []string
34+
ScriptStop bool
3435
Secrets []string
3536
Envs []string
3637
Proxy easyssh.DefaultConfig
@@ -82,7 +83,7 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
8283
}
8384
}
8485

85-
p.Config.Script = append(env, p.Config.Script...)
86+
p.Config.Script = append(env, p.scriptCommands()...)
8687

8788
if p.Config.Debug {
8889
p.log(host, "======ENV======")
@@ -179,3 +180,22 @@ func (p Plugin) Exec() error {
179180

180181
return nil
181182
}
183+
184+
func (p Plugin) scriptCommands() []string {
185+
numCommands := len(p.Config.Script)
186+
if p.Config.ScriptStop {
187+
numCommands *= 2
188+
}
189+
190+
commands := make([]string, numCommands)
191+
192+
for _, cmd := range p.Config.Script {
193+
if p.Config.ScriptStop {
194+
commands = append(commands, "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;")
195+
}
196+
197+
commands = append(commands, cmd)
198+
}
199+
200+
return commands
201+
}

0 commit comments

Comments
 (0)