在 Debian 上搭建 Jenkins 并执行自动化测试
一 环境准备与安装
sudo apt update && sudo apt install -y openjdk-11-jdkjava -versionwget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.listsudo apt update && sudo apt install -y jenkinssudo systemctl enable --now jenkinssudo ufw allow 8080/tcp二 插件与全局工具配置
三 创建任务与流水线示例
pip install -r requirements.txt && python -m pytest tests/ --junitxml=reports/results.xmlmvn clean test**/target/surefire-reports/*.xml** 或 **/reports/results.xml**allure-resultspipeline {
agent any
tools { nodejs 'NodeJS' } // 如用到 Node
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/ --junitxml=reports/results.xml' } }
}
post {
always {
junit 'reports/results.xml'
allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
}
}
}
pipeline {
agent any
tools { maven 'Maven' }
stages {
stage('Build & Test') { steps { sh 'mvn clean test' } }
}
post {
always { junit 'target/surefire-reports/*.xml' }
}
}
四 触发策略与质量门禁
H/5 * * * * 每 5 分钟检查一次变更)五 常见问题与排障要点
jenkins 用户运行:sudo chown -R jenkins:jenkins $WORKSPACE)--junitxml=...),并在 Post-build Actions 正确设置报告路径