Files
pw/config/jobs/scripts/qa-hw3-api-tests.groovy
T

126 lines
4.6 KiB
Groovy

pipeline {
agent { label 'maven' }
options {
timestamps()
ansiColor('xterm')
}
stages {
stage('Prepare Metadata') {
steps {
script {
wrap([$class: 'BuildUser']) {
env.RUN_TRIGGER_USER = env.BUILD_USER_ID ?: env.BUILD_USER ?: 'system'
env.RUN_TRIGGER_NAME = env.BUILD_USER ?: env.BUILD_USER_ID ?: 'system'
}
currentBuild.displayName = "#${env.BUILD_NUMBER} api-rest"
currentBuild.description = "by=${env.RUN_TRIGGER_NAME}; repo=${params.API_REST_REPO_URL}; ref=${params.API_REST_REPO_REF}"
}
}
}
stage('Run API REST Tests') {
steps {
sh '''
set -eux
git config --global --add safe.directory '*' || true
rm -rf ./sources ./artifacts
mkdir -p ./artifacts
git clone "${API_REST_REPO_URL}" ./sources
TARGET_REF="${API_REST_REPO_REF}"
if git -C ./sources show-ref --verify --quiet "refs/remotes/origin/${TARGET_REF}"; then
git -C ./sources checkout -B "${TARGET_REF}" "origin/${TARGET_REF}"
else
DEFAULT_REF="$(git -C ./sources symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')"
echo "Requested ref '${TARGET_REF}' not found, fallback to default '${DEFAULT_REF}'"
TARGET_REF="${DEFAULT_REF}"
git -C ./sources checkout -B "${TARGET_REF}" "origin/${TARGET_REF}"
fi
GIT_SHA="$(git -C ./sources rev-parse --short HEAD)"
CID="$(docker create localhost:5005/otus/test-api:1.0.0 bash -lc "set -e; cd /workspace; mvn -Dmaven.compiler.release=${API_REST_JAVA_RELEASE} -Dmaven.compiler.source=${API_REST_JAVA_RELEASE} -Dmaven.compiler.target=${API_REST_JAVA_RELEASE} -Dallure.results.directory=target/allure-results ${API_REST_MAVEN_GOAL}")"
cleanup_container() {
docker rm -f "${CID}" >/dev/null 2>&1 || true
}
trap cleanup_container EXIT INT TERM
tar -C ./sources -cf - . | docker cp - "${CID}:/workspace"
set +e
docker start -a "${CID}"
TEST_RC=$?
docker cp "${CID}:/workspace/target" "./artifacts" || true
mkdir -p ./artifacts/target/allure-results
if ! ls ./artifacts/target/allure-results/* >/dev/null 2>&1; then
EXTRA_UUID="$(cat /proc/sys/kernel/random/uuid)"
TS_MS="$(( $(date +%s) * 1000 ))"
STATUS="passed"
if [ "${TEST_RC}" -ne 0 ]; then
STATUS="failed"
fi
cat > "./artifacts/target/allure-results/${EXTRA_UUID}-result.json" <<EOF
{
"uuid": "${EXTRA_UUID}",
"historyId": "api-rest-summary-${BUILD_NUMBER}",
"name": "API REST pipeline summary",
"fullName": "pipeline.ApiRestSummary",
"status": "${STATUS}",
"stage": "finished",
"start": ${TS_MS},
"stop": ${TS_MS},
"labels": [
{"name": "suite", "value": "Pipeline"},
{"name": "package", "value": "pipeline"},
{"name": "testClass", "value": "ApiRestSummary"},
{"name": "testMethod", "value": "run"}
],
"steps": [],
"parameters": []
}
EOF
fi
{
echo "repo=${API_REST_REPO_URL}"
echo "ref=${TARGET_REF}"
echo "sha=${GIT_SHA}"
echo "maven_goal=${API_REST_MAVEN_GOAL}"
echo "java_release=${API_REST_JAVA_RELEASE}"
} > ./artifacts/run-info.txt
{
echo "job=${JOB_NAME}"
echo "build=${BUILD_NUMBER}"
echo "repo.url=${API_REST_REPO_URL}"
echo "repo.ref=${TARGET_REF}"
echo "repo.sha=${GIT_SHA}"
echo "maven.goal=${API_REST_MAVEN_GOAL}"
} > ./artifacts/target/allure-results/environment.properties
cat > ./artifacts/target/allure-results/executor.json <<EOF
{
"name": "Jenkins",
"type": "jenkins",
"url": "${JENKINS_URL}",
"buildName": "${JOB_NAME} #${BUILD_NUMBER}",
"buildUrl": "${BUILD_URL}",
"reportUrl": "${BUILD_URL}allure",
"buildOrder": ${BUILD_NUMBER}
}
EOF
trap - EXIT INT TERM
docker rm -f "${CID}" || true
exit "${TEST_RC}"
'''
}
}
}
post {
always {
script {
try {
allure commandline: 'allure', includeProperties: false, jdk: '', results: [[path: 'artifacts/target/allure-results']]
} catch (Exception ex) {
echo "Allure publisher unavailable: ${ex.message}"
}
}
junit allowEmptyResults: true, testResults: 'artifacts/target/surefire-reports/*.xml'
archiveArtifacts allowEmptyArchive: true, artifacts: 'artifacts/run-info.txt,artifacts/target/**'
}
}
}