—
前言
大家好,我是CI集成专家。做CI/CD实践8年,测试集成是最关键的环节之一。RunnerGo提供了完善的API接口,可以轻松集成到各种CI/CD平台。今天分享RunnerGo集成CI/CD的实战经验。
本文章参考RunnerGo v2.3官方文档
—
一、RunnerGo API介绍
API能力
RunnerGo提供丰富的API接口:
| API | 功能 |
|---|---|
| 执行测试计划 | 触发自动化/性能测试 |
| 查询执行状态 | 获取测试进度 |
| 获取测试报告 | 下载测试报告 |
| 管理场景 | 创建/修改测试场景 |
API认证
RunnerGo使用Token认证:
bash
获取Token
curl -X POST ${RUNNERGO_URL}/api/auth/login
-H “Content-Type: application/json”
-d ‘{“username”:”admin”,”password”:”password”}’
使用Token
curl -X GET ${RUNNERGO_URL}/api/plans
-H “Authorization: Bearer ${TOKEN}”
—
二、Jenkins集成
创建Pipeline
groovy
pipeline {
agent any
environment {
RUNNERGO_URL = ‘http://runnergo.example.com’
RUNNERGO_TOKEN = credentials(‘runnergo-token’)
PLAN_ID = ‘plan-xxx-xxx’
}
stages {
stage(‘RunnerGo自动化测试’) {
steps {
script {
// 触发测试
def response = sh(
script: “””
curl -s -X POST ${RUNNERGO_URL}/api/plan/run
-H “Authorization: Bearer ${RUNNERGO_TOKEN}”
-H “Content-Type: application/json”
-d ‘{“plan_id”:”${PLAN_ID}”}’
“””,
returnStdout: true
).trim()
def json = new groovy.json.JsonSlurper().parseText(response)
def reportId = json.report_id
// 等待测试完成
def status = ‘running’
while (status == ‘running’) {
sleep(time: 10, unit: ‘SECONDS’)
def statusResponse = sh(
script: “””
curl -s ${RUNNERGO_URL}/api/report/${reportId}/status
-H “Authorization: Bearer ${RUNNERGO_TOKEN}”
“””,
returnStdout: true
).trim()
def statusJson = new groovy.json.JsonSlurper().parseText(statusResponse)
status = statusJson.status
}
// 检查结果
if (status != ‘success’) {
error(“测试失败”)
}
}
}
}
}
post {
always {
// 获取报告链接
sh “””
curl -s ${RUNNERGO_URL}/api/report/${reportId}
-H “Authorization: Bearer ${RUNNERGO_TOKEN}”
reports/runnergo-report.json
“””
}
}
}
—
三、GitLab CI集成
yaml
.gitlab-ci.yml
stages:
– build
– test
runnergo_test:
stage: test
image: curlimages/curl:latest
script:
# 触发测试
– |
RESPONSE=$(curl -s -X POST ${RUNNERGO_URL}/api/plan/run
-H “Authorization: Bearer ${RUNNERGO_TOKEN}”
-H “Content-Type: application/json”
-d ‘{“plan_id”:”${PLAN_ID}”}’)
REPORT_ID=$(echo $RESPONSE | jq -r ‘.report_id’)
# 等待测试完成
– |
while true; do
STATUS=$(curl -s ${RUNNERGO_URL}/api/report/${REPORT_ID}/status
-H “Authorization: Bearer ${RUNNERGO_TOKEN}” | jq -r ‘.status’)
if [ “$STATUS” != “running” ]; then
break
fi
sleep 10
done
# 检查结果
– |
if [ “$STATUS” != “success” ]; then
echo “测试失败”
exit 1
fi
variables:
RUNNERGO_URL: “http://runnergo.example.com”
RUNNERGO_TOKEN: ${RUNNERGO_API_TOKEN}
PLAN_ID: “plan-xxx-xxx”
—
四、GitHub Actions集成
yaml
.github/workflows/test.yml
name: RunnerGo Test
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
– name: Trigger RunnerGo Test
run: |
RESPONSE=$(curl -s -X POST ${{ secrets.RUNNERGO_URL }}/api/plan/run
-H “Authorization: Bearer ${{ secrets.RUNNERGO_TOKEN }}”
-H “Content-Type: application/json”
-d ‘{“plan_id”:”${{ secrets.PLAN_ID }}”}’)
REPORT_ID=$(echo $RESPONSE | jq -r ‘.report_id’)
echo “report_id=$REPORT_ID”
> $GITHUB_ENV
– name: Wait for Test Completion
run: |
while true; do
STATUS=$(curl -s ${{ secrets.RUNNERGO_URL }}/api/report/${{ env.report_id }}/status
-H “Authorization: Bearer ${{ secrets.RUNNERGO_TOKEN }}” | jq -r ‘.status’)
if [ “$STATUS” != “running” ]; then
echo “Test completed with status: $STATUS”
if [ “$STATUS” != “success” ]; then
exit 1
fi
break
fi
sleep 10
done
– name: Download Report
if: always()
run: |
curl -s ${{ secrets.RUNNERGO_URL }}/api/report/${{ env.report_id }}
-H “Authorization: Bearer ${{ secrets.RUNNERGO_TOKEN }}”
runnergo-report.json
– name: Upload Report
if: always()
uses: actions/upload-artifact@v3
with:
name: runnergo-report
path: runnergo-report.json
—
五、质量门禁配置
测试失败阻断部署
groovy
stage(‘质量检查’) {
steps {
script {
// 执行RunnerGo测试
def success = triggerRunnerGoTest()
if (!success) {
error(‘质量门禁未通过,阻断部署’)
}
}
}
}
stage(‘部署’) {
// 只有质量检查通过才执行
steps {
sh ‘deploy.sh’
}
}
—
总结
RunnerGo CI/CD集成要点:
| 环节 | 配置 |
|---|---|
| API认证 | Token获取和使用 |
| Jenkins | Pipeline脚本 |
| GitLab CI | YAML配置 |
| GitHub Actions | Workflow配置 |
| 质量门禁 | 测试失败阻断 |
—
本系列文章完结!感谢大家持续关注RunnerGo博客。