目 录CONTENT

文章目录

Jenkins系列08-Pipeline

cplinux98
2022-08-30 / 0 评论 / 0 点赞 / 699 阅读 / 4,767 字 / 正在检测是否收录...

00:文章简介

介绍jenkins的pipeline。

01:pipeline简介

image

image

image

image

02:基本配置

image

image

image

image

image

image

03:创建一个简单的pipeline

image

image

image

04:pipeline语法特点

image

使用声明式改造语法结构

pipeline {
    agent any
    stages{
        stage ('Prepare'){
            steps{
                echo "准备环境"
            }
        }
        stage ('Checkout'){
            steps{
                echo "获取代码"
            }
        }
        stage ('Build'){
            steps{
                echo "构建镜像"
            }
        }
        stage ('Deploy'){
            steps{
                echo "部署项目"
            }
        }
        stage ('Test'){
            steps{
                echo "测试效果"
            }
        }
    }
}

image

05:pipeline命令特点

pipeline的声明式语法有一个非常重要的问题,
    1 steps内部的命令,每一条单独的命令都在【当前任务的工作目录】下执行。
        即使A命令切换到了一个新的目录,接下来的B命令并不会在对应的新目录中执行,
        而是在当前任务的工作目录下执行。如果非要在切换后的目录下执行命令B,
        那么采用shell中的 && 符号将多条命令拼接在一起即可。

    2 在流水线任务中,中文方式编写命令不友好,推荐在外面写好命令,再拷贝到里面
    3 默认情况下,不支持shell里面的表达式,因为groovy有自己的条件表达式
    4 如果jenkins的工作目录下存在同名目录,则获取失败

5.1:测试代码1

pipeline {
    agent any
    stages {
        stage('cmd-test') {
            steps {
                echo "命令测试"
                sh 'pwd'
                sh 'mkdir mihao '
                sh 'cd mihao && pwd && mkdir buhao && cd buhao && pwd'
                sh 'pwd && ls'
            }
        }
    }
}

image

image

5.2:测试代码2

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                sh 'num=$(docker ps | grep tomcat-web | wc -l)'
                echo "$num"
                echo '----------------------------'
            }
        }
    }
}

image

image

5.3:测试代码3

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                echo '----------------------------'
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
            }
        }
    }
}

image

image

06:pipeline结合webhook

6.1:配置webhook

1. 设置webhook模式,设置secret认证密码
    http://10.0.0.130:8080/project/pipeline-test
    50e4ec08e432c9795cef461205a8119f
2. gitlab的webhook关联Jenkins的任务

image

image

image

6.2:配置pipeline代码

pipeline {
    agent any

    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            steps {
                echo "部署项目"
                sh 'docker rm -f tomcat-web'
                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
            }
        }
        stage('Check') {
            steps {
                echo "检查项目"
            }
        }
        stage('Clean') {
            steps {
                echo "清理旧代码"
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
}
提交代码
vim ROOT/index.jsp
    String str = "买 pipeline 送 20000";
git add .
git commit -m 'fix bug 2'
git push origin master 

查看效果

image

07:pipeline进阶

7.1:input

7.1.1:介绍

stage的input指令允许我们中断当前的执行任务,以待进行确认和其它操作,如果我们确认没问题,那么就
继续执行。

image

image

7.1.2:测试示例

pipeline {
    agent any

    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            steps {
                echo "部署项目"
                sh 'docker rm -f tomcat-web'
                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
            }
        }
        stage('Check') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
            }
            steps {
                echo "检查项目"
            }
        }
        stage('Clean') {
            steps {
                echo "清理旧代码"
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
}

image

image

7.1.3:参数化构建示例

示例1

pipeline {
    agent any

    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            steps {
                echo "部署项目"
                sh 'docker rm -f tomcat-web'
                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
            }
        }
        stage('Check') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                parameters {
                    string(name: 'PERSON', defaultValue: '张三', description: '请输入确认者的身份')
                }
            }
            steps {
                echo "经过 ${PERSON} 的检查,该项目没有问题"
            }
        }
        stage('Clean') {
            steps {
                echo "清理旧代码"
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
}

image

示例2

pipeline {
    agent any
    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            steps {
                echo "部署项目"
                sh 'docker rm -f tomcat-web'
                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
            }
        }
        stage('Check') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                parameters {
                    string(name: 'PERSON', defaultValue: '张三', description: '请输入确认者的身份')
                    choice(name: 'CHOICE',choices: ['Dev', 'Test', 'Prod'],description: '选择合适的分支')
                    password(name: 'PASSWORD',defaultValue: 'SECRET',description: '输入密码')
                }
            }
            steps {
                echo "经过 ${PERSON} 的检查,该项目没有问题"
                echo "Hello ${PERSON}"
                echo "分支为:${CHOICE}"
                echo "密码为:${PASSWORD}"
            }
        }
        stage('Clean') {
            steps {
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
}

image

image

7.2:条件语法

7.2.1:介绍

image

image

image

image

7.2.2:实践

pipeline {
    agent any
    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('Ready') {
            steps {
                echo "准备环境"
                script {
                    result = sh(script:"[ -d tomcat-app ]",returnStatus:true)
                    if(result == 0) {
                        sh 'rm -rf tomcat-app'
                    }
                }
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            steps {
                echo "部署项目"
                sh 'docker rm -f tomcat-web'
                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
            }
        }
        stage('Check') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                parameters {
                    string(name: 'PERSON', defaultValue: '张三', description: '请输入确认者的身份')
                    choice(name: 'CHOICE',choices: ['Dev', 'Test', 'Prod'],description: '选择合适的分支')
                    password(name: 'PASSWORD',defaultValue: 'SECRET',description: '输入密码')
                }
            }
            steps {
                echo "经过 ${PERSON} 的检查,该项目没有问题"
                echo "Hello ${PERSON}"
                echo "分支为:${CHOICE}"
                echo "密码为:${PASSWORD}"
            }
        }
        stage('Clean') {
            steps {
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
}


# 手工创建一个目录
cd /var/lib/jenkins/workspace/pipeline-test
mkdir tomcat-app

image

7.3:并行语法

7.3.1:简介

image

# 示例
pipeline {
    agent any
    stages {
        stage('Non-Parallel Stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
        stage('Parallel Stage') {
            when {
                branch 'master'
            }
            failFast true
            parallel {      # 并行
                stage('Branch A') {
                    agent {
                        label "for-branch-a"
                    }
                    steps {
                        echo "On Branch A"
                    }
                }
                stage('Branch B') {
                    agent {
                        label "for-branch-b"
                    }
                    steps {
                        echo "On Branch B"
                    }
                }
                stage('Branch C') {
                    agent {
                        label "for-branch-c"
                    }
                    stages {
                        stage('Nested 1') {
                            steps {
                                echo "In stage Nested 1 within Branch C"
                            }
                        }
                        stage('Nested 2') {
                            steps {
                                echo "In stage Nested 2 within Branch C"
                            }
                        }
                    }
                }
            }
        }
    }
}

7.3.2:实践

pipeline {
    agent any
    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('Ready') {
            steps {
                echo "准备环境"
                script {
                    result = sh(script:"[ -d tomcat-app ]",returnStatus:true)
                    if(result == 0) {
                        sh 'rm -rf tomcat-app'
                    }
                }
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            parallel {
                stage('deploy_haproxy') {
                    steps {
                        echo "部署nginx应用"
                    }
                }

                stage('deploy_tomcat1_app') {
                    steps {
                        echo "部署tomcat1应用"
                    }
                }

                stage('deploy_tomcat2') {
                    stages {
                        stage('delete_container') {
                            steps {
                                echo "删除旧容器"
                                sh 'docker rm -f tomcat-web'
                            }
                        }
                        stage('start_container') {
                            steps {
                                echo "启动新容器"
                                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
                            }
                        }
                    }
                }
            }
        }
        stage('Check') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                parameters {
                    string(name: 'PERSON', defaultValue: '张三', description: '请输入确认者的身份')
                    choice(name: 'CHOICE',choices: ['Dev', 'Test', 'Prod'],description: '选择合适的分支')
                    password(name: 'PASSWORD',defaultValue: 'SECRET',description: '输入密码')
                }
            }
            steps {
                echo "经过 ${PERSON} 的检查,该项目没有问题"
                echo "Hello ${PERSON}"
                echo "分支为:${CHOICE}"
                echo "密码为:${PASSWORD}"
            }
        }
        stage('Clean') {
            steps {
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
}


image

7.4:发送消息

7.4.1:简介

image

image

简单实践

pipeline {
    agent any
    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('Ready') {
            steps {
                echo "准备环境"
                script {
                    result = sh(script:"[ -d tomcat-app ]",returnStatus:true)
                    if(result == 0) {
                        sh 'rm -rf tomcat-app'
                    }
                }
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            parallel {
                stage('deploy_haproxy') {
                    steps {
                        echo "部署nginx应用"
                    }
                }

                stage('deploy_tomcat1_app') {
                    steps {
                        echo "部署tomcat1应用"
                    }
                }

                stage('deploy_tomcat2') {
                    stages {
                        stage('delete_container') {
                            steps {
                                echo "删除旧容器"
                                sh 'docker rm -f tomcat-web'
                            }
                        }
                        stage('start_container') {
                            steps {
                                echo "启动新容器"
                                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
                            }
                        }
                    }
                }
            }
        }
        stage('Check') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                parameters {
                    string(name: 'PERSON', defaultValue: '张三', description: '请输入确认者的身份')
                    choice(name: 'CHOICE',choices: ['Dev', 'Test', 'Prod'],description: '选择合适的分支')
                    password(name: 'PASSWORD',defaultValue: 'SECRET',description: '输入密码')
                }
            }
            steps {
                echo "经过 ${PERSON} 的检查,该项目没有问题"
                echo "Hello ${PERSON}"
                echo "分支为:${CHOICE}"
                echo "密码为:${PASSWORD}"
            }
        }
        stage('Clean') {
            steps {
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
    post {
        always {
            echo "任务结束时发送消息"
        }
    }
}


image

7.4.2:实践前提

image

# jenkins 主机安装pytest
pip3 install pytest

# git-client01提交代码
pip3 install pytest

mkdir test_scripts

# 编写测试脚本
vim test_scripts/test_api.py

import requests, pytest

def test_url():
    assert 200 == requests.get('http://10.0.0.130:666/',timeout=3).status_code

# 执行测试
py.test --verbose --junit-xml test-reports/results.xml test_scripts/test_api.py

# 删除报告文件夹
rm -rf test-reports

# 推送到远程仓库
git add . 
git commit -m 'Add test'
git push origin master

7.4.3:修改项目代码

pipeline {
    agent any
    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('Ready') {
            steps {
                echo "准备环境"
                script {
                    result = sh(script:"[ -d tomcat-app ]",returnStatus:true)
                    if(result == 0) {
                        sh 'rm -rf tomcat-app'
                    }
                }
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            parallel {
                stage('deploy_haproxy') {
                    steps {
                        echo "部署nginx应用"
                    }
                }

                stage('deploy_tomcat1_app') {
                    steps {
                        echo "部署tomcat1应用"
                    }
                }

                stage('deploy_tomcat2') {
                    stages {
                        stage('delete_container') {
                            steps {
                                echo "删除旧容器"
                                sh 'docker rm -f tomcat-web'
                            }
                        }
                        stage('start_container') {
                            steps {
                                echo "启动新容器"
                                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
                            }
                        }
                    }
                }
            }
        }
        stage('Check') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                parameters {
                    string(name: 'PERSON', defaultValue: '张三', description: '请输入确认者的身份')
                    choice(name: 'CHOICE',choices: ['Dev', 'Test', 'Prod'],description: '选择合适的分支')
                    password(name: 'PASSWORD',defaultValue: 'SECRET',description: '输入密码')
                }
            }
            steps {
                echo "经过 ${PERSON} 的检查,该项目没有问题"
                echo "Hello ${PERSON}"
                echo "分支为:${CHOICE}"
                echo "密码为:${PASSWORD}"
            }
        }
        stage('Clean') {
            steps {
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Test') {
            steps {
                echo "检查项目"
                sh 'sleep 3'
                sh '/usr/local/bin/py.test --verbose --junit-xml test-reports/results.xml tomcat-app/test_scripts/test_api.py'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
    post {
        always {
            junit 'test-reports/results.xml'
        }
    }
}


项目执行结束后刷新页面,会出现一个测试的趋势图型

image

7.4.4:增加邮件发送功能

# 安装插件
 Email Extension

# 配置插件(系统设置的Extened E-mail Notification)
SMTP server:
SMTP port:
SMTP Username:
SMTP Password:
Use SSL
Use TLS

增加代码

pipeline {
    agent any
    stages {
        stage('Prepare') {
            steps {
                echo "开始 tomcat_web 任务"
            }
        }
        stage('Ready') {
            steps {
                echo "准备环境"
                script {
                    result = sh(script:"[ -d tomcat-app ]",returnStatus:true)
                    if(result == 0) {
                        sh 'rm -rf tomcat-app'
                    }
                }
            }
        }
        stage('get_code') {
            steps {
                echo "获取代码"
                sh 'git clone ssh://git@10.0.0.120:22/root/tomcat-app.git'
                sh 'cd tomcat-app && pwd && git config --local user.name "jenkins" && git config --local user.email "jenkins@example.com"'
            }
        }
        stage('update_code') {
            steps {
                echo "更新代码"
                sh '[ -d /data/docker/image ] || mkdir -p /data/docker/image'
                sh 'tar -C tomcat-app/tomcat-web -zcf tomcat-app/tomcat-web/ROOT.tar.gz ROOT --remove-files'
                sh 'mv -i tomcat-app/tomcat-web /data/docker/image/'
            }
        }
        stage('build_code') {
            steps {
                echo "构建镜像"
                sh 'docker build -t tomcat-web:v0.1 /data/docker/image/tomcat-web'
            }
        }
        stage('Deploy') {
            parallel {
                stage('deploy_haproxy') {
                    steps {
                        echo "部署nginx应用"
                    }
                }

                stage('deploy_tomcat1_app') {
                    steps {
                        echo "部署tomcat1应用"
                    }
                }

                stage('deploy_tomcat2') {
                    stages {
                        stage('delete_container') {
                            steps {
                                echo "删除旧容器"
                                sh 'docker rm -f tomcat-web'
                            }
                        }
                        stage('start_container') {
                            steps {
                                echo "启动新容器"
                                sh 'docker run -d --name tomcat-web -p 666:8080 tomcat-web:v0.1'
                            }
                        }
                    }
                }
            }
        }
        stage('Check') {
            input {
                message "Should we continue?"
                ok "Yes, we should."
                parameters {
                    string(name: 'PERSON', defaultValue: '张三', description: '请输入确认者的身份')
                    choice(name: 'CHOICE',choices: ['Dev', 'Test', 'Prod'],description: '选择合适的分支')
                    password(name: 'PASSWORD',defaultValue: 'SECRET',description: '输入密码')
                }
            }
            steps {
                echo "经过 ${PERSON} 的检查,该项目没有问题"
                echo "Hello ${PERSON}"
                echo "分支为:${CHOICE}"
                echo "密码为:${PASSWORD}"
            }
        }
        stage('Clean') {
            steps {
                sh 'rm -rf /data/docker/image/tomcat-web'
            }
        }
        stage('Test') {
            steps {
                echo "检查项目"
                sh 'sleep 3'
                sh '/usr/local/bin/py.test --verbose --junit-xml test-reports/results.xml tomcat-app/test_scripts/test_api.py'
            }
        }
        stage('Stop') {
            steps {
                echo "结束 tomcat-web 任务"
                sh 'rm -rf tomcat-app'
            }
        }
    }
    post {
        unstable {
            emailext (
                body: "任务名称:${JOB_NAME}\n构建编号:${BUILD_NUMBER}\n构建日志:${BUILD_URL}console",
                subject: '【Jenkins构建通知】:$JOB_NAME - Build # $BUILD_NUMBER -不稳定!',
                to: 'linux98_mail@yeah.net',
                from: 'linux98_mail@126.com'
            )
        }

        success {
            emailext (
                body: """项目名称:${JOB_NAME}\n构建编号:${BUILD_NUMBER}\n构建日志:${BUILD_URL}console""",
                subject: '【Jenkins构建通知】:$JOB_NAME - Build # $BUILD_NUMBER -成功!',
                to: 'linux98_mail@yeah.net',
                from: 'linux98_mail@126.com'
            )
        }

        failure {
            emailext (
                body: """项目名称:${JOB_NAME}\n构建编号:${BUILD_NUMBER}\n构建日志:${BUILD_URL}console""",
                subject: '【Jenkins构建通知】:$JOB_NAME - Build # $BUILD_NUMBER -失败!',
                to: 'linux98_mail@yeah.net',
                from: 'linux98_mail@126.com'
            )
        }
    }
}


测试效果

image

7.4.5:其他情况测试

# 测试不通过时
# git-client01
cat test_scripts/test_api.py 
import requests, pytest

def test_url():
    assert 200 == requests.get('http://10.0.0.130:6666/',timeout=3).status_code

git add .
git commit -m 'fix bug port'
git push origin master

image

0

评论区