Files
pw/ansible/playbooks/site.yml
T

134 lines
4.6 KiB
YAML

---
- name: Deploy OTUS ProjectWork Jenkins infrastructure
hosts: local
gather_facts: true
vars:
project_root: "{{ playbook_dir }}/../.."
compose_dir: "{{ project_root }}/compose"
compose_file: "{{ compose_dir }}/docker-compose.yml"
compose_env_file: "{{ compose_dir }}/.env"
selenoid_video_output_dir: "{{ compose_dir }}/selenoid/video"
jobs_profile_effective: "{{ jobs_profile | default('full') }}"
jjb_paths:
full: /workspace/projectwork/config/jobs
devops: /workspace/projectwork/config/jobs-devops
jjb_upload_path: "{{ jjb_paths.get(jobs_profile_effective, jjb_paths.full) }}"
tasks:
- name: Check Docker CLI availability
ansible.builtin.command: docker --version
changed_when: false
- name: Check Docker Compose availability
ansible.builtin.command: docker compose version
changed_when: false
- name: Create compose env file from template if missing
ansible.builtin.copy:
src: "{{ compose_dir }}/.env.example"
dest: "{{ compose_env_file }}"
force: true
mode: "0644"
- name: Ensure image build script is executable
ansible.builtin.file:
path: "{{ compose_dir }}/scripts/build_and_push_images.sh"
mode: "0755"
- name: Pre-pull Jenkins base image (retry on Docker Hub timeouts)
ansible.builtin.command:
cmd: docker pull jenkins/jenkins:2.541.3-lts-jdk21
register: pull_jenkins_base
retries: 6
delay: 20
until: pull_jenkins_base.rc == 0
- name: Build and start core services (registry, jenkins, nginx, selenoid)
ansible.builtin.command:
cmd: docker compose -f {{ compose_file }} --env-file {{ compose_env_file }} up -d --build registry jenkins nginx selenoid selenoid-ui
args:
chdir: "{{ compose_dir }}"
environment:
SELENOID_VIDEO_OUTPUT_DIR: "{{ selenoid_video_output_dir }}"
register: compose_bootstrap
retries: 4
delay: 20
until: compose_bootstrap.rc == 0
- name: Wait for Jenkins to become available
ansible.builtin.uri:
url: "http://127.0.0.1:8081/login"
status_code: 200
register: jenkins_ready
retries: 60
delay: 5
until: jenkins_ready.status == 200
- name: Wait for Selenoid to become available
ansible.builtin.uri:
url: "http://127.0.0.1:4444/status"
status_code: 200
register: selenoid_ready
retries: 60
delay: 5
until: selenoid_ready.status == 200
- name: Pre-pull Selenoid browser and recorder images
ansible.builtin.command:
cmd: docker pull {{ item }}
loop:
- selenoid/vnc:chrome_128.0
- selenoid/vnc:firefox_125.0
- selenoid/video-recorder:latest-release
- name: Build and push slave/test images to local registry
ansible.builtin.command:
cmd: ./scripts/build_and_push_images.sh localhost:5005 1.0.0
args:
chdir: "{{ compose_dir }}"
register: build_and_push_result
retries: 3
delay: 20
until: build_and_push_result.rc == 0
- name: Start swarm agents
ansible.builtin.command:
cmd: docker compose -f {{ compose_file }} --env-file {{ compose_env_file }} up -d agent-maven agent-jjb
args:
chdir: "{{ compose_dir }}"
environment:
SELENOID_VIDEO_OUTPUT_DIR: "{{ selenoid_video_output_dir }}"
- name: Build jobs_uploader image with latest changes
ansible.builtin.command:
cmd: docker compose -f {{ compose_file }} --env-file {{ compose_env_file }} build jobs_uploader
args:
chdir: "{{ compose_dir }}"
environment:
SELENOID_VIDEO_OUTPUT_DIR: "{{ selenoid_video_output_dir }}"
- name: Remove stale jobs_uploader run containers
ansible.builtin.shell: |
ids="$(docker ps -aq --filter "name=jobs_uploader-run" || true)"
if [ -n "${ids}" ]; then
docker rm -f ${ids}
fi
args:
executable: /bin/bash
- name: Upload Jenkins jobs via JJB container
ansible.builtin.command:
cmd: timeout 900 docker compose -f {{ compose_file }} --env-file {{ compose_env_file }} run --rm -e JJB_PATH={{ jjb_upload_path }} jobs_uploader
args:
chdir: "{{ compose_dir }}"
environment:
SELENOID_VIDEO_OUTPUT_DIR: "{{ selenoid_video_output_dir }}"
- name: Show endpoint details
ansible.builtin.debug:
msg:
- "Jenkins UI: http://localhost:8088 (via nginx) or http://localhost:8081"
- "Registry: http://localhost:5005/v2/"
- "JJB profile: {{ jobs_profile_effective }}"
- "JJB path: {{ jjb_upload_path }}"