Docker+Jenkins(blueocean)+Gitee构建CICD流水线

需求: 使用jenkins搭建流水线实现持续集成持续部署


一、编写docker-compose.yml安装jenkins, jenkins的版本是带blueocean插件的版本

version: '3'
services:
  mysql:
    image: mysql:8.0.29
    container_name: mysql
    restart: always
    ports:
      - 3306:3306
    privileged: true
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: 123456
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
    volumes:
      - /mydata/mysql/data/db:/var/lib/mysql #数据文件挂载
      - /mydata/mysql/data/conf:/etc/mysql/conf.d #配置文件挂载
      - /mydata/mysql/log:/var/log/mysql #日志文件挂载
  redis:
    image: redis:7
    container_name: redis
    restart: always
    command: redis-server --requirepass 1234566 --appendonly yes
    volumes:
      - /mydata/redis/data:/data
      - /etc/localtime:/etc/localtime:ro
    ports:
      - 6379:6379
  nginx:
    image: nginx:1.22
    container_name: nginx
    restart: always
    volumes:
      - /mydata/nginx/nginx.conf:/etc/nginx/nginx.conf #配置文件挂载
      - /mydata/nginx/html:/usr/share/nginx/html #静态资源根目录挂载
      - /mydata/nginx/log:/var/log/nginx #日志文件挂载
      - /etc/localtime:/etc/localtime:ro
    ports:
      - 80:80
  rabbitmq:
    image: rabbitmq:3.9-management
    container_name: rabbitmq
    restart: always
    ports:
      - 5672:5672
      - 15672:15672
    environment:
      TZ: Asia/Shanghai
    volumes:
      - /mydata/rabbitmq/data:/var/lib/rabbitmq #数据文件挂载
      - /etc/localtime:/etc/localtime:ro
  mongo:
    image: mongo:4
    container_name: mongo
    restart: always
    volumes:
      - /mydata/mongo/db:/data/db
    ports:
      - 27017:27017
    environment:
      TZ: Asia/Shanghai
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: 123456
    command: --auth
  jenkins:
    image: jenkinsci/blueocean
    container_name: jenkins
    restart: always
    user: root
    ports:
    - 8080:8080
    - 50000:50000
    volumes:
    - /mydata/jenkins_home:/var/jenkins_home
    - /etc/localtime:/etc/localtime:ro
    - /var/run/docker.sock:/var/run/docker.sock

   在/mydata/jenkins_home目录配置maven的阿里源
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第1张图片



  
  
  
  
  
  
  
    
        nexus-aliyun
        central
        Nexus aliyun
        http://maven.aliyun.com/nexus/content/groups/public
    
  
  /root/.m2
  
        
                jdk-1.8
                
                        1.8
                
                
                        1.8
                        1.8
                        1.8
                
        
  

二、登录xxx:8080配置jenkins
  1、初始密码使用 docker logs jenkins 查看获取
  2、安装插件 Docker Pipeline, 后面构建流水线需要
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第2张图片
  3、准备一个项目, 提交到Gitee
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第3张图片
  创建两个文件 Jenkinsfile和Dockerfile

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

    //定义流水线的加工流程
    stages {
        //流水线的所有阶段
        stage('1.环境检查'){
            steps {
               sh 'pwd && ls -alh'
               sh 'printenv'
               sh 'docker version'
               sh 'java -version'
               sh 'git --version'
            }
        }

        stage('2.编译'){
            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('3.打包'){
            steps {
               sh 'pwd && ls -alh'
               sh 'docker build -t ${IMAGE_NAME} .'
            }
        }

        stage('4.部署'){
            // 删除容器和虚悬镜像
            steps {
               sh 'pwd && ls -alh'
               sh 'docker rm -f ${IMAGE_NAME} && docker rmi $(docker images -q -f dangling=true)'
               sh 'docker run -d -p 8888:8080 --name ${IMAGE_NAME} -v /mydata/logs/${IMAGE_NAME}:/logs/${IMAGE_NAME} ${IMAGE_NAME}'
            }
        }
    }
}
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" ]

  4、jenkins首页首页左侧的新建任务, 输入任务名称, 选择流水线
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第4张图片
  4、配置流水线
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第5张图片

   5、返回首页点击'打开 Blue Ocean'选择运行
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第6张图片

 到此整体功能已经完成

三、测试
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第7张图片
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第8张图片

四、触发远程构建
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第9张图片

Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第10张图片   创建一个jenkins账号用来触发远程构建
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第11张图片


构建maven项目
Docker+Jenkins(blueocean)+Gitee构建CICD流水线_第12张图片

 

你可能感兴趣的:(编程,jenkins,docker,运维,java)