In a development or production environment, we often have an automated deployment solution (commonly known as one-click deployment). One of the more popular is the GitLab +Jenkins implementation, but it takes up a lot of memory, doesn’t have 8GB of memory, is difficult to run smoothly, and isn’t fast to deploy. Recently found a magic Drone, lightweight CI/DI tool, combined with GOGS use memory occupy less than 1G, a few lines of this can achieve automated deployment, recommended to you!

Mall SpringBoot practical electricity project (40 k + star) address: https://github.com/macrozheng/mall

Drone profile

Drone is a continuous integration tool based on container technology that uses simple YAML configuration files to complete complex automated build, test and deploy tasks. It has 22K+ STAR available on GitHub.

Gogs installation

We will use the lightweight Gogs to set up a Git repository. This is just a brief description of the installation steps, so you can refer to the details
GitHub 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 downloading, 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
  • Gogs after the success of the operation, access to the Web page address and registered account: http://192.168.5.78:10080

  • Then bring our SpringBoot projectmall-tiny-droneThe source code can be uploaded, the project address:https://github.com/macrozheng…

Drone installation

Next we install Drone, worthy of being a CI/DI tool based on the container, it is very convenient to use Docker to install!

  • First, download the image of Drone’s Server and Runner;
# Drone Server Docker Pull Drone/Drone :1 # Drone Runner Docker Pull Drone -Runner-Docker :1 # Drone
  • There is a concept of Server and Runner, so let’s understand it first;

    • Server: Web page is provided for the management of Drone, which is used to manage pipeline tasks in the warehouse obtained from Git.
    • Runner: A separate daemon that polls the Server for pipeline tasks that need to be executed and then executes.
  • Now let’s install itdrone-server, 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 a lot of configuration parameters here, the following unified explanation;

    • DRONE_GOGS_SERVER: Used to configure the GOGS service address.
    • DRONE_RPC_SECRET: Drone_rpc_secret: The shared secret key of the Drone, used to verify the RPC connection to the server. Server and runner need to provide the same secret key.
    • DRONE_SERVER_HOST: Used to configure the address accessible externally to the Drone Server.
    • DRONE_SERVER_PROTO: The protocol used to configure the Drone Server to be externally accessible, which must be HTTP or HTTPS.
    • DRONE_USER_CREATE: Create an administrator account that needs to be registered with GOGS.
  • Next installdrone-runner-docker, when there is a task to be performed, a temporary container will be started to perform the pipeline task;
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 a lot of configuration parameters here, and they will be explained below.

    • DRONE_RPC_PROTO: Used to configure the protocol 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 to connect to the Drone Server.
    • DRONE_RUNNER_CAPACITY: Limits the number of pipeline tasks that the runner can perform concurrently.
    • DRONE_RUNNER_NAME: Name of the custom runner.

Drone use

  • Let’s visit the Drone console page, login for the first time need to input password (registered in Gogs account), access to the address: http://192.168.5.78:3080/

  • The items we have in the Gogs will now be in the list, and if not, you can click on itSYNCButton;

  • Next we need to set up the warehouse and set the warehouse toTrusted(otherwise the container created by Drone cannot mount the directory to the host), and finally clickSAVEButton save;

  • After saving successfully, 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 performed.

  • If we pull it down to the bottom, we can send a test push. A green √ will be displayed if the push succeeds.

  • At this time, we found that the pipeline execution failed in Drone, because we referenced the Secret in the scriptssh_password;

  • Add a Secret in the setting of the warehouse, Secret is specially used to store the password, the password can only be used or deleted, can not be viewed;

  • inACTIVITY FEEDThe use ofRESTARTThe pipeline can be re-executed and found to have been successfully executed.

Write a script

When we Push code into the Git repository, Web hooks are triggered automatically, and the Drone will Clone the code from the Git repository and pass it through the project directory
.drone.ymlConfigure, execute the corresponding pipeline, and let’s see how the script is written.

  • First, let’s take a look at.drone.ymlIn the configuration of the workflow are what operations, look at the flow chart to know;

  • Let’s do a complete one.drone.yml, with detailed annotations, look at the basic understanding!
Pipeline type: docker # Pipeline type: docker # Pipeline type: kubernetes, exec, SSH and so on Steps: # Define the steps of the pipeline that will be executed in sequence - name: package # Image: "When you mount the directory in the container to the host, the repository needs to turn on the Trusted setting" -name: maven-cache path" -name: maven-build path: /app/build # mount the application with the packed JAR and execution script -cp target/ mall-tiny-drone-1.0-snapshot.jar/mall-tiny-drone-1.0-snapshot.jar/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 mirror Settings/drone - SSH # SSH tools: host: 192.168.5.78 # remote connection address username: root # remote connection account password: Port: 22 # remote connection port: command_timeout: 5m # remote execution command timeout -cd /mydata/maven/build - chmod +x run.sh # Change the script to executable -./run.sh # Run the script pack the image and run the volumes: -name: maven-cache host: path: /mydata/maven/build -name: maven-cache host: path: /mydata/maven/build /mydata/maven/cache
  • run.shExecute scripts to package applications and run container mirroring, which I won’t repeat here, but refer to for more details”I often use automated deployment skills, thief easy to use, recommended to everyone!”, the successful running effect is as follows.

conclusion

Compared to Jenkins’ complex graphical interface, the Drone uses scripts to define pipelined tasks in a much simpler and more intuitive way. Drone is more lightweight, with less memory footprint and fast response time! What does automated deployment need Jenkins? Wouldn’t it be nice to give Git the entire CI/DI function directly?

The resources

  • Official documentation: https://docs.drone.io/
  • Combined with Maven using: https://docs.drone.io/pipelin…
  • Combined with SSH using: http://plugins.drone.io/apple…
  • The container directory mounted to the host machine: https://docs.drone.io/pipelin…

Project source address

https://github.com/macrozheng…

In this paper, making
https://github.com/macrozheng/mall-learningHas been included, welcome everyone STAR!