mirror of
https://github.com/appleboy/drone-ssh.git
synced 2025-05-09 18:23:21 +08:00
Merge b6c3a4ffbe
into e6d4fa77d1
This commit is contained in:
commit
a55992ec33
12
plugin.go
12
plugin.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"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"))
|
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)
|
stdoutChan, stderrChan, doneChan, errChan, err := ssh.Stream(strings.Join(p.Config.Script, "\n"), p.Config.CommandTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/appleboy/easyssh-proxy"
|
"github.com/appleboy/easyssh-proxy"
|
||||||
@ -137,6 +138,23 @@ func TestStreamFromSSHCommand(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
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) {
|
func TestSSHScriptWithError(t *testing.T) {
|
||||||
plugin := Plugin{
|
plugin := Plugin{
|
||||||
Config: Config{
|
Config: Config{
|
||||||
|
Loading…
Reference in New Issue
Block a user