golang通过SSH复制文件和构建产物的CI/CD插件drone-scp的使用

Golang通过SSH复制文件和构建产物的CI/CD插件drone-scp的使用

drone-scp简介

drone-scp是一个通过SSH安全传输文件和构建产物的工具,支持作为独立二进制文件、Docker容器或Drone CI集成使用。

主要特性

  • 支持多线程并行文件传输
  • 支持源文件选择使用通配符模式
  • 支持传输文件到多个目标主机
  • 支持每个主机上的多个目标目录
  • 通过文件路径或原始内容灵活进行SSH密钥认证
  • 支持SSH ProxyCommand高级网络功能

安装方式

下载预编译二进制

预编译二进制可从release页面下载,支持以下操作系统:

  • Windows amd64/386
  • Linux arm/amd64/386
  • Darwin amd64/386

通过Go安装

export GO111MODULE=on
go get -u -v github.com/appleboy/drone-scp

编译二进制

export GOOS=linux
export GOARCH=amd64
export CGO_ENABLED=0
export GO111MODULE=on

go test -cover ./...

go build -v -a -tags netgo -o release/linux/amd64/drone-scp .

Docker镜像

构建Docker镜像:

make docker

使用示例

二进制方式使用

使用公钥认证

drone-scp --host example.com \
  --port 22 \
  --username appleboy \
  --key-path "${HOME}/.ssh/id_rsa" \
  --target /home/appleboy/test \
  --source your_local_folder_path

使用密码认证

drone-scp --host example.com \
  --port 22 \
  --username appleboy \
  --password xxxxxxx \
  --target /home/appleboy/test \
  --source your_local_folder_path

使用ssh-agent

# 启动本地ssh agent
eval `ssh-agent -s`

# 导入本地公钥
ssh-add

# 执行scp(无需密码或key-path参数)
drone-scp --host example.com \
  --port 22 \
  --username appleboy \
  --target /home/appleboy/test \
  --source your_local_folder_path

传输多个源文件/目录到多个目标

drone-scp --host example1.com \
  --host example2.com \
  --port 22 \
  --username appleboy \
  --password xxxxxxx
  --target /home/appleboy/test1 \
  --target /home/appleboy/test2 \
  --source your_local_folder_path_1
  --source your_local_folder_path_2

Docker方式使用

使用公钥认证

docker run --rm \
  -e SCP_HOST=example.com \
  -e SCP_USERNAME=xxxxxxx \
  -e SCP_PORT=22 \
  -e SCP_KEY_PATH="${HOME}/.ssh/id_rsa"
  -e SCP_SOURCE=SOURCE_FILE_LIST \
  -e SCP_TARGET=TARGET_FOLDER_PATH \
  -v $(pwd):$(pwd) \
  -w $(pwd) \
  appleboy/drone-scp

使用密码认证

docker run --rm \
  -e SCP_HOST=example.com \
  -e SCP_USERNAME=xxxxxxx \
  -e SCP_PORT=22 \
  -e SCP_PASSWORD="xxxxxxx"
  -e SCP_SOURCE=SOURCE_FILE_LIST \
  -e SCP_TARGET=TARGET_FOLDER_PATH \
  -v $(pwd):$(pwd) \
  -w $(pwd) \
  appleboy/drone-scp

传输多个源文件/目录到多个目标

docker run --rm \
  -e SCP_HOST=example1.com,example2.com \
  -e SCP_USERNAME=xxxxxxx \
  -e SCP_PASSWORD=xxxxxxx \
  -e SCP_PORT=22 \
  -e SCP_SOURCE=SOURCE_FILE_LIST_1,SOURCE_FILE_LIST_2 \
  -e SCP_TARGET=TARGET_FOLDER_PATH_1,TARGET_FOLDER_PATH_2 \
  -v $(pwd):$(pwd) \
  -w $(pwd) \
  appleboy/drone-scp

Drone CI集成使用

docker run --rm \
  -e PLUGIN_HOST=example.com \
  -e PLUGIN_USERNAME=xxxxxxx \
  -e PLUGIN_PASSWORD=xxxxxxx \
  -e PLUGIN_PORT=xxxxxxx \
  -e PLUGIN_SOURCE=SOURCE_FILE_LIST \
  -e PLUGIN_TARGET=TARGET_FOLDER_PATH \
  -e PLUGIN_RM=false \
  -e PLUGIN_DEBUG=true \
  -v $(pwd):$(pwd) \
  -w $(pwd) \
  appleboy/drone-scp

测试

运行测试:

make test

更多关于golang通过SSH复制文件和构建产物的CI/CD插件drone-scp的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang通过SSH复制文件和构建产物的CI/CD插件drone-scp的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用drone-scp插件在Drone CI/CD中通过SSH复制文件

drone-scp是一个用于Drone CI/CD系统的插件,它允许你在构建管道中通过SSH协议复制文件和构建产物到远程服务器。下面我将详细介绍如何使用这个插件。

基本概念

drone-scp插件主要功能:

  • 通过SSH连接远程服务器
  • 上传文件或目录
  • 支持SCP和SFTP协议
  • 可配置SSH密钥认证

安装与配置

1. 在.drone.yml中添加步骤

kind: pipeline
type: docker
name: default

steps:
- name: build
  image: golang:1.18
  commands:
    - go build -o myapp ./cmd/myapp

- name: deploy
  image: appleboy/drone-scp
  settings:
    host: your-server.com
    username: deploy
    port: 22
    key: 
      from_secret: ssh_key
    source: myapp
    target: /home/deploy/apps/
    rm: true

2. 配置SSH密钥

在Drone的仓库设置中添加一个名为ssh_key的secret,值为你的SSH私钥内容。

详细配置选项

- name: deploy
  image: appleboy/drone-scp
  settings:
    # 必填项
    host: your-server.com       # 远程服务器地址
    username: deploy            # SSH用户名
    port: 22                    # SSH端口
    key: 
      from_secret: ssh_key      # SSH私钥
    
    # 文件传输相关
    source: "dist/*"            # 要上传的文件/目录(支持通配符)
    target: "/var/www/html"     # 远程目标路径
    rm: true                    # 上传前删除目标目录内容
    overwrite: true             # 覆盖已存在文件
    timeout: 60                 # 超时时间(秒)
    
    # 可选配置
    command_timeout: 10         # 命令执行超时
    strip_components: 0         # 去除源路径中的前N级目录
    tar_tmp_path: "/tmp"        # 临时tar文件路径
    tar_exec: "tar"             # tar命令路径
    
    # 调试选项
    debug: true                 # 启用调试输出
    verbose: true               # 详细输出

实际示例

示例1:部署Go应用

steps:
- name: build
  image: golang:1.18
  commands:
    - go build -o app ./cmd/app
    - tar czf app.tar.gz app

- name: deploy
  image: appleboy/drone-scp
  settings:
    host: production.example.com
    username: deploy
    port: 2222
    key:
      from_secret: production_key
    source: app.tar.gz
    target: /tmp/
    strip_components: 1

示例2:部署前端项目

steps:
- name: build
  image: node:16
  commands:
    - npm install
    - npm run build

- name: deploy
  image: appleboy/drone-scp
  settings:
    host: web.example.com
    username: www-data
    port: 22
    key:
      from_secret: web_key
    source: dist/*
    target: /var/www/html
    rm: true
    overwrite: true

高级用法

1. 使用多个目标

- name: deploy
  image: appleboy/drone-scp
  settings:
    host:
      - server1.example.com
      - server2.example.com
    username: deploy
    key:
      from_secret: ssh_key
    source: build/*
    target: /opt/myapp/

2. 使用密码认证

- name: deploy
  image: appleboy/drone-scp
  settings:
    host: legacy.example.com
    username: olduser
    password:
      from_secret: ssh_password
    source: app
    target: /home/olduser/app

3. 执行远程命令

- name: deploy
  image: appleboy/drone-scp
  settings:
    host: db.example.com
    username: admin
    key:
      from_secret: db_key
    source: migrations/*.sql
    target: /tmp/migrations/
    command: 
      - "mysql -u root -p$$DB_PASSWORD mydb < /tmp/migrations/latest.sql"
    env:
      DB_PASSWORD:
        from_secret: db_password

常见问题解决

  1. 权限问题:确保目标目录对SSH用户可写
  2. 连接超时:检查防火墙设置和SSH服务是否运行
  3. 认证失败:验证SSH密钥是否正确,确保公钥已添加到远程服务器的~/.ssh/authorized_keys
  4. 路径问题:使用绝对路径更可靠

安全建议

  1. 使用SSH密钥而非密码
  2. 为CI/CD创建专用部署用户
  3. 限制部署用户的权限
  4. 定期轮换SSH密钥

通过合理配置drone-scp插件,你可以轻松实现构建产物的自动化部署,提高CI/CD流程的效率。

回到顶部