CI流程图.png

命令行模式

环境准备

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

dockers/maven/Dockerfile

1
2
3
4
FROM 
## 安装openjdk
## 安装Maven
CMD ["mvn"]

集成、测试、验证
4. 进行自动化测试

1
docker-compose run --rm test

docker-compose.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
    1
    docker-compose up -d server
  2. 人员确认启动状况
  3. 关闭alpha server
    1
    docker-compose stop server
  4. 产出报表与封存
    1
    docker-compose run --rm package

    部署

  5. 构建生产镜像
    不需要maven,把额外的都去掉,保留最干净的环境,Dockerfile一般放在根目录下
    1
    make build-docker-prod-image
    1
    2
    3
    4
    5
    6
    # 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
  6. 发布生产镜像
    1
    docker push localhost:5000/java_simple_prod
  7. 启动生产镜像
    1
    make deploy-production-local
    Makefile
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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所有应用在指定镜像中执行
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    pipeline {
    agent {
    docker {
    image 'maven:3-alpine'
    args '-v /root/.m2:/root/.m2'
    }
    }
    stages {
    stage('Build') {
    steps {
    checkout scm
    }
    }
    }
  2. post当执行完有错误或者成功时运行
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    pipeline {
    agent any
    stages {
    stage('Test') {
    steps {
    sh 'mvn test'
    }
    post {
    always {
    junit 'target/surefire-reports/*.xml'
    }
    }
    }
    }
    }
  3. input parameter用户输入
    1
    2
    3
    4
    5
    stage('wait for input'){
    input {
    ......
    }
    }
  4. when condition
    1
    2
    3
    4
    5
    6
    7
    8
    stage('deploy project'){
    when{
    branch 'master'
    }
    steps{
    ......
    }
    }