美文网首页devops:改变思维,让一切更加高效
CI/CD流程 命令行方式与Jenkins Blue Ocean

CI/CD流程 命令行方式与Jenkins Blue Ocean

作者: 潘晓华Michael | 来源:发表于2018-07-03 00:27 被阅读164次
CI流程图.png

命令行模式

环境准备

  1. 克隆代码
git clone
  1. 启动私有docker hub
 docker run -d -p 5000:5000 -v /data/registry:/var/lib/registry --name registry --r
estart=always registry
  1. 制作建置环境
docker build -t localhost:5000/maven dockers/maven

dockers/maven/Dockerfile

FROM 
## 安装openjdk
## 安装Maven
CMD ["mvn"]

集成、测试、验证

  1. 进行自动化测试
docker-compose run --rm test

docker-compose.yaml

data:
  image: alpine
  volumes:
    - ~/.m2:/root/.m2
  command: "/bin/bash"

install:
  image: localhost:5000/maven
  volumes:
    - ./:/app
  volumes_from:
    - data
  working_dir: /app
  command: 'mvn test'

server:
  image: localhost:5000/maven
  volumes:
    - ./:/app
    - ~/.m2:/root/.m2
  working_dir: /app
  ports:
    - "8000:8000"
  command: "mvn spring-boot:run"
  1. 启动 alpha server
docker-compose up -d server
  1. 人员确认启动状况
  2. 关闭alpha server
docker-compose stop server
  1. 产出报表与封存
docker-compose run --rm package

部署

  1. 构建生产镜像
    不需要maven,把额外的都去掉,保留最干净的环境,Dockerfile一般放在根目录下
make build-docker-prod-image
# Dockerfile
FROM java
COPY target/spring-boot-simple-data-rest-0.1.0.jar /app/
EXPOSE 8000
WORKDIR /app
CMD /bin/bash -c 'java -jar spring-boot-simple-data-rest-0.1.0.jar 
  1. 发布生产镜像
docker push localhost:5000/java_simple_prod
  1. 启动生产镜像
make deploy-production-local

Makefile

start-docker-registry:
  - docker run -d -p 5000:5000 -v /data/registry:/var/lib/registry --name registry --r
estart=always registry

build-docker-env:
  docker build -t localhost:5000/maven dockers/maven

build-docker-prod-image:
  docker build -t localhost:5000/java_simple_prod .

deploy-production-local:
  - docker rm -f java_simple_prod
  - docker run -d --name java_simple_prod -p 8000:8000 localhost:5000/java_simple_prod

Jenkins模式Blue Ocean

# Jenkinsfile https://github.com/agileworks-tw/spring-boot-sample
pipeline {
    agent any
    stages {
        stage('checkout project') {
            steps {
                //git url: 'https://github.com/agileworks-tw/spring-boot-sample.git'
                checkout scm
            }
        }
        stage('check docker install and build env') {
            steps {
                sh "docker -v"
                sh "docker-compose -v"
                sh "docker ps"
                sh "make start-docker-registry"
                sh "make build-docker-env"
            }
        }
        stage('test project and serve') {
            steps {
                sh "docker-compose run --rm test"
                sh "docker-compose up -d server"
            }
            post {
                always {
                    archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
                    junit '**/target/surefire-reports/TEST-*.xml'
                }
            }
        }
        stage('wait for confirm') {
            input {
                message "Does staging at http://localhost:8000 look good?"
                ok "Deploy to production"
                submitter "admin"
                parameters {
                    string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
                }
            }
            steps {
                echo "Hello, ${PERSON}, nice to meet you."

            }
            post { 
                always { 
                    sh "docker-compose stop server"
                }
            }
        }
        stage('deploy project') {
            when {
                branch 'master'
            }
            steps {
                sh "docker-compose run --rm package"
                sh "make build-docker-prod-image"
                sh "docker push localhost:5000/java_sample_prod"
                sh "make deploy-production-local"
            }
            
        }        
    }
}

拓展

  1. agent docker所有应用在指定镜像中执行
pipeline {
    agent {
        docker {
            image 'maven:3-alpine'
            args '-v /root/.m2:/root/.m2'
        }
    }
    stages {
        stage('Build') {
            steps {
                checkout scm
            }
        }
}
  1. post当执行完有错误或者成功时运行
pipeline {
  agent any
  stages {
     stage('Test') {
            steps {
                sh 'mvn test'
            }
            post {
                always {
                    junit 'target/surefire-reports/*.xml'
                }
            }
        }
  }
}
  1. input parameter用户输入
stage('wait for input'){
  input {
    ......
  }
}
  1. when condition
stage('deploy project'){
  when{
    branch 'master'
  }
  steps{
    ......
  }
}

相关文章

网友评论

    本文标题:CI/CD流程 命令行方式与Jenkins Blue Ocean

    本文链接:https://www.haomeiwen.com/subject/hopruftx.html