Files
pw/config/jobs/scripts/qa-hw6-playwright-tests.groovy

162 lines
6.5 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} playwright/${params.PLAYWRIGHT_BROWSER}"
currentBuild.description = "by=${env.RUN_TRIGGER_NAME}; repo=${params.PLAYWRIGHT_REPO_URL}; ref=${params.PLAYWRIGHT_REPO_REF}"
}
}
}
stage('Run Playwright Tests') {
steps {
sh '''
set -eux
git config --global --add safe.directory '*' || true
rm -rf ./sources ./artifacts
mkdir -p ./artifacts
git clone "${PLAYWRIGHT_REPO_URL}" ./sources
TARGET_REF="${PLAYWRIGHT_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)"
TEST_IMAGE="${PLAYWRIGHT_DOCKER_IMAGE}"
docker pull "${TEST_IMAGE}" || true
CID="$(docker create \
--add-host=host.docker.internal:host-gateway \
"${TEST_IMAGE}" \
bash -lc "set -e; cd /tmp; mvn -Dheadless=${PLAYWRIGHT_HEADLESS} -Dbrowser=${PLAYWRIGHT_BROWSER} -DbaseUrl=${PLAYWRIGHT_BASE_URL} test")"
cleanup_container() {
docker rm -f "${CID}" >/dev/null 2>&1 || true
}
trap cleanup_container EXIT INT TERM
tar -C ./sources -cf - . | docker cp - "${CID}:/tmp"
set +e
docker start -a "${CID}"
TEST_RC=$?
docker cp "${CID}:/tmp/target" "./artifacts/target" || true
docker cp "${CID}:/tmp/traces" "./artifacts/traces" || true
mkdir -p ./artifacts/target/allure-results
ATTACHMENT_COUNT=0
ATTACHMENTS_MANIFEST="./artifacts/target/allure-results/.external-attachments.txt"
: > "${ATTACHMENTS_MANIFEST}"
ATTACH_FILES="$(find ./artifacts -type f \\( -iname '*.png' -o -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.mp4' -o -iname '*.webm' -o -iname '*.zip' \\) 2>/dev/null || true)"
if [ -n "${ATTACH_FILES}" ]; then
while IFS= read -r attachment_file; do
if [ -z "${attachment_file}" ] || [ ! -f "${attachment_file}" ]; then
continue
fi
ATTACHMENT_COUNT=$((ATTACHMENT_COUNT + 1))
ext="$(echo "${attachment_file}" | awk -F. '{print tolower($NF)}')"
mime="application/octet-stream"
case "${ext}" in
png) mime="image/png" ;;
jpg|jpeg) mime="image/jpeg" ;;
mp4) mime="video/mp4" ;;
webm) mime="video/webm" ;;
zip) mime="application/zip" ;;
esac
source_name="external-attachment-${ATTACHMENT_COUNT}.${ext}"
cp "${attachment_file}" "./artifacts/target/allure-results/${source_name}" || continue
safe_name="$(basename "${attachment_file}" | sed 's/"/\\"/g')"
printf '%s|%s|%s\n' "${safe_name}" "${mime}" "${source_name}" >> "${ATTACHMENTS_MANIFEST}"
done <<EOF
${ATTACH_FILES}
EOF
fi
if [ "${ATTACHMENT_COUNT}" -gt 0 ]; then
ATTACHMENTS_JSON="$(awk -F'|' 'BEGIN{first=1} {if(!first){printf(",")} first=0; gsub(/"/,"\\\"", $1); printf("{\\\"name\\\":\\\"%s\\\",\\\"type\\\":\\\"%s\\\",\\\"source\\\":\\\"%s\\\"}", $1, $2, $3)}' "${ATTACHMENTS_MANIFEST}")"
EXTRA_UUID="$(cat /proc/sys/kernel/random/uuid)"
TS_MS="$(( $(date +%s) * 1000 ))"
cat > "./artifacts/target/allure-results/${EXTRA_UUID}-result.json" <<EOF
{
"uuid": "${EXTRA_UUID}",
"historyId": "external-artifacts-${BUILD_NUMBER}",
"name": "Collected artifacts",
"fullName": "pipeline.CollectedArtifacts",
"status": "passed",
"stage": "finished",
"start": ${TS_MS},
"stop": ${TS_MS},
"labels": [
{"name": "suite", "value": "Pipeline Artifacts"},
{"name": "package", "value": "pipeline"},
{"name": "testClass", "value": "CollectedArtifacts"},
{"name": "testMethod", "value": "attach"}
],
"attachments": [${ATTACHMENTS_JSON}],
"steps": [],
"parameters": []
}
EOF
fi
{
echo "repo=${PLAYWRIGHT_REPO_URL}"
echo "ref=${TARGET_REF}"
echo "sha=${GIT_SHA}"
echo "browser=${PLAYWRIGHT_BROWSER}"
echo "headless=${PLAYWRIGHT_HEADLESS}"
echo "base_url=${PLAYWRIGHT_BASE_URL}"
echo "docker_image=${TEST_IMAGE}"
} > ./artifacts/run-info.txt
{
echo "job=${JOB_NAME}"
echo "build=${BUILD_NUMBER}"
echo "repo.url=${PLAYWRIGHT_REPO_URL}"
echo "repo.ref=${TARGET_REF}"
echo "repo.sha=${GIT_SHA}"
echo "browser=${PLAYWRIGHT_BROWSER}"
echo "headless=${PLAYWRIGHT_HEADLESS}"
echo "base.url=${PLAYWRIGHT_BASE_URL}"
echo "docker.image=${TEST_IMAGE}"
} > ./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/**,artifacts/traces/**'
}
}
}