涉及到的插件需要自行下载
# 设置保留构建历史
properties([buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '7', numToKeepStr: '10')), [$class: 'RebuildSettings', autoRebuild: false, rebuildDisabled: false]])
pipeline {
agent { label 'master' } // 指定 master 节点
# 参数化
parameters {
gitParameter branch: '', branchFilter: '.*', defaultValue: 'origin/master', description: '分支', name: 'BRANCH', quickFilterEnabled: false, selectedValue: 'DEFAULT', sortMode: 'DESCENDING_SMART', tagFilter: '*', type: 'GitParameterDefinition'
choice choices: ['online', 'dev'], description: '测试环境,与base-url同时使用,则使用base-url的值', name: 'ENV'
string defaultValue: 'https://www.baidu.com', description: '测试网站基础URL', name: 'BASE_URL', trim: true
string description: '''浏览器窗口大小,格式:宽*高,默认为最大化
示例:1440*900''', name: 'BROWSER_SIZE', trim: true
extendedChoice defaultValue: 'all', description: '运行标记的测试用例', multiSelectDelimiter: ',', name: 'MARK', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_CHECKBOX', value: 'all,dev', visibleItemCount: 10
string defaultValue: 'None', description: '''并发量,输入指定work数执行
None:按CPU*0.8取整计算
0:不进行并发执行''', name: 'WORK_NUMBER', trim: true
booleanParam description: '是否发送飞书报告', name: 'SEND_NOTIFICATION'
reactiveChoice choiceType: 'PT_RADIO', description: '默认选择主群发送,第二个作为调试发送', filterLength: 1, filterable: false, name: 'WEBHOOK_URL', randomName: 'choice-parameter-164326137306684', referencedParameters: 'SEND_NOTIFICATION', script: groovyScript(fallbackScript: [classpath: [], oldScript: '', sandbox: false, script: ''], script: [classpath: [], oldScript: '', sandbox: true, script: '''if (SEND_NOTIFICATION) {return["https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx:selected", "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx"]} else{return}'''])
}
stages {
stage('清理工作区') {
steps {
// 删除工作区,保持环境干净
cleanWs()
}
}
stage('切换分支') {
steps {
timestampedEcho("开始拉取代码...")
// Git 源码管理,拉取参数化选择的分支
checkout scmGit(branches: [[name: '${BRANCH}']], extensions: [cloneOption(depth: 1, noTags: false, reference: '', shallow: true)], userRemoteConfigs: [[credentialsId: 'xxxxxxx', url: 'git@github.com:xxxxx.git']])
}
}
stage('构建') {
steps {
timestampedEcho("开始构建...")
// 执行构建 Shell 脚本
sh '''#!/bin/bash +x\n
echo "=================构建分支: ${BRANCH}=================="
echo "=================加载环境变量=================="
echo "=================激活Python环境=================="
echo "=================检查运行环境=================="
python --version
pip --version
allure --version
echo "=================进入项目根目录: ${WORKSPACE}=================="
cd ${WORKSPACE}
#echo "=================更新依赖库=================="
#pip install -r requirements.txt
echo "=================执行脚本=================="
echo "=================结束=================="
'''
}
post {
always {
script {
timestampedEcho("生成allure report...")
allure jdk: '', report: 'report/html', results: [[path: 'report/allure']]
}
}
}
}
}
post {
always {
timestampedEcho("构建结束,是否发送飞书报告: ${SEND_NOTIFICATION}")
sh '''#!/bin/bash +x\n
cd ${WORKSPACE}/data
echo "================= 读取 env.properties 文件并加载环境变量=================="
echo "=================检查是否发送通知=================="
# 检查构建参数是否要求发送通知
if [ "$SEND_NOTIFICATION" = "true" ]; then
echo "=================发送飞书通知=================="
export PYTHONPATH=${WORKSPACE}
export AUTO_BUILD_NAME=${BUILD_USER}
python ${WORKSPACE}/utils/notify/lark.py ${BUILD_URL}allure ${WEBHOOK_URL}
echo "已发送通知"
else
echo "不发送通知"
fi
'''
}
}
}
// 辅助函数:输出带时间戳的消息
def timestampedEcho(String message) {
def timestamp = new Date().format("yyyy-MM-dd HH:mm:ss")
echo "[${timestamp}] ${message}"
}