手动部署
- 在jenkins服务器上安装nodejs,npm,newman,newman-reporter-htmlextra
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt install npm
npm install -g newman
npm install -g newman-reporter-htmlextra
检查命令
node -v //v14.15.0
npm -v //6.14.8
newman -v //5.2.1
- 新建gitlab项目,将多个项目的postman的collection.json和environment.json上传。项目目录为postman/API_TEST_FILE
- 在jenkins服务器上添加插件: NodeJS Plugin
- 在jenkins添加流水线项目(此处文件路径要写完整)
def projectName = ''
pipeline {
agent any
triggers{
xxx
}
stages {
//获取触发项目信息
stage ('checkBuilder') {
steps {
script {
def log = currentBuild.buildCauses
def out = "${log}".tokenize('''"''')
projectName = "${out[1]}"
echo "{$projectName}"
}
}
}
stage('checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
userRemoteConfigs:[[
url: 'git@github.com:helpapa/postman.git',
credentialsId: 'helpapaSsh'
]]
])
}
}
stage ('at'){
when {
equals(expected: "at_pipeline", actual: "${projectName}")
}
steps {
sleep 60
script{
sh 'newman run /home/ubuntu/.jenkins/workspace/postman/API_TEST_FILE/at_collection.json -e /home/ubuntu/.jenkins/workspace/postman/API_TEST_FILE/at_environment.json -r htmlextra --reporter-html-export /home/ubuntu/.jenkins/workspace/postman'
}
}
}
stage ('demo'){
when {
equals(expected: "demo", actual: "${projectName}")
}
steps {
echo "demo postman run"
}
}
}
}
- 构建成功后,在构建历史的workspace/newman处查看报告,如果报告没有css,则在Manage Jenkins-Script Console处运行代码
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
相关资料资料,资料2
docker部署
- 拉取htmlextra镜像,此镜像使用postman/newman:alpine为基础镜像
docker pull dannydainton/htmlextra
- 运行代码:
docker run --rm -t -v /workDir:/etc/newman dannydainton/htmlextra run at_collection.json -e at_environment.json -r htmlextra
(workDir为存储json文件的路径,测试报告在/workDir/newman处)