From 2daab5e1aff05050040c5c0cc86e94bf85c71af7 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 24 Jul 2017 13:10:11 +0200 Subject: [PATCH 1/3] Expand DRONE* variables before uploading the script --- plugin.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugin.go b/plugin.go index d48db66..afeeb79 100644 --- a/plugin.go +++ b/plugin.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "strconv" "strings" "sync" @@ -86,6 +87,17 @@ func (p Plugin) Exec() error { }, } + // expand DRONE env variables in script (like DRONE_DEPLOY_TO) + for index, _ := range p.Config.Script { + parts := strings.Split(p.Config.Script[index], " ") + for i, _ := range parts { + if strings.Contains(parts[i], "DRONE") { + parts[i] = os.ExpandEnv(parts[i]) + } + } + p.Config.Script[index] = strings.Join(parts, " ") + } + p.log(host, "commands: ", strings.Join(p.Config.Script, "\n")) stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout) if err != nil { From 4b6bf763c8af19740dbef86f1c9a1e8727af8849 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 24 Jul 2017 15:14:30 +0200 Subject: [PATCH 2/3] Make linter happy --- plugin.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin.go b/plugin.go index afeeb79..211d6c5 100644 --- a/plugin.go +++ b/plugin.go @@ -88,9 +88,9 @@ func (p Plugin) Exec() error { } // expand DRONE env variables in script (like DRONE_DEPLOY_TO) - for index, _ := range p.Config.Script { + for index := range p.Config.Script { parts := strings.Split(p.Config.Script[index], " ") - for i, _ := range parts { + for i := range parts { if strings.Contains(parts[i], "DRONE") { parts[i] = os.ExpandEnv(parts[i]) } From b6c3a4ffbed817e5999c21115a5bb020d1eecb57 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 24 Jul 2017 19:22:41 +0200 Subject: [PATCH 3/3] Add test to fix coverage The test should anaylize the output of the command to make sure it worked but I didn't find any suitable function --- plugin_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/plugin_test.go b/plugin_test.go index 8fe9709..3d3653d 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -1,6 +1,7 @@ package main import ( + "os" "testing" "github.com/appleboy/easyssh-proxy" @@ -137,6 +138,23 @@ func TestStreamFromSSHCommand(t *testing.T) { assert.Nil(t, err) } +func TestDroneEnvSubstitution(t *testing.T) { + os.Setenv("DRONE_DEPLOY_TO", "dev") + plugin := Plugin{ + Config: Config{ + Host: []string{"localhost", "127.0.0.1"}, + UserName: "drone-scp", + Port: 22, + KeyPath: "./tests/.ssh/id_rsa", + Script: []string{"echo $DRONE_DEPLOY_TO $PATH"}, + CommandTimeout: 60, + }, + } + + err := plugin.Exec() + assert.Nil(t, err) +} + func TestSSHScriptWithError(t *testing.T) { plugin := Plugin{ Config: Config{