Linux 上用 Jenkins 落地自动化测试的可执行方案
一 架构与准备
sudo apt update && sudo apt install openjdk-11-jdkwget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update && sudo apt install jenkins
sudo systemctl start jenkins && sudo systemctl enable jenkins
docker volume create jenkins-data
docker run -d --name jenkins \
-p 8080:8080 -p 50000:50000 \
-v jenkins-data:/var/jenkins_home \
jenkins/jenkins:lts-jdk11
二 插件与全局工具配置
三 流水线示例与报告通知
示例一 Python + Pytest + Allure(推荐)
pipeline {
agent any
tools { nodejs 'node' python 'python3' } // 如用到 Node/Python 可在此指定
stages {
stage('Checkout') {
steps { git 'https://github.com/your/repo.git' }
}
stage('Install') {
steps { sh 'pip install -r requirements.txt' }
}
stage('Test') {
steps { sh 'pytest tests/ --alluredir=allure-results' }
}
}
post {
always {
allure([
includeProperties: false,
jdk: '',
properties: [],
reportBuildPolicy: 'ALWAYS',
results: [[path: 'allure-results']]
])
emailext(
subject: '构建结果: ${BUILD_STATUS}',
body: '详情: ${BUILD_URL}',
to: 'team@example.com'
)
}
}
}
示例二 Java + Maven + JUnit
pipeline {
agent any
tools { maven 'Maven-3' jdk 'JDK-11' }
stages {
stage('Checkout') { steps { git 'https://github.com/your/java-repo.git' } }
stage('Build & Test') { steps { sh 'mvn clean test' } }
}
post {
always {
junit '**/target/surefire-reports/*.xml'
emailext(
subject: '构建结果: ${BUILD_STATUS}',
body: '详情: ${BUILD_URL}',
to: 'team@example.com'
)
}
}
}
四 触发策略与质量门禁
triggers { cron('H 2 * * *') } // 每天 02:00 左右执行
五 常见问题与最佳实践
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
并重启浏览器或清理缓存后重试(仅在受信网络中使用)。