Implement fully automated Jenkins HW8 setup with Ansible, JCasC and JJB

This commit is contained in:
2026-04-15 22:56:40 +03:00
parent 9f801e52a4
commit 2aa8a49ee1
46 changed files with 1333 additions and 412 deletions
+6
View File
@@ -0,0 +1,6 @@
---
- defaults:
name: global
project_folder: /workspace/otus-autotests
test_image_tag: "1.0.0"
build_keep: 40
+7
View File
@@ -0,0 +1,7 @@
---
- property:
name: hw8-build-policy
properties:
- build-discarder:
num-to-keep: 40
- disable-concurrent-builds
@@ -0,0 +1,31 @@
pipeline {
agent { label 'jjb' }
options {
timestamps()
ansiColor('xterm')
}
stages {
stage('Check Docker & Registry') {
steps {
sh '''
set -eux
docker version
curl -fsS http://registry:5000/v2/_catalog
docker pull localhost:5005/otus/test-selenium:1.0.0
docker pull localhost:5005/otus/test-api:1.0.0
docker pull localhost:5005/otus/test-mobile:1.0.0
'''
}
}
stage('Check Sources') {
steps {
sh '''
set -eux
test -f /workspace/otus-autotests/homework_4/pom.xml
test -f /workspace/otus-autotests/hw7/pom.xml
test -f /workspace/otus-autotests/hw8/config/jobs/global.yaml
'''
}
}
}
}
+44
View File
@@ -0,0 +1,44 @@
pipeline {
agent { label 'jjb' }
options {
timestamps()
ansiColor('xterm')
}
stages {
stage('Validate Input') {
steps {
sh '''
set -eux
test -d "${JJB_PATH}"
test -f "${JJB_PATH}/global.yaml"
'''
}
}
stage('Deploy Jobs') {
steps {
withCredentials([usernamePassword(
credentialsId: 'jenkins-admin-userpass',
usernameVariable: 'J_USER',
passwordVariable: 'J_PASS'
)]) {
sh '''
set -eux
cat > /tmp/jenkins-job-builder.ini <<EOF
[job_builder]
ignore_cache=True
keep_descriptions=True
recursive=True
[jenkins]
url=${JENKINS_URL_INTERNAL}
user=${J_USER}
password=${J_PASS}
EOF
jenkins-jobs --conf /tmp/jenkins-job-builder.ini --flush-cache update "${JJB_PATH}"
rm -f /tmp/jenkins-job-builder.ini
'''
}
}
}
}
}
@@ -0,0 +1,38 @@
pipeline {
agent { label 'maven' }
options {
timestamps()
ansiColor('xterm')
}
stages {
stage('Run API Tests In Docker') {
steps {
sh '''
set -eux
rm -rf ./artifacts
mkdir -p ./artifacts
CID="$(docker create localhost:5005/otus/test-api:1.0.0 bash -lc "set -e; cd /workspace; mvn -f citrus-tests/pom.xml test")"
cleanup_container() {
docker rm -f "${CID}" >/dev/null 2>&1 || true
}
trap cleanup_container EXIT INT TERM
tar -C /workspace/otus-autotests/homework_4 -cf - . | docker cp - "${CID}:/workspace"
set +e
docker start -a "${CID}"
TEST_RC=$?
docker cp "${CID}:/workspace/citrus-tests/target" "./artifacts/citrus-target" || true
trap - EXIT INT TERM
docker rm -f "${CID}" || true
exit "${TEST_RC}"
'''
}
}
}
post {
always {
junit allowEmptyResults: true, testResults: 'artifacts/citrus-target/surefire-reports/*.xml'
archiveArtifacts allowEmptyArchive: true, artifacts: 'artifacts/citrus-target/**'
}
}
}
@@ -0,0 +1,148 @@
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/**'
}
}
}
+54
View File
@@ -0,0 +1,54 @@
pipeline {
agent { label 'jjb' }
options {
timestamps()
ansiColor('xterm')
}
stages {
stage('Run Jobs In Parallel') {
steps {
script {
def fanout = [:]
fanout['selenium'] = {
build job: 'qa-selenium-tests',
wait: true,
propagate: true,
parameters: [
string(name: 'BROWSER', value: params.BROWSER),
string(name: 'BASE_URL', value: params.BASE_URL),
string(name: 'EXECUTION_MODE', value: params.EXECUTION_MODE),
string(name: 'SELENOID_URL', value: params.SELENOID_URL),
string(name: 'HEADLESS', value: params.HEADLESS)
]
}
fanout['mobile'] = {
build job: 'qa-mobile-appium-tests',
wait: true,
propagate: true,
parameters: [
string(name: 'APP_URL', value: params.APP_URL),
string(name: 'DB_URL', value: params.DB_URL),
string(name: 'DB_USER', value: params.DB_USER),
string(name: 'DB_PASSWORD', value: params.DB_PASSWORD),
string(name: 'WISHLISTS_USERNAME', value: params.WISHLISTS_USERNAME),
string(name: 'WISHLISTS_PASSWORD', value: params.WISHLISTS_PASSWORD),
string(name: 'GIFTS_USERNAME', value: params.GIFTS_USERNAME),
string(name: 'GIFTS_PASSWORD', value: params.GIFTS_PASSWORD),
string(name: 'RESERVATION_USERNAME', value: params.RESERVATION_USERNAME),
string(name: 'RESERVATION_PASSWORD', value: params.RESERVATION_PASSWORD),
string(name: 'RESERVATION_OWNER', value: params.RESERVATION_OWNER)
]
}
fanout['api'] = {
build job: 'qa-api-citrus-tests', wait: true, propagate: true
}
parallel fanout
}
}
}
}
}
@@ -0,0 +1,53 @@
pipeline {
agent { label 'maven' }
options {
timestamps()
ansiColor('xterm')
}
stages {
stage('Run Selenium Tests In Docker') {
steps {
sh '''
set -eux
rm -rf ./artifacts
mkdir -p ./artifacts
EXTRA_ARGS=""
if [ "${BROWSER}" = "chrome" ]; then
EXTRA_ARGS="-Dchrome.binary=/usr/bin/chromium"
fi
CID="$(docker create \
--add-host=host.docker.internal:host-gateway \
localhost:5005/otus/test-selenium:1.0.0 \
bash -lc "set -e; cd /workspace; mvn -Dexecution.mode=${EXECUTION_MODE} -Dbrowser=${BROWSER} -Dbrowser.version= -Dselenoid.url=${SELENOID_URL} -Dbase.url=${BASE_URL} -Dselenide.headless=${HEADLESS} -Dallure.results.directory=target/allure-results ${EXTRA_ARGS} test")"
cleanup_container() {
docker rm -f "${CID}" >/dev/null 2>&1 || true
}
trap cleanup_container EXIT INT TERM
tar -C /workspace/otus-autotests/homework_4 -cf - . | docker cp - "${CID}:/workspace"
set +e
docker start -a "${CID}"
TEST_RC=$?
docker cp "${CID}:/workspace/target" "./artifacts/target" || true
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/target/**'
}
}
}
@@ -0,0 +1,11 @@
---
- job:
name: infra-health-check
description: "Проверка инфраструктуры Jenkins/Registry/Agent образов."
project-type: pipeline
concurrent: false
sandbox: true
properties:
- build-discarder:
num-to-keep: 30
dsl: !include-raw-verbatim: ../scripts/infra-health-check.groovy
+16
View File
@@ -0,0 +1,16 @@
---
- job:
name: jobs-uploader
description: "Обновляет Jenkins jobs из JJB YAML (config/jobs)."
project-type: pipeline
concurrent: false
sandbox: true
properties:
- build-discarder:
num-to-keep: 30
parameters:
- string:
name: JJB_PATH
default: /workspace/otus-autotests/hw8/config/jobs
description: "Путь до JJB-конфигов"
dsl: !include-raw-verbatim: ../scripts/jobs-uploader.groovy
@@ -0,0 +1,11 @@
---
- job:
name: qa-api-citrus-tests
description: "API тесты модуля citrus-tests (homework_4)."
project-type: pipeline
concurrent: true
sandbox: true
properties:
- build-discarder:
num-to-keep: 50
dsl: !include-raw-verbatim: ../scripts/qa-api-citrus-tests.groovy
@@ -0,0 +1,49 @@
---
- job:
name: qa-mobile-appium-tests
description: "Appium mobile тесты (hw7) с автоскачиванием APK через APP_URL."
project-type: pipeline
concurrent: true
sandbox: true
properties:
- build-discarder:
num-to-keep: 30
parameters:
- string:
name: APP_URL
default: http://wiremock:8080/wishlist.apk
description: "URL APK для автоскачивания (capability app)"
- string:
name: DB_URL
default: jdbc:postgresql://sql.otus.kartushin.su:5432/wishlist
description: "JDBC URL"
- string:
name: DB_USER
default: student
description: "DB user"
- string:
name: DB_PASSWORD
default: "student"
description: "DB password"
- string:
name: WISHLISTS_USERNAME
default: user1us
- string:
name: WISHLISTS_PASSWORD
default: user1us
- string:
name: GIFTS_USERNAME
default: user2us
- string:
name: GIFTS_PASSWORD
default: user2us
- string:
name: RESERVATION_USERNAME
default: user3us
- string:
name: RESERVATION_PASSWORD
default: user3us
- string:
name: RESERVATION_OWNER
default: user4us
dsl: !include-raw-verbatim: ../scripts/qa-mobile-appium-tests.groovy
+64
View File
@@ -0,0 +1,64 @@
---
- job:
name: qa-runner
description: "Оркестратор: запускает тестовые job параллельно."
project-type: pipeline
concurrent: true
sandbox: true
properties:
- build-discarder:
num-to-keep: 50
parameters:
- choice:
name: BROWSER
choices:
- chrome
- firefox
- string:
name: BASE_URL
default: https://otus.ru
- string:
name: EXECUTION_MODE
default: selenoid
- string:
name: SELENOID_URL
default: http://host.docker.internal:4444/wd/hub
- choice:
name: HEADLESS
choices:
- "true"
- "false"
- string:
name: APP_URL
default: http://wiremock:8080/wishlist.apk
- string:
name: DB_URL
default: jdbc:postgresql://sql.otus.kartushin.su:5432/wishlist
- string:
name: DB_USER
default: student
- string:
name: DB_PASSWORD
default: "student"
- string:
name: WISHLISTS_USERNAME
default: user1us
- string:
name: WISHLISTS_PASSWORD
default: user1us
- string:
name: GIFTS_USERNAME
default: user2us
- string:
name: GIFTS_PASSWORD
default: user2us
- string:
name: RESERVATION_USERNAME
default: user3us
- string:
name: RESERVATION_PASSWORD
default: user3us
- string:
name: RESERVATION_OWNER
default: user4us
dsl: !include-raw-verbatim: ../scripts/qa-runner.groovy
@@ -0,0 +1,36 @@
---
- job:
name: qa-selenium-tests
description: "Selenium/Selenide тесты (homework_4) с выбором браузера."
project-type: pipeline
concurrent: true
sandbox: true
properties:
- build-discarder:
num-to-keep: 50
parameters:
- choice:
name: BROWSER
choices:
- chrome
- firefox
description: "Браузер для запуска UI-тестов"
- string:
name: BASE_URL
default: https://otus.ru
description: "Базовый URL тестируемого сайта"
- string:
name: EXECUTION_MODE
default: selenoid
description: "Режим запуска (local|selenoid)"
- string:
name: SELENOID_URL
default: http://host.docker.internal:4444/wd/hub
description: "URL Selenoid (используется при EXECUTION_MODE=selenoid)"
- choice:
name: HEADLESS
choices:
- "true"
- "false"
description: "Headless режим"
dsl: !include-raw-verbatim: ../scripts/qa-selenium-tests.groovy
+38
View File
@@ -0,0 +1,38 @@
---
- view:
name: DevOps
view-type: list
description: "Инфраструктурные job"
filter-executors: true
filter-queue: true
job-name:
- jobs-uploader
- infra-health-check
columns:
- status
- weather
- job
- last-success
- last-failure
- last-duration
- build-button
- view:
name: QA
view-type: list
description: "Тестовые job и раннер"
filter-executors: true
filter-queue: true
job-name:
- qa-runner
- qa-selenium-tests
- qa-api-citrus-tests
- qa-mobile-appium-tests
columns:
- status
- weather
- job
- last-success
- last-failure
- last-duration
- build-button