pipeline { agent { label 'maven' } options { timestamps() ansiColor('xterm') } stages { stage('Prepare Workspace') { steps { sh ''' set -eux rm -rf ./project mkdir -p ./project cp -a /workspace/otus-autotests/hw7/. ./project/ ''' } } stage('Start Mobile Environment') { steps { dir('project') { sh ''' set -eux COMPOSE_FILE="/workspace/otus-autotests/hw8/config/compose/mobile-ci.compose.yml" if docker compose version >/dev/null 2>&1; then compose_cmd() { PROJECT_DIR="$PWD" docker compose -f "${COMPOSE_FILE}" "$@"; } elif docker-compose version >/dev/null 2>&1; then compose_cmd() { PROJECT_DIR="$PWD" docker-compose -f "${COMPOSE_FILE}" "$@"; } else echo "Neither docker compose plugin nor docker-compose binary is available" exit 1 fi export COMPOSE_PROJECT_NAME=mobileci compose_cmd down -v --remove-orphans || true compose_cmd up -d wiremock android-emulator-1 android-emulator-2 EMULATORS="" for service in android-emulator-1 android-emulator-2; do cid="$(compose_cmd ps -q "$service")" if [ -z "${cid}" ]; then echo "Container for ${service} not found, skipping" continue fi healthy="false" for i in $(seq 1 80); do status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}starting{{end}}' "${cid}" || true)" if [ "${status}" = "healthy" ]; then healthy="true" break fi sleep 10 done if [ "${healthy}" = "true" ]; then if [ -n "${EMULATORS}" ]; then EMULATORS="${EMULATORS}," fi EMULATORS="${EMULATORS}${service}|http://${service}:4723|Android Emulator|${APP_URL}" else echo "Service ${service} is not healthy in time, excluding from test run" docker logs "${cid}" || true fi done if [ -z "${EMULATORS}" ]; then echo "No healthy emulators available" exit 1 fi printf '%s' "${EMULATORS}" > .mobile_emulators.txt ''' } } } stage('Run Appium Tests In Docker') { steps { dir('project') { sh ''' set -eux rm -rf ./target MOBILE_EMULATORS_VALUE="$(cat ./.mobile_emulators.txt)" DB_PASSWORD_EFFECTIVE="${DB_PASSWORD:-${MOBILE_DB_PASSWORD:-}}" if [ -z "${DB_PASSWORD_EFFECTIVE}" ]; then echo "DB password is not set. Provide DB_PASSWORD job parameter or MOBILE_DB_PASSWORD env variable." exit 1 fi CID="$(docker create \ --network mobileci_default \ -e DB_URL="${DB_URL:-}" \ -e DB_USER="${DB_USER:-}" \ -e DB_PASSWORD="${DB_PASSWORD_EFFECTIVE}" \ -e APP_URL="${APP_URL:-}" \ -e MOBILE_EMULATORS="${MOBILE_EMULATORS_VALUE}" \ -e WISHLISTS_USERNAME="${WISHLISTS_USERNAME:-}" \ -e WISHLISTS_PASSWORD="${WISHLISTS_PASSWORD:-}" \ -e GIFTS_USERNAME="${GIFTS_USERNAME:-}" \ -e GIFTS_PASSWORD="${GIFTS_PASSWORD:-}" \ -e RESERVATION_USERNAME="${RESERVATION_USERNAME:-}" \ -e RESERVATION_PASSWORD="${RESERVATION_PASSWORD:-}" \ -e RESERVATION_OWNER="${RESERVATION_OWNER:-}" \ localhost:5005/otus/test-mobile:1.0.0 \ bash -lc "set -e; cd /workspace; timeout 1800s mvn -Dallure.results.directory=target/allure-results test")" cleanup_container() { docker rm -f "${CID}" >/dev/null 2>&1 || true } trap cleanup_container EXIT INT TERM tar -C "$PWD" -cf - . | docker cp - "${CID}:/workspace" set +e docker start -a "${CID}" TEST_RC=$? docker cp "${CID}:/workspace/target" "./target" || true trap - EXIT INT TERM docker rm -f "${CID}" || true exit "${TEST_RC}" ''' } } } } post { always { dir('project') { sh ''' set +e COMPOSE_FILE="/workspace/otus-autotests/hw8/config/compose/mobile-ci.compose.yml" if docker compose version >/dev/null 2>&1; then compose_cmd() { PROJECT_DIR="$PWD" docker compose -f "${COMPOSE_FILE}" "$@"; } elif docker-compose version >/dev/null 2>&1; then compose_cmd() { PROJECT_DIR="$PWD" docker-compose -f "${COMPOSE_FILE}" "$@"; } else echo "compose command is not available for cleanup" exit 0 fi export COMPOSE_PROJECT_NAME=mobileci compose_cmd down -v --remove-orphans || true ''' } script { try { allure commandline: 'allure', includeProperties: false, jdk: '', results: [[path: 'project/target/allure-results']] } catch (Exception ex) { echo "Allure publisher unavailable: ${ex.message}" } } junit allowEmptyResults: true, testResults: 'project/target/surefire-reports/*.xml' archiveArtifacts allowEmptyArchive: true, artifacts: 'project/target/**' } } }