Jenkinsfile
4.34 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
pipeline {
agent any
parameters {
choice(
name: 'DEPLOY_ENV',
choices: ['test', 'uat', 'prod'],
description: '选择部署环境'
)
}
triggers {
cron('0 10 * * *')
}
environment {
PYTHON_VERSION = '3.8'
PROJECT_NAME = 'api_test'
DEPLOY_ENV = "${params.DEPLOY_ENV ?: 'test'}"
IP_HOST_TEST = 'ai-test.hikoon.com'
IP_HOST_UAT = 'ai-uat.hikoon.com'
IP_HOST_PROD = 'api.hikoon.com'
}
stages {
stage('检出代码') {
steps {
echo "开始检出代码..."
checkout scm
}
}
stage('环境准备') {
steps {
echo "准备Python环境..."
sh '''
python3 --version
if [ ! -d "venv" ]; then
python3 -m venv venv
fi
. venv/bin/activate
pip install --upgrade pip -q
pip install -r requirements.txt -q
echo "依赖安装完成"
'''
}
}
stage('执行测试') {
steps {
echo "开始执行API自动化测试 (环境: ${DEPLOY_ENV})..."
script {
def ipHost = 'ai-test.hikoon.com'
if (env.DEPLOY_ENV == 'uat') {
ipHost = env.IP_HOST_UAT
} else if (env.DEPLOY_ENV == 'prod') {
ipHost = env.IP_HOST_PROD
} else {
ipHost = env.IP_HOST_TEST
}
env.IP_HOST = ipHost
}
sh '''
. venv/bin/activate
export DEPLOY_ENV="${DEPLOY_ENV}"
export IP_HOST="${IP_HOST}"
echo "当前执行环境: ${DEPLOY_ENV}"
echo "IP_HOST: ${IP_HOST}"
python3 api_test.py
'''
}
}
stage('生成报告') {
steps {
echo "正在生成测试报告..."
sh '''
echo "查找报告文件..."
ls -lh reports/${DEPLOY_ENV}/ 2>/dev/null || echo "未找到报告文件"
'''
}
}
}
post {
always {
echo "正在收集测试结果..."
// 列出所有报告文件(调试用)
sh '''
echo "=== 报告文件清单 ==="
if [ -d "reports" ]; then
find reports -type f -name "*.html" -o -name "*.xlsx" | sort
else
echo "reports 目录不存在"
fi
echo "===================="
'''
// 归档报告文件
archiveArtifacts artifacts: 'reports/**/*',
allowEmptyArchive: true,
fingerprint: true
// 发布 HTML 报告到 Jenkins
script {
def reportDir = "reports/${env.DEPLOY_ENV}"
sh "ls -la ${reportDir} 2>/dev/null || echo 'Directory not found'"
def htmlFile = sh(
script: "find ${reportDir} -maxdepth 1 -name '*.html' -type f 2>/dev/null | sort -r | head -1 | xargs basename",
returnStdout: true
).trim()
if (htmlFile) {
echo "找到HTML报告: ${htmlFile}"
publishHTML([
reportDir: reportDir,
reportFiles: htmlFile,
reportName: "Test Report [${env.DEPLOY_ENV}]",
keepAll: true,
alwaysLinkToLastBuild: true,
allowMissing: false
])
echo "HTML报告已发布到Jenkins"
} else {
echo "警告: 未找到HTML报告文件 (路径: ${reportDir})"
}
}
}
success {
echo "[SUCCESS] [${DEPLOY_ENV}环境] 所有测试用例运行成功!"
}
failure {
echo "[FAILURE] [${DEPLOY_ENV}环境] 有测试用例执行失败!"
}
}
}