Drone持续集成
Drone是一个用Go语言开发的基于容器运行的持续集成软件。
请注意,目前Drone1.0.0rc已经发布,但是本文所有内容均基于Drone0.8.0。
安装
Drone可以通过Docker安装,请参考https://0-8-0.docs.drone.io/installation/
运行时需要通过环境变量设置与Drone集成的版本控制系统。drone支持的版本控制系统包括GitHub,Gogs,GitLab等。我使用的是Gogs。
你可以修改后使用下面的compose文件。
version: '2' services: drone-server-gogs: image: drone/drone:0.8 ports: - 8020:8000 - 9000 volumes: - ./drone-gogs-data/:/var/lib/drone/ restart: always environment: - DRONE_OPEN=false - DRONE_HOST=https://drone-ci.example.com - DRONE_ADMIN=admin - DRONE_GOGS=true - DRONE_GOGS_URL=https://git.example.com/ - DRONE_SECRET=secret drone-agent-gogs: image: drone/agent:0.8 command: agent restart: always links: - drone-server-gogs depends_on: - drone-server-gogs volumes: - /var/run/docker.sock:/var/run/docker.sock environment: - DRONE_SERVER=drone-server-gogs:9000 - DRONE_SECRET=secret
构建Docker镜像
首先在项目根目录下新建.drone.yml
workspace: base: /app path: . pipeline: build: image: plugins/docker repo: registry.example.com/crawler username: username password: password tags: - ${DRONE_COMMIT_BRANCH} - latest registry: registry.example.com
管理密钥(secrets)
secrets的作用是存储构建过程中会使用到的敏感数据,如密码等,使其可以在pipeline中被引用,并且在yml文件中不可见。
在一些插件中会有预先设置好的secret字段,以ssh插件为例
pipeline: ssh: image: appleboy/drone-ssh host: foo.com username: root port: 22 secrets: [ ssh_password ] script: - echo hello - echo world
只需要在secrets列表中声明所用到的secrets字段,即可在该插件中引用该secret。
自定义密钥
前文所述的密钥仅限于插件中已经事先定义好的字段,如果需要自己定义一个密钥字段,则需要使用下面的配置将密钥映射到环境变量。
pipeline: ssh: image: appleboy/drone-ssh host: example.com secrets: - source: SSH_USERNAME target: ssh_username - source: SSH_PASSWORD target: ssh_password - source: GIT_USERNAME target: git_username - source: GIT_PASS target: git_pass - source: DEST_DIR target: dest_dir envs: [ git_username, git_pass, dest_dir ] script: - cd $DEST_DIR - git pull https://$GIT_USERNAME:$GIT_PASS@git.example.com/foo/bar.git master