4

In the daily development, testing or operation and maintenance environment, we have an increasingly strong demand for automated deployment.

Friends who have used SVN should know the function of hooks. Indeed, this "special" function is often used in the daily internal test environment of the enterprise. Through this function, can realize the automatic update and release code function .

Later, continuous integration and continuous deployment were gradually introduced, the most commonly used is to use Jenkins to cooperate with other code management tools or platforms to achieve the above functions. I have written a lot of introduction articles before: Jenkins+Maven+Svn realizes automatic code packaging and release , uses Gitlab+Jenkins to realize multi-branch automatic independent deployment , interested readers can consult it by themselves.

However, based on the above model, everyone knows that there are some shortcomings. The first is that the deployment environment is more troublesome, a lot of components need to be installed, and the second is that the server itself requires higher resource conditions. I recently discovered a very you still using Jenkins? Hurry up and take a look at these alternatives! ), which is a lightweight player than the above tools, but it can also achieve automated deployment functions, so, Today, the migrant worker will share with you this tool for lifting the sky.

Introduction

图片

Drone is a continuous delivery system based on container technology. Drone uses a simple YAML configuration file to define and execute the Pipelines in the Docker container. There are currently 23.2K+ Stars on Github, indicating that they are very popular.

图片

Drone seamlessly integrates with popular source code management systems, including GitHub, GitHub Enterprise, Bitbucket, etc.

Pre-environment configuration

Use github as a code management repository (others are also available, this is more convenient)

Open github.com, click on the avatar, select Settings/Developer settings/OAuth Apps, and then click New OAuth App to create an OAuth application.

图片

图片

Among them, http://drone.mingongge.com is the domain name where you deploy Drone. Please pay attention to the Authorization callback URL address.

图片

Keep the ClientID and ClientSecret in it, and you will need to use them later.

Deploy Drone

Note: The installation of Drone requires a drone-server and drone-runner,

drone-runner is not required, and the official does not recommend installing runner and server on the same server. If you want to install this way, you can set DRONE_AGENTS_ENABLED=false . drone-server will be the default runner. This article installs runner and server. On the same server.

# Drone的Server
docker pull drone/drone:1

# Drone的Runner
docker pull drone/drone-runner-docker:1
Install drone-server
docker run \  
--volume=/var/lib/drone:/data \  
--env=DRONE_GITHUB_CLIENT_ID={{DRONE_GITHUB_CLIENT_ID}} \  
--env=DRONE_GITHUB_CLIENT_SECRET={{DRONE_GITHUB_CLIENT_SECRET}} \  
--env=DRONE_RPC_SECRET={{DRONE_RPC_SECRET}} \  
--env=DRONE_SERVER_HOST={{DRONE_SERVER_HOST}} \  
--env=DRONE_SERVER_PROTO={{DRONE_SERVER_PROTO}} \  
--env=DRONE_USER_CREATE=username:USER_NAME,admin:true \  
--publish=80:80 \  
--publish=443:443 \  
--restart=always \  
--detach=true \  
--name=drone \  
drone/drone:1

Parameter Description

DRONE_GITHUB_CLIENT_ID
#在上一步中生成的 GitHub oauth 客户端 ID
DRONE_GITHUB_CLIENT_SECRET
#在上一步中生成的 GitHub oauth 客户端密钥
DRONE_RPC_SECRET
#提供在上一步中生成的共享密钥。这用于验证服务器和运行程序之间的 rpc 连接。服务器和运行器必须提供相同的秘钥值。
DRONE_SERVER_HOST
#服务器对外提供的或访问的域名,也可以是IP地址,用IP+端口的方式来配置
DRONE_SERVER_PROTO
#必需的字符串值提供您的外部协议方案。此值应设置为 http 或 https。
DRONE_USER_CREATE
#初始管理员用户
Install drone-runner-docker
$ docker run -d \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -e DRONE_RPC_PROTO=https \
  -e DRONE_RPC_HOST=drone.mingongge.com \
  -e DRONE_RPC_SECRET=super-duper-secret \
  -e DRONE_RUNNER_CAPACITY=2 \
  -e DRONE_RUNNER_NAME=${HOSTNAME} \
  -p 3000:3000 \
  --restart always \
  --name runner \
  drone/drone-runner-docker:1

Parameter Description

DRONE_RPC_HOST
#提供 Drone 服务器的主机名(和可选端口)。运行器连接到主机地址处的服务器以接收管道以供执行。
DRONE_RPC_PROTO
#提供用于连接到您的 Drone 服务器的协议。该值必须是 http 或 https。
DRONE_RPC_SECRET
#提供用于与您的 Drone 服务器进行身份验证的共享密钥。这必须与您的 Drone 服务器配置中定义的秘密相匹配。

Manage and use operations

Then open the browser, enter the server's domain name or IP + port, you can access the drone management page.

图片

If you have created a project code repository on github, the page here will be displayed. If it is not displayed, click SYNC to synchronize.

图片

Next, we need to set the warehouse, click on the project and then set the warehouse to Trusted, click SAVE to save.

图片

Then a Webhooks will be generated on github, through which the drone will be triggered to execute the job.

图片

Create Secrets

图片

In fact, the entire process is very similar to Jenkins. When submitting a code update to the code repository, Webhooks will be automatically triggered, and then Drone will clone the code from the code repository, and then pass this .drone.yml (equivalent to the pom.xml file used by java) Configure and execute the corresponding pipeline operation.

For example, write a simple file like this

kind: pipeline
type: kubernetes
name: default

steps:
- name: drone_deploy
  image: alpine
  commands:
  - echo hello
  - echo world

Then execute the operation and push it to the code warehouse

git add .
git commit -m 'deploy test by mingongge'
git remote add origin mingongge@github.com:mingongge/dronetest.git
git push -u origin master

View the results through the drone management page

图片

And the operation of the pipeline is also successful, and the relevant information is returned as follows

图片

Compared with the operation of Jenkins, it seems to be much simpler. The key operation is to write the configuration file .drone.yml. The other operations are graphical operations and there is no special difficulty.

Official operation document: Official document: https://docs.drone.io/

Time is limited and the tutorial may be incomplete. After you deploy it on your own, you are welcome to come and discuss this super awesome artifact, and jointly create your own CI/CD pipeline.


民工哥
26.4k 声望56.7k 粉丝

10多年IT职场老司机的经验分享,坚持自学一路从技术小白成长为互联网企业信息技术部门的负责人。2019/2020/2021年度 思否Top Writer