1
头图
In the development or production environment, we often engage in an automated deployment plan (commonly known as one-click deployment). The more popular one is the Gitlab+Jenkins implementation scheme, but this scheme occupies a large amount of memory, without 8G memory, it is difficult to run smoothly, and it is not fast to deploy. Recently discovered an artifact Drone, a lightweight CI/DI tool, combined with Gogs to use less than 1G of memory, a few lines of scripts can achieve automated deployment, I recommend it to everyone!

SpringBoot actual combat e-commerce project mall (40k+star) address: https://github.com/macrozheng/mall

Introduction to Drone

Drone is a continuous integration tool based on container technology. It uses simple YAML configuration files to complete complex automated construction, testing, and deployment tasks. There are already 22K+Star on Github.

Gogs installation

We will use the lightweight Gogs to build the Git warehouse. Here is just a brief introduction to the installation steps. For specific use, please refer to "Github Star 34K+Star, this open source project helps you build Git services in seconds! " .
  • First, you need to download the Docker image of Gogs;
docker pull gogs/gogs
  • After the download is complete, run Gogs in the Docker container;
docker run -p 10022:22 -p 10080:3000 --name=gogs \
-e TZ="Asia/Shanghai" \
-v /mydata/gogs:/data  \
-d gogs/gogs
  • After Gogs runs successfully, visit the web page address and register an account: http://192.168.5.78 :10080

Drone installation

Next, we install Drone, which is worthy of being a container-based CI/DI tool. It is very convenient to use Docker to install!
  • First download the mirror images of Drone's Server and Runner;
# Drone的Server
docker pull drone/drone:1
# Drone的Runner
docker pull drone-runner-docker:1
  • Here is the concept of Server and Runner, let's understand it first;

    • Server: Provides a web page for the management of Drone, which is used to manage the pipeline tasks in the warehouse obtained from Git.
    • Runner: A separate daemon process that will poll the Server to obtain the pipeline tasks that need to be executed, and then execute them.
  • Next, let's install drone-server , just use the following command;
docker run \
  -v /mydata/drone:/data \
  -e DRONE_AGENTS_ENABLED=true \
  -e DRONE_GOGS_SERVER=http://192.168.5.78:10080 \
  -e DRONE_RPC_SECRET=dronerpc666 \
  -e DRONE_SERVER_HOST=192.168.5.78:3080 \
  -e DRONE_SERVER_PROTO=http \
  -e DRONE_USER_CREATE=username:macro,admin:true \
  -e TZ="Asia/Shanghai" \
  -p 3080:80 \
  --restart=always \
  --detach=true \
  --name=drone \
  drone/drone:1
  • There are many configuration parameters here, which will be explained in a unified manner below;

    • DRONE_GOGS_SERVER: used to configure the Gogs service address.
    • DRONE_RPC_SECRET: Drone's shared secret key, used to verify the RPC connection to the server, the server and runner need to provide the same secret key.
    • DRONE_SERVER_HOST: Used to configure the externally accessible address of the Drone server.
    • DRONE_SERVER_PROTO: Used to configure the externally accessible protocol of the Drone server, which must be http or https.
    • DRONE_USER_CREATE: Create an administrator account, which needs to be registered in Gogs.
  • Next, install drone-runner-docker . When there are tasks that need to be executed, a temporary container will be started to execute pipeline tasks;
docker run -d \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -e DRONE_RPC_PROTO=http \
  -e DRONE_RPC_HOST=192.168.5.78:3080 \
  -e DRONE_RPC_SECRET=dronerpc666 \
  -e DRONE_RUNNER_CAPACITY=2 \
  -e DRONE_RUNNER_NAME=runner-docker \
  -e TZ="Asia/Shanghai" \
  -p 3000:3000 \
  --restart always \
  --name runner-docker \
  drone/drone-runner-docker:1
  • There are many configuration parameters here, which are explained below.

    • DRONE_RPC_PROTO: Used to configure the protocol used to connect to the Drone server, which must be http or https.
    • DRONE_RPC_HOST: Used to configure the access address of the Drone server, the runner will connect to the server to obtain the pipeline task and execute it.
    • DRONE_RPC_SECRET: Used to configure the shared secret key for connecting to the Drone server.
    • DRONE_RUNNER_CAPACITY: Limit the number of pipeline tasks that the runner can execute concurrently.
    • DRONE_RUNNER_NAME: Customize the name of the runner.

Drone use

  • Let's visit the console page of Drone. The first time you log in, you need to enter the account password (the account registered in Gogs). The access address is: http://192.168.5.78 :3080/

  • At this time, our project in Gogs will now be in the list, if not, click the SYNC button;

  • Next we need to set the warehouse, set the warehouse to Trusted (otherwise the container created by Drone cannot mount the directory to the host), and finally click the SAVE button to save;

  • After the save is successful, a web hook will be automatically configured in Gogs. When we push the code to Gogs, this hook will be triggered, and then the pipeline task in Drone will be executed;

  • Pull to the bottom, we can send a test push, the push is successful, a green √ will be displayed;

  • At this time, we found in Drone that the pipeline execution failed, because we quoted ssh_password in Secret in the script;

  • Just add a Secret in the warehouse settings. Secret is used to store passwords. This password can only be used or deleted, and cannot be viewed;

  • In ACTIVITY FEED use RESTART can re-execute the pipeline, has found that successful implementation.

Scripting

When we push the code to the Git repository, the web hook will be automatically triggered, and then Drone will clone the code from the Git repository, and then .drone.yml configuration in the project directory. Next, let’s take a look at how this script is written. of.
  • First of all, let's understand what operations are configured in the workflow configured .drone.yml

  • Here is a complete .drone.yml with detailed annotations, and you can understand it by looking at it!
kind: pipeline # 定义对象类型,还有secret和signature两种类型
type: docker # 定义流水线类型,还有kubernetes、exec、ssh等类型
name: mall-tiny-drone # 定义流水线名称

steps: # 定义流水线执行步骤,这些步骤将顺序执行
  - name: package # 流水线名称
    image: maven:3-jdk-8 # 定义创建容器的Docker镜像
    volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置
      - name: maven-cache
        path: /root/.m2 # 将maven下载依赖的目录挂载出来,防止重复下载
      - name: maven-build
        path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
    commands: # 定义在Docker容器中执行的shell命令
      - mvn clean package # 应用打包命令
      - cp target/mall-tiny-drone-1.0-SNAPSHOT.jar /app/build/mall-tiny-drone-1.0-SNAPSHOT.jar
      - cp Dockerfile /app/build/Dockerfile
      - cp run.sh /app/build/run.sh

  - name: build-start
    image: appleboy/drone-ssh # SSH工具镜像
    settings:
      host: 192.168.5.78 # 远程连接地址
      username: root # 远程连接账号
      password:
        from_secret: ssh_password # 从Secret中读取SSH密码
      port: 22 # 远程连接端口
      command_timeout: 5m # 远程执行命令超时时间
      script:
        - cd /mydata/maven/build # 进入宿主机构建目录
        - chmod +x run.sh # 更改为可执行脚本
        - ./run.sh # 运行脚本打包应用镜像并运行

volumes: # 定义流水线挂载目录,用于共享数据
  - name: maven-build
    host:
      path: /mydata/maven/build # 从宿主机中挂载的目录
  - name: maven-cache
    host:
      path: /mydata/maven/cache

to sum up

Compared with Jenkins's complex graphical interface operation, Drone uses scripts to define pipeline tasks is undoubtedly simpler and more intuitive. Drone is more lightweight, with less memory usage and fast response speed! What is Jenkins for automated deployment? Isn't it good to give Git the entire CI/DI function directly?

Reference

Project source address

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-drone

This article GitHub https://github.com/macrozheng/mall-learning has been included, welcome to Star!

macrozheng
1.1k 声望1.3k 粉丝