Actual combat: Docker+Jenkins+Gitee builds CICD pipeline

foreword

Continuous integration and continuous delivery have always been popular development and operation methods. CICD saves a lot of operation and maintenance time and can also improve developers' code integration specifications. After developing the required functions, developers can directly submit them to gitee, and then Jenkins directly compiles the code and deploys the integrated pipeline. The efficiency of devops can be greatly improved through pipeline deployment, which is also a manifestation of enterprise information automation.

Jenkins deployment

Jenkins is an open source software project. It is a continuous integration tool developed based on Java. It is used to monitor continuous and repetitive work. It aims to provide an open and easy-to-use software platform that enables continuous integration of software projects.
Jenkins deployment can be directly deployed physically and in containers. This time we directly use docker containerized deployment.

Create Jenkins docker-compose

Because some plug-ins in the higher version cannot be fully downloaded, today we use a lower version for demonstration, which is usually enough.
[root@localhost app]# pwd
/app
[root@localhost app]# vim docker-compose-jenkins.yaml

version: '3'
services:
  jenkins:
    image: registry.cn-hangzhou.aliyuncs.com/senfel/jenkins:2.346.1
    container_name: jenkins
    restart: unless-stopped  #指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
    volumes:
      - "/usr/bin/docker:/usr/bin/docker"
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "/usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7"
      - "./jenkins/jenkins_home:/var/jenkins_home"
      - "./jenkins/jenkins_config:/var/jenkins_config"
    environment:
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      JAVA_OPTS: '-Xmx2048M -Xms2048M -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:MaxNewSize=128m -Djava.util.logging.config.file=/var/jenkins_home/log.properties -Duser.timezone=Asia/Shanghai'
    user: root
    ports:
      - "10000:8080"

Configure maven source

Create maven configuration file directory
[root@localhost app]# mkdir -p jenkins/jenkins_home/appconfig/maven
add settings.xml
[root@localhost app]# vim jenkins/jenkins_home/appconfig/maven/settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>
  </servers>
  <mirrors>
    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
  </mirrors>
  <localRepository>/root/.m2</localRepository>
  <profiles>
        <profile>
                <id>jdk-1.8</id>
                <activation>
                        <jdk>1.8</jdk>
                </activation>
                <properties>
                        <maven.compiler.source>1.8</maven.compiler.source>
                        <maven.compiler.target>1.8</maven.compiler.target>
                        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
                </properties>
        </profile>
  </profiles>
</settings>

Start the Jenkins container to install the plugin

[root@localhost app]# docker-compose -f docker-compose-jenkins.yaml up -d
insert image description here

Browser http://10.10.22.91:10000
insert image description here

Wait for initialization to complete to unlock Jenkins
insert image description here

Get the initial password
[root@localhost app]# cat jenkins/jenkins_home/secrets/initialAdminPassword
install the recommended plugin
insert image description here

Create user and configure instance
insert image description here

Install the Docker Pipleline plugin
insert image description here

In System Settings -> Configure Global Security", do not verify the availability of ssh
insert image description here

Gitee ssh public key configuration and test project submission

1. Enter the docker container to create rsa
[root@localhost app]# docker exec -it jenkins bash
root@c781568bbe98:/# ssh-keygen -t rsa -C "[email protected]"
enter all subsequent steps
insert image description here

View the public key and configure it in gitee ssh
root@c781568bbe98:/# cat /root/.ssh/id_rsa.pub
insert image description here

View the private key and remember to create the pipeline configuration credentials later in jenkins
root@c781568bbe98:/# cat /root/.ssh/id_rsa

2. Prepare a project and create two files Dockerfile and Jenkinsfile in the root directory
insert image description here

Dockerfile

FROM openjdk:8-jre-alpine
# 将当前目录下的jar包复制到docker容器的/目录下
COPY target/*.jar /app.jar
# 运行过程中创建一个xx.jar文件
RUN touch /app.jar;

ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom"
ENV PARAMS=""

# 声明服务运行在8080端口
EXPOSE 8080
# 指定docker容器启动时运行jar包
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /app.jar $PARAMS" ]

Jenkinsfile

pipeline{
    agent any
    environment {
      WS = "${WORKSPACE}"
      IMAGE_NAME = "demo"
    }

    //定义流水线的加工流程
    stages {
        //流水线的所有阶段
        stage('step1:env check'){
            steps {
               sh 'pwd && ls -alh'
               sh 'printenv'
               sh 'docker version'
               sh 'java -version'
               sh 'git --version'
            }
        }

        stage('step2:compile'){
            agent {
                docker {
                    image 'maven:3-alpine'
                    args '-v maven-repository:/root/.m2'
                 }
            }
            steps {
               sh 'pwd && ls -alh'
               sh 'mvn -v'
               sh 'cd ${WS} && mvn clean package -s "/var/jenkins_home/appconfig/maven/settings.xml" -Dmaven.test.skip=true'
            }
        }

        stage('step3:package'){
            steps {
               sh 'pwd && ls -alh'
               sh 'docker build -t ${IMAGE_NAME} .'
            }
        }

        stage('step4:arrange'){
            // 删除容器和虚悬镜像
            steps {
               sh 'pwd && ls -alh'
               sh 'docker rm -f ${IMAGE_NAME} || true && docker rmi $(docker images -q -f dangling=true) || true'
               sh 'docker run -d -p 8888:8088 --name ${IMAGE_NAME} -v /app/logs/${IMAGE_NAME}:/logs/${IMAGE_NAME} ${IMAGE_NAME}'
            }
        }
    }
}

3. Submit to Gitee
insert image description here

Jenkins creates a pipeline

1. Console - New item
insert image description here

2. Directly enter the pipeline option for git configuration
2.1 Add credentials
insert image description here
insert image description here

2.2 Fill in the gitee code repository and choose your favorite branch
insert image description here

3. Pipeline construction demonstration
3.1 Enter our self-built pipeline demo, click to build now
insert image description here

3.2 View build view
insert image description here

As shown in the figure: the build is successful

3.3 Workbench view pipeline
insert image description here

As shown in the figure: the pipeline demo was built successfully last time

3.4 Verify the demo project deployment function
GET 10.10.22.91:8888/hello
insert image description here

After testing, the function is normal, and the pipeline deployment is successful.

write at the end

Actual combat: Docker+Jenkins+Gitee is relatively simple to build a CICD pipeline. This time we introduced Docker Pipleline to build and deploy our project directly from gitee. Of course, we can also automatically trigger the build according to the Generic Webhook Trigger plug-in, which is also relatively simple.

Guess you like

Origin blog.csdn.net/weixin_39970883/article/details/131932086