Bye Jenkins! A few lines of scripts for automated deployment, this artifact is a bit awesome!

In a development or production environment, we often develop a set of Automated Deployment schemes (commonly referred to 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 a 8G memory, can't run smoothly, and can't be deployed quickly. Recently discovered a magic Drone, lightweight CI/DI tool, combined with Gogs using less than 1G of memory, several lines of scripts can achieve automatic deployment, recommended to you!

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

Introduction to Drone

Drone is a container-based, continuous integration tool that uses simple YAML configuration files to accomplish complex automated build, test, and deployment tasks. It already has 22K+Star on Github.

Gogs Installation

We'll use lightweight Gogs to build the Git warehouse. Here's just a brief description of the installation steps for reference "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
  • Run Gogs in the Docker container when the download is complete;
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 your account: http://192.168.5.78:10080

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

Drone Installation

Next we'll install Drone, which is a container-based CI/DI tool, and it's easy to install with Docker!

  • First download Drone's Server and Runner images;
# Drone's Server
docker pull drone/drone:1
# Drone's Runner
docker pull drone-runner-docker:1
  • Here is a concept of Server and Runner, let's first understand it;

    • Server: Provides a Web page for Drone management to manage pipelining tasks in warehouses fetched from Git.
    • Runner: A separate daemon that polls the Server for pipeline tasks to perform and then executes them.
  • Next, let's install drone-server using the following commands;

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, which are explained uniformly below.

    • DRONE_GOGS_SERVER: Used to configure the Gogs service address.
    • DRONE_RPC_SECRET:Drone's shared secret key, used to validate rpc connections to servers, which both servers and runner s need to provide.
    • DRONE_SERVER_HOST: Used to configure an externally accessible address for Drone server.
    • DRONE_SERVER_PROTO: The protocol used to configure external access to Drone server must be http or https.
    • DRONE_USER_CREATE: Create an administrator account that needs to be registered with Gogs.
  • Next, install drone-runner-docker, which starts temporary containers to perform pipelining tasks when there are tasks to perform.

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: The protocol used to configure the connection to Drone server must be http or https.
    • DRONE_RPC_HOST: Used to configure Drone server's access address, runner connects to the server to get pipelining tasks and execute them.
    • DRONE_RPC_SECRET: Used to configure the shared key to connect to Drone server.
    • DRONE_RUNNER_CAPACITY: Limits the number of pipeline tasks that runner can perform concurrently.
    • DRONE_RUNNER_NAME: The name of the custom runner.

Drone uses

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

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

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

  • Once saved successfully, a Web hook is automatically configured in Gogs, which is triggered when we push code into Gogs, then pipelining in Drone is performed.

  • Pull to the bottom, we can send a test push that will show green_if it succeeds.

  • At this point we found in Drone that its pipelining failed because we referenced ssh_in Secret in our script Password;

  • Add a Secret to the warehouse settings. Secret is designed to store passwords that can only be used or deleted and cannot be viewed.

  • Using RESTART in ACTIVITY FEED, the pipeline can be reexecuted and found to have been successfully executed.

Scripting

When we go to the Git repository Push code, the Web hook is automatically triggered, and Drone will go from the Git repository Clone code to the project directory. drone.yml configuration, pipeline execution, let's see how this script is written.

  • First let's get to know in. Drone. What are the operations of the workflow configured in yml? Just look at the flowchart.

  • Another complete one. drone.yml, with detailed annotations, you will 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 so on
name: mall-tiny-drone # Define Pipeline Name

steps: # Define pipeline execution steps that will be executed sequentially
  - name: package # Pipeline name
    image: maven:3-jdk-8 # Define the Docker image for creating containers
    volumes: # To mount the container directory to the host machine, the warehouse needs to open the Trusted setting
      - name: maven-cache
        path: /root/.m2 # Mount maven download dependent directories to prevent duplicate Downloads
      - name: maven-build
        path: /app/build # Mount the packaged Jar and execution script for the application
    commands: # Define shell commands executed in the Docker container
      - mvn clean package # Apply Packaging 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 Mirror
    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 organization 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 a 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

summary

Compared to Jenkins'sophisticated graphical interface operations, Drone's use of scripts to define pipelining tasks is undoubtedly simpler and more intuitive. Drone is lighter, uses less memory and responds faster! What Jenkins do you want for automated deployment? Isn't it bad to give Git the whole CI/DI function directly?

Reference material

  • Official documents: https://docs.drone.io/
  • In conjunction with Maven: https://docs.drone.io/pipeline/kubernetes/examples/language/maven/
  • Used in conjunction with SSH: http://plugins.drone.io/appleboy/drone-ssh/
  • Mount the container directory to the host machine: https://docs.drone.io/pipeline/docker/syntax/volumes/host/

Project Source Address

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

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

Keywords: Java Docker github Spring Boot

Added by robert_gsfame on Tue, 01 Feb 2022 07:48:42 +0200