CI/CD流程 命令列方式與Jenkins Blue Ocean方式

weixin_34249678發表於2018-07-03
5793257-f6585f98fff46dee.png
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{
    ......
  }
}

相關文章