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
@@ -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/**'
}
}
}