使用drone-cli的drone exec命令在本地执行适用于drone.io的流水线

有一种名为drone.io的CI/CD服务,可以自己运营的开源版本也可得到。

现在就OSS版来说,它在接收hook的服务器和执行任务的代理之间有一个明显的分离,如果要按照指南进行设置,需要准备一台专用服务器。

顺便说一下,在需要认证和OAuth联动的地方,基本构建是基于webhook(也可以使用CLI),因此在想要确认一下感觉时,使用服务器/代理配置有点夸张。

我在查看文件时发现了一个叫做”drone exec”的工具,我决定试一试。

安装drone-cli

CLI安装 | docs.drone.io

由于我手上的是macOS系统,所以我会使用HomeBrew来进行。

brew tap drone/drone
brew install drone

现在我已经可以使用drone-cli了。

$ drone --version
drone version 0.7.0

顺带一提,我们应该运行Docker for Mac。

请在本地运行无人机执行

因为它看起来很好,所以我要运行一个使用Redis作为后端的代码示例。

在Redis上为例 | docs.drone.io

将.drone.yml文件放置在适当的位置。

$ tree ./ -a
./
├── .drone.yml
└── hello.text

0 directories, 2 files

从Example开始,将.drone.yml的内容设定为如下。

workspace:
  base: /pipeline
  path: src

pipeline:
  build:
    image: redis
    commands:
      - pwd
      - find ./
      - redis-cli -h redis ping
      - redis-cli -h redis set FOO bar
      - redis-cli -h redis get FOO

services:
  redis:
    image: redis

我本来以为工作空间的设定是默认的,但我试着添加了一些描述。

通过无人机执行在本地执行。

只需试试执行即可。

$ drone exec 
1:C 07 Jun 07:52:31.302 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 07 Jun 07:52:31.304 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 07 Jun 07:52:31.304 # Server started, Redis version 3.2.8
1:M 07 Jun 07:52:31.304 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 07 Jun 07:52:31.304 * The server is now ready to accept connections on port 6379
+ pwd
/pipeline/src
+ find ./
./
./.drone.yml
./hello.text
+ redis-cli -h redis ping
PONG
+ redis-cli -h redis set FOO bar
OK
+ redis-cli -h redis get FOO
bar

$ echo $?
0

做得很好。

在查看drone exec –help命令时会发现有一个–local选项,但指定DRONE_SERVER / DRONE_TOKEN也会被忽略,因此可以认为drone exec是仅限于本地使用的。

由于drone.io本身使用了docker-engine,所以即使在本地也可以执行类似于代理的功能。它的功能确实很好。