Refactor the Jenkins role, now can be used on CentOS too.

This commit is contained in:
Andrea Dell'Amico 2019-01-24 13:20:24 +01:00
parent 2b058ee0b3
commit 48d5e03ea7
5 changed files with 204 additions and 136 deletions

View File

@ -5,6 +5,12 @@ jenkins_pkg_state: latest
jenkins_repo_key: 'https://pkg.jenkins.io/debian/jenkins-ci.org.key'
jenkins_stable_repo: 'deb http://pkg.jenkins.io/debian-stable binary/'
jenkins_latest_repo: 'deb http://pkg.jenkins.io/debian binary/'
jenkins_rh_stable_repo: https://pkg.jenkins.io/redhat-stable
jenkins_rh_stable_repo_key: https://pkg.jenkins.io/redhat-stable/jenkins.io.key
jenkins_rh_latest_repo: https://pkg.jenkins.io/redhat
jenkins_rh_latest_repo_key: https://pkg.jenkins.io/redhat/jenkins.io.key
jenkins_packages:
- jenkins
@ -76,3 +82,7 @@ jenkins_plugins:
enabled: True
build-timeout-plugin:
enabled: True
embeddable-build-status:
enabled: True
github-branch-source:
enabled: True

View File

@ -0,0 +1,52 @@
---
- block:
- name: Jenkins package repository key
apt_key: url='{{ jenkins_repo_key }}'
- name: Install the jenkins stable repository
apt_repository: repo='{{ jenkins_stable_repo }}' update_cache=yes
when: not jenkins_use_latest
- name: Install the jenkins latest repository
apt_repository: repo='{{ jenkins_latest_repo }}' update_cache=yes
when: jenkins_use_latest
- name: Install jenkins
apt: pkg={{ item }} state={{ jenkins_pkg_state }} update_cache=yes cache_valid_time=3600
register: jenkins_install
with_items: '{{ jenkins_packages }}'
- name: Install some jenkins requirements
apt: pkg={{ item }} state={{ jenkins_pkg_state }} update_cache=yes cache_valid_time=3600
with_items: '{{ jenkins_package_requirements }}'
- name: install sbt launcher
copy: src={{ item }} dest=/usr/local/lib/{{ item }}
with_items: '{{ jenkins_sbt_launch_jars }}'
- name: Set the startup jenkins options
template: src=jenkins.default.j2 dest=/etc/default/jenkins owner=root group=root mode=0444
notify: Restart jenkins
when: jenkins_install
tags: [ 'jenkins', 'jenkins_master' ]
- block:
- name: Ensure that jenkins is stoppend and disabled
service: name=jenkins state=stopped enabled=no
- name: Remove jenkins
apt: pkg={{ item }} state=absent
with_items: '{{ jenkins_packages }}'
- name: Remove the jenkins stable repository
apt_repository: repo='{{ jenkins_stable_repo }}' state=absent update_cache=yes
- name: Remove the jenkins latest repository
apt_repository: repo='{{ jenkins_latest_repo }}' state=absent update_cache=yes
- name: Remove the jenkins package repository key
apt_key: url='{{ jenkins_repo_key }}' state=absent
when: not jenkins_install
tags: [ 'jenkins', 'jenkins_master' ]

View File

@ -0,0 +1,85 @@
---
- block:
# Handle plugins
# If Jenkins is installed or updated, wait for pulling the Jenkins CLI, assuming 10s should be sufficiant
- name: 120 seconds delay while starting Jenkins
wait_for: port={{ jenkins_http_port }} delay={{ jenkins_restart_delay }}
# Create Jenkins CLI destination directory
- name: "Create Jenkins CLI destination directory"
file: path={{ jenkins_dest }} state=directory
- name: Get Jenkins CLI
get_url: url=http://localhost:{{ jenkins_http_port }}/jnlpJars/jenkins-cli.jar dest={{ jenkins_cli_dest }} mode=0440
# Create the Jenkins administrative user password file
- name: Create the Jenkins administrative user password file
copy: content={{ jenkins_admin_pwd }} dest={{ jenkins_admin_user_pwd_file }} mode=600
- name: Install plugins without a specific version
jenkins_plugin: name="{{ item.key }}" params='{{ jenkins_access_params }}'
register: my_jenkins_plugin_unversioned
when: '"version" not in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Install plugins with a specific version
jenkins_plugin: name="{{ item.key }}" version="{{ item.value['version'] }}" params='{{ jenkins_access_params }}'
register: my_jenkins_plugin_versioned
when: '"version" in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Initiate the jenkins_restart_required fact
set_fact:
jenkins_restart_required: no
- name: Check if restart is required by any of the versioned plugins
set_fact:
jenkins_restart_required: yes
when: item is changed
with_items: '{{ my_jenkins_plugin_versioned.results }}'
- name: Check if restart is required by any of the unversioned plugins
set_fact:
jenkins_restart_required: yes
when: item is changed
with_items: '{{ my_jenkins_plugin_unversioned.results }}'
- name: Restart Jenkins if required
become_user: root
service: name=jenkins state=restarted
when: jenkins_restart_required
- name: Wait for Jenkins to start up
uri:
url: '{{ jenkins_local_url }}'
status_code: 200
timeout: 5
register: jenkins_service_status
# Keep trying for 5 mins in 5 sec intervals
retries: 60
delay: 5
until: >
'status' in jenkins_service_status and
jenkins_service_status['status'] == 200
when: jenkins_restart_required
- name: Reset the jenkins_restart_required fact
set_fact:
jenkins_restart_required: no
when: jenkins_restart_required
- name: Plugin pinning
jenkins_plugin: name="{{ item.key }}" state="{{ 'pinned' if item.value['pinned'] else 'unpinned'}}" params='{{ jenkins_access_params }}'
when: '"pinned" in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Plugin enabling
jenkins_plugin: name="{{ item.key }}" state="{{ 'enabled' if item.value['enabled'] else 'disabled'}}" params='{{ jenkins_access_params }}'
when: '"enabled" in item.value'
with_dict: '{{ jenkins_plugins }}'
become: True
become_user: '{{ jenkins_username }}'
when: jenkins_install
tags: [ 'jenkins', 'jenkins_plugins', 'jenkins_master' ]

View File

@ -0,0 +1,48 @@
---
- block:
- name: Install the jenkins stable repository
yum_repository:
name: Jenkins
description: Jenkins stable repository
file: jenkins
baseurl: {{ jenkins_rh_stable_repo}}
enabled: yes
gpgcheck: True
gpgkey: {{ jenkins_rh_stable_repo_key }}
state: present
when: not jenkins_use_latest
- name: Install the jenkins latest repository
yum_repository:
name: Jenkins
description: Jenkins latest repository
file: jenkins
baseurl: {{ jenkins_rh_latest_repo}}
enabled: yes
gpgcheck: True
gpgkey: {{ jenkins_rh_latest_repo_key }}
state: present
when: jenkins_use_latest
- name: Install jenkins
yum: pkg={{ jenkins_packages }} state={{ jenkins_pkg_state }}
register: jenkins_install
when: jenkins_install
tags: [ 'jenkins', 'jenkins_master' ]
- block:
- name: Ensure that jenkins is stoppend and disabled
service: name=jenkins state=stopped enabled=no
- name: Remove jenkins
yum: pkg={{ jenkins_packages }} state=absent
- name: Remove the jenkins repository
yum_repository:
name: Jenkins
file: jenkins
state: absent
when: not jenkins_install
tags: [ 'jenkins', 'jenkins_master' ]

View File

@ -1,143 +1,16 @@
---
- import_tasks: jenkins_deb_pkgs.yml
when: ansible_distribution_file_variety == "Debian"
- import_tasks: jenkins_rh_pkgs.yml
when: ansible_distribution_file_variety == "RedHat"
- import_tasks: jenkins_plugins.yml
when: jenkins_install
- block:
- name: Jenkins package repository key
apt_key: url='{{ jenkins_repo_key }}'
- name: Install the jenkins stable repository
apt_repository: repo='{{ jenkins_stable_repo }}' update_cache=yes
when: not jenkins_use_latest
- name: Install the jenkins latest repository
apt_repository: repo='{{ jenkins_latest_repo }}' update_cache=yes
when: jenkins_use_latest
- name: Install jenkins
apt: pkg={{ item }} state={{ jenkins_pkg_state }} update_cache=yes cache_valid_time=3600
register: jenkins_install
with_items: '{{ jenkins_packages }}'
- name: Install some jenkins requirements
apt: pkg={{ item }} state={{ jenkins_pkg_state }} update_cache=yes cache_valid_time=3600
with_items: '{{ jenkins_package_requirements }}'
- name: install sbt launcher
copy: src={{ item }} dest=/usr/local/lib/{{ item }}
with_items: '{{ jenkins_sbt_launch_jars }}'
- name: Set the startup jenkins options
template: src=jenkins.default.j2 dest=/etc/default/jenkins owner=root group=root mode=0444
notify: Restart jenkins
- name: Ensure that jenkins is started and enabled
service: name=jenkins state=started enabled=yes
when: jenkins_install
tags: [ 'jenkins', 'jenkins_master' ]
- block:
# Handle plugins
# If Jenkins is installed or updated, wait for pulling the Jenkins CLI, assuming 10s should be sufficiant
- name: 120 seconds delay while starting Jenkins
wait_for: port={{ jenkins_http_port }} delay={{ jenkins_restart_delay }}
# Create Jenkins CLI destination directory
- name: "Create Jenkins CLI destination directory"
file: path={{ jenkins_dest }} state=directory
- name: Get Jenkins CLI
get_url: url=http://localhost:{{ jenkins_http_port }}/jnlpJars/jenkins-cli.jar dest={{ jenkins_cli_dest }} mode=0440
# Create the Jenkins administrative user password file
- name: Create the Jenkins administrative user password file
copy: content={{ jenkins_admin_pwd }} dest={{ jenkins_admin_user_pwd_file }} mode=600
- name: Install plugins without a specific version
jenkins_plugin: name="{{ item.key }}" params='{{ jenkins_access_params }}'
register: my_jenkins_plugin_unversioned
when: '"version" not in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Install plugins with a specific version
jenkins_plugin: name="{{ item.key }}" version="{{ item.value['version'] }}" params='{{ jenkins_access_params }}'
register: my_jenkins_plugin_versioned
when: '"version" in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Initiate the jenkins_restart_required fact
set_fact:
jenkins_restart_required: no
- name: Check if restart is required by any of the versioned plugins
set_fact:
jenkins_restart_required: yes
when: item is changed
with_items: '{{ my_jenkins_plugin_versioned.results }}'
- name: Check if restart is required by any of the unversioned plugins
set_fact:
jenkins_restart_required: yes
when: item is changed
with_items: '{{ my_jenkins_plugin_unversioned.results }}'
- name: Restart Jenkins if required
become_user: root
service: name=jenkins state=restarted
when: jenkins_restart_required
- name: Wait for Jenkins to start up
uri:
url: '{{ jenkins_local_url }}'
status_code: 200
timeout: 5
register: jenkins_service_status
# Keep trying for 5 mins in 5 sec intervals
retries: 60
delay: 5
until: >
'status' in jenkins_service_status and
jenkins_service_status['status'] == 200
when: jenkins_restart_required
- name: Reset the jenkins_restart_required fact
set_fact:
jenkins_restart_required: no
when: jenkins_restart_required
- name: Plugin pinning
jenkins_plugin: name="{{ item.key }}" state="{{ 'pinned' if item.value['pinned'] else 'unpinned'}}" params='{{ jenkins_access_params }}'
when: '"pinned" in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Plugin enabling
jenkins_plugin: name="{{ item.key }}" state="{{ 'enabled' if item.value['enabled'] else 'disabled'}}" params='{{ jenkins_access_params }}'
when: '"enabled" in item.value'
with_dict: '{{ jenkins_plugins }}'
become: True
become_user: '{{ jenkins_username }}'
when: jenkins_install
tags: [ 'jenkins', 'jenkins_plugins', 'jenkins_master' ]
- block:
- name: Ensure that jenkins is stoppend and disabled
service: name=jenkins state=stopped enabled=no
- name: Remove the jenkins requirements
apt: pkg={{ item }} state=absent
with_items: '{{ jenkins_package_requirements }}'
- name: Remove jenkins
apt: pkg={{ item }} state=absent
with_items: '{{ jenkins_packages }}'
- name: Remove the jenkins stable repository
apt_repository: repo='{{ jenkins_stable_repo }}' state=absent update_cache=yes
- name: Remove the jenkins latest repository
apt_repository: repo='{{ jenkins_latest_repo }}' state=absent update_cache=yes
- name: Remove the jenkins package repository key
apt_key: url='{{ jenkins_repo_key }}' state=absent
when: not jenkins_install
tags: [ 'jenkins', 'jenkins_master' ]