mirror of
https://github.com/appleboy/drone-scp.git
synced 2026-06-16 14:49:20 +08:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b73ec894ab | |||
| 05eba8f809 | |||
| a2493062f7 | |||
| b2b346a0ca | |||
| 9d8f5ac419 | |||
| 5d93e7b8ab | |||
| bf812f8e29 | |||
| 8814cfe72c | |||
| 954e0069e6 | |||
| 03524ed8bd | |||
| 531df19c8c | |||
| df8214b645 | |||
| c85ca1ffd2 | |||
| 933b45bc15 | |||
| 15344d67ae | |||
| cf9e6f260d | |||
| cfa325a8c4 |
@@ -351,13 +351,6 @@ steps:
|
||||
username:
|
||||
from_secret: docker_username
|
||||
|
||||
- name: microbadger
|
||||
pull: always
|
||||
image: plugins/webhook:1
|
||||
settings:
|
||||
url:
|
||||
from_secret: microbadger_url
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/heads/master
|
||||
|
||||
@@ -11,35 +11,21 @@ image: appleboy/drone-scp
|
||||
The SCP plugin copy files and artifacts to target host machine via SSH. The below pipeline configuration demonstrates simple usage:
|
||||
|
||||
```yaml
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
target: /home/deploy/web
|
||||
source: release.tar.gz
|
||||
```
|
||||
|
||||
Example configuration with custom username, password and port:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
+ username: appleboy
|
||||
+ password: 12345678
|
||||
+ port: 4430
|
||||
target: /home/deploy/web
|
||||
source: release.tar.gz
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
username: foo
|
||||
password: bar
|
||||
port: 22
|
||||
target: /var/www/deploy/${DRONE_REPO_OWNER}/${DRONE_REPO_NAME}
|
||||
source: release.tar.gz
|
||||
```
|
||||
|
||||
Example configuration with multiple source and target folder:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
@@ -54,8 +40,7 @@ pipeline:
|
||||
Example configuration with multiple host:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
- host: example.com
|
||||
@@ -69,8 +54,7 @@ pipeline:
|
||||
Example configuration with wildcard pattern of source list:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
@@ -86,9 +70,8 @@ pipeline:
|
||||
Remove target folder before copy files and artifacts to target:
|
||||
|
||||
```diff
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
host: example.com
|
||||
settings:
|
||||
target: /home/deploy/web
|
||||
source: release.tar.gz
|
||||
@@ -98,7 +81,7 @@ Remove target folder before copy files and artifacts to target:
|
||||
Example for remove the specified number of leading path elements:
|
||||
|
||||
```diff
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host: example.com
|
||||
@@ -110,8 +93,7 @@ Example for remove the specified number of leading path elements:
|
||||
Example configuration using `SSHProxyCommand`:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
@@ -129,8 +111,7 @@ pipeline:
|
||||
Example configuration using password from secrets:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
@@ -149,29 +130,27 @@ pipeline:
|
||||
Example configuration using command timeout:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
- example1.com
|
||||
- example2.com
|
||||
- example1.com
|
||||
- example2.com
|
||||
user: ubuntu
|
||||
password:
|
||||
from_secret: ssh_password
|
||||
from_secret: ssh_password
|
||||
port: 22
|
||||
- command_timeout: 120
|
||||
+ command_timeout: 2m
|
||||
target: /home/deploy/web
|
||||
source:
|
||||
- release/*.tar.gz
|
||||
- release/*.tar.gz
|
||||
```
|
||||
|
||||
Example configuration for ignore list:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
@@ -215,14 +194,23 @@ rm
|
||||
: remove target folder before copy files and artifacts
|
||||
|
||||
timeout
|
||||
: timeout is the maximum amount of time for the TCP connection to establish
|
||||
: Timeout is the maximum amount of time for the ssh connection to establish, default is 30 seconds.
|
||||
|
||||
command_timeout
|
||||
: timeout is the maximum amount of time for execute command
|
||||
: Command timeout is the maximum amount of time for the execute commands, default is 10 minutes.
|
||||
|
||||
strip_components
|
||||
: remove the specified number of leading path elements
|
||||
|
||||
tar_tmp_path
|
||||
: temporary path for tar file on the dest host
|
||||
|
||||
tar_exec
|
||||
: alternative `tar` executable to on the dest host
|
||||
|
||||
overwrite
|
||||
: use `--overwrite` flag with tar
|
||||
|
||||
proxy_host
|
||||
: proxy hostname or IP
|
||||
|
||||
|
||||
@@ -36,8 +36,7 @@ Copy files and artifacts via SSH using a binary, docker or [Drone CI](http://doc
|
||||
`v1.5.0`: change command timeout flag to `Duration`. See the following setting:
|
||||
|
||||
```diff
|
||||
pipeline:
|
||||
scp:
|
||||
- name: scp files
|
||||
image: appleboy/drone-scp
|
||||
settings:
|
||||
host:
|
||||
@@ -65,6 +64,7 @@ The pre-compiled binaries can be downloaded from [release page](https://github.c
|
||||
With `Go` installed
|
||||
|
||||
```sh
|
||||
export GO111MODULE=on
|
||||
go get -u -v github.com/appleboy/drone-scp
|
||||
```
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ require (
|
||||
github.com/appleboy/easyssh-proxy v1.2.0
|
||||
github.com/fatih/color v1.7.0
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/mattn/go-colorable v0.1.2 // indirect
|
||||
github.com/mattn/go-isatty v0.0.9 // indirect
|
||||
github.com/mattn/go-colorable v0.1.4 // indirect
|
||||
github.com/mattn/go-isatty v0.0.10 // indirect
|
||||
github.com/stretchr/testify v1.4.0
|
||||
github.com/urfave/cli v1.22.1
|
||||
)
|
||||
|
||||
@@ -11,11 +11,11 @@ github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
|
||||
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
|
||||
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
|
||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
|
||||
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
|
||||
@@ -33,8 +33,8 @@ golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25/go.mod h1:djNgcEr1/C05ACk
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
|
||||
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
|
||||
@@ -56,12 +56,13 @@ func main() {
|
||||
Name: "timeout",
|
||||
Usage: "connection timeout",
|
||||
EnvVar: "PLUGIN_TIMEOUT,SCP_TIMEOUT,INPUT_TIMEOUT",
|
||||
Value: 30 * time.Second,
|
||||
},
|
||||
cli.DurationFlag{
|
||||
Name: "command.timeout,T",
|
||||
Usage: "command timeout",
|
||||
EnvVar: "PLUGIN_COMMAND_TIMEOUT,SSH_COMMAND_TIMEOUT,INPUT_COMMAND_TIMEOUT",
|
||||
Value: 60 * time.Second,
|
||||
Value: 10 * time.Minute,
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "key, k",
|
||||
@@ -193,11 +194,21 @@ func main() {
|
||||
EnvVar: "PLUGIN_TAR_EXEC,SCP_TAR_EXEC,INPUT_TAR_EXEC",
|
||||
Value: "tar",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "tar.tmp-path",
|
||||
Usage: "Temporary path for tar file on the dest host",
|
||||
EnvVar: "PLUGIN_TAR_TMP_PATH,SCP_TAR_TMP_PATH",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "debug",
|
||||
Usage: "remove target folder before upload data",
|
||||
EnvVar: "PLUGIN_DEBUG,DEBUG,INPUT_DEBUG",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "overwrite",
|
||||
Usage: "use --overwrite flag with tar",
|
||||
EnvVar: "PLUGIN_OVERWRITE,SCP_OVERWRITE,INPUT_OVERWRITE",
|
||||
},
|
||||
}
|
||||
|
||||
// Override a template
|
||||
@@ -273,6 +284,8 @@ func run(c *cli.Context) error {
|
||||
Debug: c.Bool("debug"),
|
||||
StripComponents: c.Int("strip.components"),
|
||||
TarExec: c.String("tar.exec"),
|
||||
TarTmpPath: c.String("tar.tmp-path"),
|
||||
Overwrite: c.Bool("overwrite"),
|
||||
Proxy: easyssh.DefaultConfig{
|
||||
Key: c.String("proxy.ssh-key"),
|
||||
KeyPath: c.String("proxy.key-path"),
|
||||
|
||||
@@ -239,14 +239,6 @@
|
||||
ignore_missing: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'microbadger',
|
||||
image: 'plugins/webhook:1',
|
||||
pull: 'always',
|
||||
settings: {
|
||||
url: { 'from_secret': 'microbadger_url' },
|
||||
},
|
||||
},
|
||||
],
|
||||
depends_on: depends_on,
|
||||
trigger: {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -57,8 +58,10 @@ type (
|
||||
Remove bool
|
||||
StripComponents int
|
||||
TarExec string
|
||||
TarTmpPath string
|
||||
Proxy easyssh.DefaultConfig
|
||||
Debug bool
|
||||
Overwrite bool
|
||||
}
|
||||
|
||||
// Plugin values.
|
||||
@@ -194,6 +197,32 @@ type fileList struct {
|
||||
Source []string
|
||||
}
|
||||
|
||||
func (p *Plugin) buildArgs(target string) []string {
|
||||
args := []string{}
|
||||
|
||||
args = append(args,
|
||||
p.Config.TarExec,
|
||||
"-xf",
|
||||
p.DestFile,
|
||||
)
|
||||
|
||||
if p.Config.StripComponents > 0 {
|
||||
args = append(args, "--strip-components")
|
||||
args = append(args, strconv.Itoa(p.Config.StripComponents))
|
||||
}
|
||||
|
||||
if p.Config.Overwrite {
|
||||
args = append(args, "--overwrite")
|
||||
}
|
||||
|
||||
args = append(args,
|
||||
"-C",
|
||||
target,
|
||||
)
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
// Exec executes the plugin.
|
||||
func (p *Plugin) Exec() error {
|
||||
if len(p.Config.Host) == 0 {
|
||||
@@ -261,6 +290,9 @@ func (p *Plugin) Exec() error {
|
||||
},
|
||||
}
|
||||
|
||||
// upload file to the tmp path
|
||||
p.DestFile = fmt.Sprintf("%s%s", p.Config.TarTmpPath, p.DestFile)
|
||||
|
||||
// Call Scp method with file you want to upload to remote server.
|
||||
p.log(host, "scp file to server.")
|
||||
err := ssh.Scp(tar, p.DestFile)
|
||||
@@ -298,10 +330,18 @@ func (p *Plugin) Exec() error {
|
||||
|
||||
// untar file
|
||||
p.log(host, "untar file", p.DestFile)
|
||||
if p.Config.StripComponents > 0 {
|
||||
_, _, _, err = ssh.Run(fmt.Sprintf("%s -xf %s --strip-components=%d -C %s", p.Config.TarExec, p.DestFile, p.Config.StripComponents, target), p.Config.CommandTimeout)
|
||||
} else {
|
||||
_, _, _, err = ssh.Run(fmt.Sprintf("%s -xf %s -C %s", p.Config.TarExec, p.DestFile, target), p.Config.CommandTimeout)
|
||||
commamd := strings.Join(p.buildArgs(target), " ")
|
||||
if p.Config.Debug {
|
||||
fmt.Println("$", commamd)
|
||||
}
|
||||
outStr, errStr, _, err := ssh.Run(commamd, p.Config.CommandTimeout)
|
||||
|
||||
if outStr != "" {
|
||||
p.log(host, "output: ", outStr)
|
||||
}
|
||||
|
||||
if errStr != "" {
|
||||
p.log(host, "error: ", errStr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -267,6 +268,7 @@ func TestIgnoreList(t *testing.T) {
|
||||
Target: []string{filepath.Join(u.HomeDir, "ignore")},
|
||||
CommandTimeout: 60 * time.Second,
|
||||
TarExec: "tar",
|
||||
Debug: true,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -456,3 +458,79 @@ func TestRemoveDestFile(t *testing.T) {
|
||||
err = plugin.removeDestFile(ssh)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestPlugin_buildArgs(t *testing.T) {
|
||||
type fields struct {
|
||||
Repo Repo
|
||||
Build Build
|
||||
Config Config
|
||||
DestFile string
|
||||
}
|
||||
type args struct {
|
||||
target string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "default command",
|
||||
fields: fields{
|
||||
Config: Config{
|
||||
Overwrite: false,
|
||||
TarExec: "tar",
|
||||
},
|
||||
DestFile: "foo.tar",
|
||||
},
|
||||
args: args{
|
||||
target: "foo",
|
||||
},
|
||||
want: []string{"tar", "-xf", "foo.tar", "-C", "foo"},
|
||||
},
|
||||
{
|
||||
name: "strip components",
|
||||
fields: fields{
|
||||
Config: Config{
|
||||
Overwrite: false,
|
||||
TarExec: "tar",
|
||||
StripComponents: 2,
|
||||
},
|
||||
DestFile: "foo.tar",
|
||||
},
|
||||
args: args{
|
||||
target: "foo",
|
||||
},
|
||||
want: []string{"tar", "-xf", "foo.tar", "--strip-components", "2", "-C", "foo"},
|
||||
},
|
||||
{
|
||||
name: "overwrite",
|
||||
fields: fields{
|
||||
Config: Config{
|
||||
TarExec: "tar",
|
||||
StripComponents: 2,
|
||||
Overwrite: true,
|
||||
},
|
||||
DestFile: "foo.tar",
|
||||
},
|
||||
args: args{
|
||||
target: "foo",
|
||||
},
|
||||
want: []string{"tar", "-xf", "foo.tar", "--strip-components", "2", "--overwrite", "-C", "foo"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
p := &Plugin{
|
||||
Repo: tt.fields.Repo,
|
||||
Build: tt.fields.Build,
|
||||
Config: tt.fields.Config,
|
||||
DestFile: tt.fields.DestFile,
|
||||
}
|
||||
if got := p.buildArgs(tt.args.target); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("Plugin.buildArgs() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user