Gogs+Drone+Docker Automated Deployment CICD

Environment centos7

1: Install docker

1. Check your current kernel version through the uname -r command

$ uname -r

2. Log in to Centos with root privileges. Ensure that the yum package is up to date.

$ sudo yum update

3. Uninstall the old version (if the old version has been installed)

$ sudo yum remove docker  docker-common docker-selinux docker-engine

4. Install the required packages. Yum util provides the yum config manager function. The other two are dependent on the devicemapper driver

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

5. Set yum source

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

6. You can view all docker versions in all warehouses and select a specific version to install

$ yum list docker-ce --showduplicates | sort -r

7. Install docker

$ sudo yum install docker-ce  #Since only the stable warehouse is enabled by default in repo, the latest stable version 17.12.0 is installed here
$ sudo yum install <FQPN>  # For example: sudo yum install docker-ce-17.12.0.ce

8. Boot and join boot

$ sudo systemctl start docker
$ sudo systemctl enable docker

9. Verify whether the installation is successful (there are two parts: client and service, which means that the installation and startup of docker are successful)

$ docker version

docker basic operation commands

run Run a container
start/stop/restart start-up/stop it/restart
kill Send a message to the container kill command
rm Delete a container. If it is still running, you must first stop
pause/unpause suspend/recovery
create Create a container but do not run it
exec Send command to container

docker run command comments

-a stdin: Specifies the standard input / output content type, optional STDIN/STDOUT/STDERR Three;
-d: Run the container in the background and return to the container ID;
-i: Run the container in interactive mode, usually with -t Simultaneous use;
-P: Random port mapping: the internal port of the container is randomly mapped to the port of the host
-p: Specify the port mapping in the format: host(host)port:Container port
-t: Reassign a pseudo input terminal to the container, usually with -i Simultaneous use;
--name="nginx-lb": Specify a name for the container;
--dns 8.8.8.8: Specifies the container to use DNS Server, which is consistent with the host by default;
--dns-search example.com: Specify container DNS Search the domain name, which is consistent with the host by default;
-h "mars": Specifies the name of the container hostname;
-e username="ritchie": Setting environment variables;
--env-file=[]: Read environment variables from the specified file;
--cpuset="0-2" or --cpuset="0,1,2": Bind container to specified CPU function;
-m :Set the maximum memory used by the container;
--net="bridge": Specify the network connection type of the container, support bridge/host/none/container: Four types;
--link=[]: Add a link to another container;
--expose=[]: Open a port or a group of ports;
--volume , -v: Bind a volume

2: Install Gogs

1: Turn on the server or local firewall in advance. The following commands have to turn on 10022 and 10080
2: Create folder in advance / mydata/gogs

docker run -p 10022:22 -p 10080:3000 --name=gogs \
-e TZ="Asia/Shanghai" \
-v /mydata/gogs:/data  \
-d gogs/gogs

Open the browser. Enter ***********: 10080 for configuration

3: Install Drone

1) Install the Drone server (server, which is used to interact with gogs and some configuration and specified tasks)

1: Turn on the server or local firewall in advance. The following command must be turned on 7777
2: Create folder / var/drone in advance
3: Turn on the debugger switch to debug problems
4: Flag of detach. true indicates background operation, and false indicates log output
5: - p is equivalent to -- publish, - v is equivalent to -- volume,-e is equivalent to -- env
6: DRONE_RPC_SECRET can use a custom key (use your favorite string)
7: DRONE_USER_CREATE is the original user of creation management. If it is not set here, there will be trouble later, and try to set it as the login user of gogs (jjia, replace your gogs user name)

 docker run \
  -v=/var/drone:/data \
  --env=DRONE_DEBUG=true \
  --env=DRONE_LOGS_TRACE=true \
  --env=DRONE_LOGS_DEBUG=true\
  --env=DRONE_LOGS_PRETTY=true \
  --env=DRONE_AGENTS_ENABLED=true \
  --env=DRONE_GIT_ALWAYS_AUTH=true \
  --env=DRONE_RPC_SECRET=buxiangshagnban \
  --env=DRONE_SERVER_HOST=http://*******:7777 \
  --env=DRONE_SERVER_PROTO=http \
  --env=DRONE_GOGS_SERVER=http://*******:10080 \
  -e DRONE_USER_CREATE=username:jjia,admin:true \
  --publish=7777:80 \
  -e TZ="Asia/Shanghai" \
  --restart=always \
  --detach=true \
  --name=drone2 \
  drone/drone:2

Open the browser. Enter ***********: 7777 for configuration
The user name and password are the user name and password of gogs

2) Install the Drone runner (client, used to perform tasks)

1: The port here can not be opened, because its principle is to request the drone server regularly, not pushed by the server, so it's OK not to open the port
2: DRONE_ RPC_ The secret must be connected with the drone of the drone server_ RPC_ Keep the secret consistent, otherwise it cannot be authenticated
3: I installed two runners here, but it is not necessary. You can install only one docker runner or ssh runner, or even install the pen runner. See the details Official website
4: Don't mention the rest. Just watch the navigation of the official website in two directions: server and runner

  docker run --detach \
  --env=DRONE_RPC_PROTO=http \
  --env=DRONE_RPC_HOST=***********:7777 \
  --env=DRONE_RPC_SECRET=buxiangshagnban \
  --publish=9108:3000 \
  --restart always \
  --name ssrn1 \
  drone/drone-runner-ssh
  
docker run --detach \
  --volume=/var/run/docker.sock:/var/run/docker.sock \
  --env=DRONE_RPC_PROTO=http \
  --env=DRONE_RPC_HOST=***********:7777 \
  --env=DRONE_RPC_SECRET=buxiangshagnban \
  --env=DRONE_RUNNER_CAPACITY=2 \
  --env=DRONE_RUNNER_NAME=myfirstrunner \
  --publish=8089:3000 \
  --restart=always \
  --name=dcrn \
  drone/drone-runner-docker:1

3) . drone. YML (configuration file to realize the specific publishing process)

The location of. drone.yml needs to be placed in the root directory of the project you want to implement automatic deployment
See the specific configuration Official website document
Example

---
kind: pipeline
type: docker
name: df_pipeline

#Process trigger method
trigger:
  #Specify branch
  branch:
    - debugger
  #Specify event
  event:
    - push

steps: # Define pipeline execution steps, which will be executed in sequence
  - name: package # Pipeline name
    image: maven:3-jdk-8 # Defines the Docker image that creates the container
    volumes: # To mount the directory in the container to the host computer, the warehouse needs to enable the Trusted setting
      - name: mevan-setting
        path: /usr/share/maven/conf #Synchronous configuration
    commands: # Defines the shell commands executed in the Docker container
      - ls
      - mvn clean # Apply package command
      - mvn install # Apply package command
      - mkdir -p /app/gcs/
      - cp -r ./** /app/gcs/

  - name: start
    image: appleboy/drone-ssh # SSH tool image
    settings:
      host: ***** # Remote connection address
      username: root # Remote connection account
      password:
        from_secret: sever_password # Read SSH password from Secret
      port: 22 # Remote connection port
      command_timeout: 5m # Remote command execution timeout
      script:
        - ls

volumes: # Defines the pipeline mount directory for sharing data
  - name: mevan-setting #For mevan download acceleration
    host:
      path: /mydata/maven/conf/ # Directory mounted from host

Posted by samUK on Sun, 31 Oct 2021 20:19:00 -0700