Bye, Jenkins! A few lines of script to complete the automatic deployment. This artifact is a little powerful

Posted by progman on Wed, 09 Feb 2022 01:12:42 +0100

In the development or production environment, we often make a set of automatic deployment scheme (commonly known as one click deployment). Jenkins + 8G is a popular solution, but it doesn't take up much memory, and it's not easy to implement it. Recently, I found an artifact Drone, a lightweight CI/DI tool. Combined with Gogs, it takes up less than 1G of memory, and a few lines of script can realize automatic deployment. I recommend it to you!

Introduction to Drone

Drone is a continuous integration tool based on container technology. It can complete complex automatic construction, testing and deployment tasks with a simple YAML configuration file. There are already 22K+Star on Github.

 

Gogs installation

We will use lightweight Gogs to build Git warehouse. Here is just the installation steps. For specific use, please refer to Github standard star 34K+Star, this open source project helps you build Git service in seconds.

  • First, you need to download the Docker image of Gogs;
docker pull gogs/gogs
  • After downloading, run Gogs in 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 the account: http://192.168.5.78:10080

 

  • Then upload the source code of our SpringBoot project mall tiny drone. Project address: https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-drone

 

Drone installation

Next, we install Drone, which is worthy of being a container based CI/DI tool. It is very convenient to install with Docker!

  • First, download the images of Drone's Server and Runner;
#Drone's Server
docker pull drone/drone:1
#Drone's Runner
docker pull drone-runner-docker:1
  • Here are the concepts of Server and Runner. Let's understand them 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 that polls the Server to obtain the pipeline tasks to be executed, and then executes them.
  • Next, let's install the drone server and 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 are explained in a unified way below;
    • DRONE_GOGS_SERVER: used to configure the Gogs service address.
    • DRONE_RPC_SECRET: Drone's shared secret key is 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. It must be http or https.
    • DRONE_USER_CREATE: create an administrator account, which needs to be registered in Gogs.
  • Next, install the drone runner docker. When there is a task to be executed, the temporary container will be started to execute 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 many configuration parameters here, which are explained in a unified way below.
    • DRONE_RPC_PROTO: used to configure the protocol to connect to the Drone server. It 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 and execute pipeline tasks.
    • DRONE_RPC_SECRET: used to configure the shared secret key connected to the Drone server.
    • DRONE_ RUNNER_ Capability: limit the number of pipeline tasks that the runner can execute concurrently.
    • DRONE_RUNNER_NAME: the name of the custom runner.

Drone use

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

 

  • At this time, our items in Gogs will now be in the list. If not, click SYNC;

 

  • Next, we need to set the warehouse to Trusted (otherwise the container created by Drone cannot be attached to the host computer), and finally click SAVE to SAVE;

 

  • After saving successfully, a Web hook will be automatically configured in Gogs. When we push code into 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, and a green √ will be displayed if the push is successful;

 

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

 

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

 

  • Using RESTART in the ACTIVITY FEED can re execute the pipeline and find that it has been successfully executed.

 

Script

When we Push the code to Git warehouse, the Web hook will be triggered automatically, and then Drone will Clone the code from Git warehouse, and then pass the code in the project directory Drone. Configure YML and execute the corresponding pipeline. Next, let's see how this script is written.

  • First of all, let's learn about drone. See the flow chart for the operations of the workflow configured in YML;

 

  • Another complete one drone.yml, with detailed notes, you can basically understand it!
kind: pipeline #Define object types, as well as secret and signature
type: docker #Define pipeline types, as well as kubernetes, exec, ssh and other types
name: mall-tiny-drone #Define pipeline name

steps: #Define pipeline execution steps, which will be executed in sequence
  - name: package #Pipeline name
    image: maven:3-jdk-8 #Defines the Docker image for creating containers
    volumes: #To mount the directory in the container to the host computer, the warehouse needs to enable the Trusted setting
      - name: maven-cache
        path: /root/.m2 #Mount the directory that maven download depends on to prevent repeated downloads
      - name: maven-build
        path: /app/build #Mount the packaged Jar and execution script of the application
    commands: #Defines the shell commands executed in the Docker container
      - mvn clean package #Apply package command
      - 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 tool image
    settings:
      host: 192.168.5.78 #Remote connection address
      username: root #Remote connection account
      password:
        from_secret: ssh_password #Read SSH password from Secret
      port: 22 #Remote connection port
      command_timeout: 5m #Remote command execution timeout
      script:
        - cd /mydata/maven/build #Enter the host build directory
        - chmod +x run.sh #Change to executable script
        - ./run.sh #Run the script to package the application image and run it

volumes: #Define the pipeline mount directory for sharing data
  - name: maven-build
    host:
      path: /mydata/maven/build #Directory mounted from host
  - name: maven-cache
    host:
      path: /mydata/maven/cache
  • run.sh execution script can realize packaging application and running container image, which will not be repeated here. For details, please refer to my common automatic deployment skills, which are easy for thieves to use, and I recommend them to you, The operation is successful and the effect is as follows.

 

summary

Compared with Jenkins' complex graphical interface operation, drone's use of scripts to define pipeline tasks is undoubtedly simpler and more intuitive. Drone is more lightweight, less memory and fast response! What do you need for automated deployment, Jenkins? Isn't it nice to give Git the whole CI/DI function directly?

 

Most complete learning notes: real questions from big factories + micro services + MySQL + distributed + SSM framework + Java+Redis + data structure and algorithm + Network + Linux+Spring bucket + JVM + high concurrency + brain map of major learning + interview collection

Topics: Java