Using docker to achieve continuous delivery based on cloud effect (Part 2)

Time:2021-1-16
  • catalog
  • preface
  • 1、 Configuration warehouse

    • (1) Configure private service warehouse
  • 2、 Create project

    • (1) New code group
    • (2) New code base
    • (3) Pull code
    • (4) Create Maven project
  • 3、 Image file

    • (1) Basic image
    • (2) Write dockerfile
  • 4、 Build deployment

    • (1) Host management
    • (2) Create pipeline
  • 5、 Verification test

    • (1) Publish demo domain
    • (2) Deploy demo service
    • (3) Visit demo service
  • 6、 Finally

Due to space limitation, this article contains the last three sections. For the first three sections, please see “using docker to achieve continuous delivery based on cloud effect (Part 1)”

preface

Alibaba cloud has released its latest [cloud effect 2020] product. Cloud efficiency provides end-to-end collaborative services and R & D tools from “requirements → development → testing → release → operation and maintenance → operation”, supports multiple deployment forms of public cloud, proprietary cloud and hybrid cloud, and helps developers improve R & D efficiency through the application of artificial intelligence and automation technology, so as to continuously and rapidly deliver effective value.

Cloud effect 2020 products include multiple module functions, such as project collaboration, knowledge base, assembly line, code management, test environment, warehouse management and so on. This paper mainly introduces the use of docker for Java project construction and deployment, involving warehouse management, code management, pipeline and other functions.

Before reading the text, you need to be proficient in using Maven to build Java projects, and understand some basic concepts and commands related to docker.

Tips: there are many pictures in this article. It is recommended to read them in WiFi environment. Local tyrants are free to read them~

4、 Build deployment

(1) Host management

1. Click the menu button in the upper left corner to find and click to enter the pipeline

Using docker to achieve continuous delivery based on cloud effect (Part 2)

2. Click host group management, create a host, and select alicloud ECs

Using docker to achieve continuous delivery based on cloud effect (Part 2)

3. Click new service authorization to grant access to the server

Using docker to achieve continuous delivery based on cloud effect (Part 2)

4. Authorization

Using docker to achieve continuous delivery based on cloud effect (Part 2)

5. Click agree to authorize

Using docker to achieve continuous delivery based on cloud effect (Part 2)

6. After authorization, select the authorized server in the pop-up box and click next

Using docker to achieve continuous delivery based on cloud effect (Part 2)

7. Fill in the server name and set the host environment, and click save

Using docker to achieve continuous delivery based on cloud effect (Part 2)

8. Check the host group management, and the server configuration is successful

Using docker to achieve continuous delivery based on cloud effect (Part 2)

(2) Create pipeline

1. Click all pipelines and create a new releasedemo-domainOur assembly line

Using docker to achieve continuous delivery based on cloud effect (Part 2)

2. Select Java, because it is publisheddemo-domainGo to the private service warehouse, select test, build, and click create

Using docker to achieve continuous delivery based on cloud effect (Part 2)

3. Add code source

Using docker to achieve continuous delivery based on cloud effect (Part 2)

4. Edit the pipeline, select the Java build upload link, delete the build upload step, and configure the Java build

Using docker to achieve continuous delivery based on cloud effect (Part 2)

5. Modify the pipeline name and the build command tomvn -X versions:set -DremoveSnapshot=true && mvn -Dmaven.test.skip=true clean deploy -s settings.xml, as shown below, and save

Using docker to achieve continuous delivery based on cloud effect (Part 2)

6. New build and deploymentdemo-serviceBecause ofdemo-serviceYou need to deploy and start. Here, select mirror build

Using docker to achieve continuous delivery based on cloud effect (Part 2)

7. Add code source as well

Using docker to achieve continuous delivery based on cloud effect (Part 2)

8. Edit the pipeline and configure the Java build step, which is to package the Java source code into a jar package

Using commands
mvn -X versions:set -DremoveSnapshot=true && mvn -Dmaven.test.skip=true clean package -s settings.xml

Using docker to achieve continuous delivery based on cloud effect (Part 2)

9. Edit the pipeline, configure the image construction and push it to the alicloud image warehouse. New service authorization is required to ensure normal access to alicloud container image service

Using docker to achieve continuous delivery based on cloud effect (Part 2)

Using docker to achieve continuous delivery based on cloud effect (Part 2)

10. At the same time, you need to create an image warehouse in the container image service

Using docker to achieve continuous delivery based on cloud effect (Part 2)

11. Continue to configure image construction and push to alicloud image warehouse

Using docker to achieve continuous delivery based on cloud effect (Part 2)

12. Configure the docker deployment environment

The deployment script is as follows

export image=$(echo $image | base64 -d);
sh /zccoder/shell/deploy.sh stop demo-service demo-service 8081 $image;
sh /zccoder/shell/deploy.sh start demo-service demo-service 8081 $image;

Using docker to achieve continuous delivery based on cloud effect (Part 2)

13. In/zccoder/shellDirectory to create and write deploy.sh script

#!/bin/bash

OG_NAME=$0
ACTION=$1
SERVICE_NAME=$2
SERVICE_LOG_NAME=$3
SERVICE_PORT=$4
DOCKER_URL=$5

usage() {
    echo "Not Support this operation: "${ACTION}
    echo "Usage: ${OG_NAME} {start|stop|restart} {SERVICE_NAME} {SERVICE_LOG_NAME} {SERVICE_PORT} {ENV_PROFILES}"
}

stop_application() {
    echo 'stopping......'
    CONTAINER=`docker ps -a |grep "${SERVICE_NAME}$" | awk '{print $1}'`
    if [[ -n "${CONTAINER}" ]]
        then
        docker stop ${CONTAINER}
        docker rm ${CONTAINER}
    fi
    #Delete image
    APP_HOME=/www/service/${SERVICE_NAME}
    if [[ -f "${APP_HOME}/docker_url" ]]
        then
        DOCKER_URL=`cat ${APP_HOME}/docker_url`
        if [[ -n "${DOCKER_URL}" ]]
            then
            TAG_NAME=`echo ${DOCKER_URL##*:}`
        fi
    fi
    if [[ -n "${TAG_NAME}" ]]
        then
        IMAGE=`docker images | grep ${SERVICE_NAME} | grep ${TAG_NAME} |awk '{print $3}'`
    else
        IMAGE=`docker images | grep ${SERVICE_NAME}|awk '{print $3}'`
    fi
    if [[ -n "${IMAGE}" ]]
        then
        docker rmi ${IMAGE}
    fi
}

create_log_dir(){
    #Log path should be real
    log_path="/zccoder/logs/"${SERVICE_LOG_NAME}
    mkdir -p ${log_path}
}

start_application() {
    Echo 'image version:' ${docker}_ URL}

    #Alibaba cloud ECS machine
    HOST_IP=$(ifconfig eth0 | grep "inet " | awk -F: '{print $0}' | awk '{print $2}')
    # JAVA_OPTS=`eval cat /zccoder/shell/java-opts-${SERVICE_LOG_NAME}.conf`
    docker run --cap-add=SYS_PTRACE \
               -e HOST_IP=${HOST_IP} \
               -v /zccoder/logs:/zccoder/logs -v ${log_path}:/log \
               -p ${SERVICE_PORT}:${SERVICE_PORT} \
               --name ${SERVICE_NAME} \
               -d ${DOCKER_URL} \
               java -Djava.security.egd=file:/dev/./urandom -jar ${SERVICE_NAME}.jar
     CONTAINER_ID=`docker ps -a | grep -v "grep" | grep ${SERVICE_NAME} | awk -F: '{print $1}' | awk  '{print $1}' | head -n 1`
     if [[ -n "${CONTAINER_ID}" ]]
        then
        RESULT=`docker inspect -f '{{.State.Running}}' ${CONTAINER_ID}`
        if [[ ${RESULT} = 'true' ]]
           then
           Echo "deployment successful"
           exit
        fi
     fi
     Echo "deployment failed"
}

start() {
    create_log_dir
    start_application
}

stop() {
    stop_application
}

case "$ACTION" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    *)
        usage
    ;;
esac

5、 Verification test

(1) Publish demo domain

1. Click demo domain deploy

Using docker to achieve continuous delivery based on cloud effect (Part 2)

2. Click Run to pop up the run configuration, and then click run again

Using docker to achieve continuous delivery based on cloud effect (Part 2)

Using docker to achieve continuous delivery based on cloud effect (Part 2)

3. Click log to view the build log

Using docker to achieve continuous delivery based on cloud effect (Part 2)

4. Check the build log to show that the build is successful

Using docker to achieve continuous delivery based on cloud effect (Part 2)

5. Verify in Maven private service warehouse, because the build command containsversions:set -DremoveSnapshot=trueSo it will be published to the release repository

Using docker to achieve continuous delivery based on cloud effect (Part 2)

(2) Deploy demo service

1. Click demo service deploy

Using docker to achieve continuous delivery based on cloud effect (Part 2)

2. Click Run to check the success of the pipeline

Using docker to achieve continuous delivery based on cloud effect (Part 2)

Using docker to achieve continuous delivery based on cloud effect (Part 2)

3. Check the startup status of docker container on ECs server

Using docker to achieve continuous delivery based on cloud effect (Part 2)

(3) Visit demo service

Using docker to achieve continuous delivery based on cloud effect (Part 2)

6、 Finally

The above is all the content of using docker to achieve continuous delivery based on cloud effect. Thank you for spending your precious time to read it, and hope you have something to gain.

Welcome to my official account.

Using docker to achieve continuous delivery based on cloud effect (Part 2)