Skip to content

Commit e051731

Browse files
nre-abletonemmetog
authored andcommitted
Various cleanups and improvements (#52)
* Cleanup: wrap lines to 80 characters * Bump Python package versions * Make jenkins_plugins var default to empty list I'm not sure where these 8 plugins came from, but I don't think it makes sense to decide this for our users. * Only install python-software-properties on Xenial * Add a special molecule user We shouldn't add a Jenkins user, since that is done by this role anyways. Not adding a user will test that the role creates one successfully. * Test that the Jenkins user/group was created * Test that the Jenkins dir/config.xml were created * Test that job dirs/config.xml files were created * Test that custom files are deployed * Test that secrets are deployed * Test the Jenkins Java process runs as intended
1 parent bebdd57 commit e051731

File tree

10 files changed

+113
-48
lines changed

10 files changed

+113
-48
lines changed

README.md

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ Ansible Role for Jenkins
33

44
Installs and completely configures Jenkins using Ansible.
55

6-
This role is used when you want all your Jenkins configuration
7-
in version control so you can deploy Jenkins repeatably
8-
and reliably and you can treat your Jenkins as a [Cow instead
9-
of a Pet](https://blog.engineyard.com/2014/pets-vs-cattle).
10-
11-
If you are looking for a role to install Jenkins and you
12-
want to configure everything through the web interface and you
13-
don't care about being able to repeatably deploy this
14-
same fully-configured Jenkins then you don't need
15-
this role, have a look at the
6+
This role is used when you want all your Jenkins configuration in version
7+
control so you can deploy Jenkins repeatably and reliably and you can treat your
8+
Jenkins as a [Cow instead of a
9+
Pet](https://blog.engineyard.com/2014/pets-vs-cattle).
10+
11+
If you are looking for a role to install Jenkins and you want to configure
12+
everything through the web interface and you don't care about being able to
13+
repeatably deploy this same fully-configured Jenkins then you don't need this
14+
role, have a look at the
1615
[geerlingguy/ansible-role-jenkins](https://github.com/geerlingguy/ansible-role-jenkins)
1716
role instead.
1817

@@ -21,11 +20,10 @@ Requirements
2120

2221
Requires curl to be installed on the server.
2322

24-
If deploying using Docker then you need Docker
25-
installed on the server.
23+
If deploying using Docker then you need Docker installed on the server.
2624

27-
(Docker, apt-get and yum are the only supported ways at the moment
28-
although more ways can easily be added, PRs welcome).
25+
(Docker, apt-get and yum are the only supported ways at the moment although more
26+
ways can easily be added, PRs welcome).
2927

3028
Installation
3129
------------
@@ -199,21 +197,27 @@ The example above will look for the job configs in
199197
`{{ playbook_dir }}/jenkins-configs/jobs/my-first-job/config.xml` and
200198
`{{ playbook_dir }}/jenkins-configs/jobs/another-awesome-job/config.xml`.
201199
202-
***NOTE***: These directories are customizable, see the `jenkins_source_dir_configs` and `jenkins_source_dir_jobs` role variables.
200+
***NOTE***: These directories are customizable, see the
201+
`jenkins_source_dir_configs` and `jenkins_source_dir_jobs` role variables.
203202
204203
The role will also look for `{{ playbook_dir }}/jenkins-configs/config.xml`
205-
These config.xml will be templated over to the server to be used as the job configuration.
206-
It will upload the whole secrets directory under `{{ playbook_dir }}/jenkins-configs/secrets` and configure custom files provided under `{{ jenkins_custom_files }}` variable. Note that `{{ jenkins_include_secrets }}` and `{{ jenkins_include_custom_files }}` variables should be set to true for these to work.
207-
Additionally the role can install custom plugins by providing the .jpi or .hpi files as a list under `{{ jenkins_custom_plugins }}` variable.
208-
209-
config.xml and custom files are templated so you can put variables in them,
210-
for example it would be a good idea to encrypt sensitive variables
211-
in ansible vault.
204+
These config.xml will be templated over to the server to be used as the job
205+
configuration. It will upload the whole secrets directory under
206+
`{{ playbook_dir }}/jenkins-configs/secrets` and configure custom files provided
207+
under `{{ jenkins_custom_files }}` variable. Note that
208+
`{{ jenkins_include_secrets }}` and `{{ jenkins_include_custom_files }}`
209+
variables should be set to true for these to work. Additionally the role can
210+
install custom plugins by providing the .jpi or .hpi files as a list under
211+
`{{ jenkins_custom_plugins }}` variable.
212+
213+
config.xml and custom files are templated so you can put variables in them, for
214+
example it would be a good idea to encrypt sensitive variables in ansible vault.
212215
213216
Example Job Configs
214217
-------------------
215218
216-
Here's an example of what you could put in `{{ playbook_dir }}/jenkins-configs/jobs/my-first-job/config.xml`:
219+
Here's an example of what you could put in
220+
`{{ playbook_dir }}/jenkins-configs/jobs/my-first-job/config.xml`:
217221
218222
```xml
219223
<?xml version='1.0' encoding='UTF-8'?>
@@ -242,8 +246,9 @@ Here's an example of what you could put in `{{ playbook_dir }}/jenkins-configs/j
242246
Example Jenkins Configs
243247
-----------------------
244248

245-
In `{{ jenkins_source_dir_configs }}/config.xml` you put your global
246-
Jenkins configuration, for example:
249+
In `{{ jenkins_source_dir_configs }}/config.xml` you put your global Jenkins
250+
configuration, for example:
251+
247252
```xml
248253
<?xml version='1.0' encoding='UTF-8'?>
249254
<hudson>
@@ -333,10 +338,9 @@ Jenkins configuration, for example:
333338
Making Changes
334339
--------------
335340

336-
When you want to make a big change in a configuration file
337-
or you want to add a new job the normal workflow is to make
338-
the change in the Jenkins UI
339-
first, then copy the resulting XML back into your VCS.
341+
When you want to make a big change in a configuration file or you want to add a
342+
new job the normal workflow is to make the change in the Jenkins UI first, then
343+
copy the resulting XML back into your VCS.
340344

341345
License
342346
-------
@@ -348,8 +352,8 @@ Author Information
348352

349353
Made with love by Emmet O'Grady.
350354

351-
I am the founder of [NimbleCI](https://nimbleci.com) which
352-
builds Docker containers for feature branch workflow projects in Github.
355+
I am the founder of [NimbleCI](https://nimbleci.com) which builds Docker
356+
containers for feature branch workflow projects in Github.
353357

354-
I blog on my [personal blog](http://blog.emmetogrady.com) and
355-
about Docker related things on the [NimbleCI blog](https://blog.nimbleci.com).
358+
I blog on my [personal blog](http://blog.emmetogrady.com) and about Docker
359+
related things on the [NimbleCI blog](https://blog.nimbleci.com).

defaults/main.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,8 @@ jenkins_source_secrets: "{{ jenkins_source_dir_configs }}/secrets/"
4040
# The names of the jobs (config.xml must exist under jenkins_source_dir_jobs/job_name/)
4141
jenkins_jobs: []
4242

43-
# These plugins will be installed in the jenkins instance
44-
jenkins_plugins:
45-
- git
46-
- log-parser
47-
- copyartifact
48-
- workflow-aggregator
49-
- workflow-multibranch
50-
- docker-workflow
51-
- template-project
52-
- ec2
43+
# List of plugins to install in the Jenkins instance
44+
jenkins_plugins: []
5345

5446
# How long to wait for installing plugins, in seconds. If you are seeing failures in the
5547
# "Wait for plugins to finish installing" task with an error message like "Timeout when

molecule/default/Dockerfile.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ RUN \
2121
yum clean all; \
2222
fi
2323

24-
RUN useradd -d /jenkins -m -U jenkins
24+
RUN useradd -G sudo molecule
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<test-secret/>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, world!

molecule/default/playbook.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
vars:
55
jenkins_config_owner: "jenkins"
66
jenkins_config_group: "jenkins"
7+
jenkins_custom_files:
8+
- src: "userContent/index.html"
9+
dest: "userContent/index.html"
710
jenkins_home: "/jenkins"
11+
jenkins_include_custom_files: true
12+
jenkins_include_secrets: true
813
jenkins_install_via: "apt"
914
jenkins_jobs:
1015
- test_job

molecule/default/tests/test_default.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,60 @@ def test_jenkins_installed(host):
1515
assert package.is_installed
1616

1717

18+
def test_jenkins_user(host):
19+
assert host.user('jenkins').group == 'jenkins'
20+
assert host.user('jenkins').home == '/jenkins'
21+
22+
23+
def test_jenkins_dir(host):
24+
assert host.file('/jenkins').is_directory
25+
assert host.file('/jenkins').mode == 0o0755
26+
assert host.file('/jenkins').user == 'jenkins'
27+
assert host.file('/jenkins').group == 'jenkins'
28+
assert host.file('/jenkins/config.xml').is_file
29+
assert host.file('/jenkins/config.xml').user == 'jenkins'
30+
assert host.file('/jenkins/config.xml').group == 'jenkins'
31+
32+
33+
def test_jenkins_secrets_files(host):
34+
assert host.file('/jenkins/secrets').is_directory
35+
assert host.file('/jenkins/secrets').mode == 0o0700
36+
assert host.file('/jenkins/secrets').user == 'jenkins'
37+
assert host.file('/jenkins/secrets').group == 'jenkins'
38+
test_secret_file = host.file('/jenkins/secrets/com.example.secret.xml')
39+
assert test_secret_file.is_file
40+
assert test_secret_file.user == 'jenkins'
41+
assert test_secret_file.group == 'jenkins'
42+
43+
44+
def test_jenkins_job_files(host):
45+
assert host.file('/jenkins/jobs').is_directory
46+
assert host.file('/jenkins/jobs').user == 'jenkins'
47+
assert host.file('/jenkins/jobs').group == 'jenkins'
48+
assert host.file('/jenkins/jobs/test_job').is_directory
49+
assert host.file('/jenkins/jobs/test_job').user == 'jenkins'
50+
assert host.file('/jenkins/jobs/test_job').group == 'jenkins'
51+
test_job_config_file = host.file('/jenkins/jobs/test_job/config.xml')
52+
assert test_job_config_file.is_file
53+
assert test_job_config_file.user == 'jenkins'
54+
assert test_job_config_file.group == 'jenkins'
55+
56+
57+
def test_jenkins_custom_files(host):
58+
assert host.file('/jenkins/userContent').is_directory
59+
assert host.file('/jenkins/userContent').user == 'jenkins'
60+
assert host.file('/jenkins/userContent').group == 'jenkins'
61+
assert host.file('/jenkins/userContent/index.html').is_file
62+
assert host.file('/jenkins/userContent/index.html').user == 'jenkins'
63+
assert host.file('/jenkins/userContent/index.html').group == 'jenkins'
64+
65+
66+
def test_jenkins_java_process(host):
67+
process = host.process.get(command='/usr/bin/java')
68+
69+
assert '-Djenkins.install.runSetupWizard=false' in process.args
70+
71+
1872
def test_jenkins_version():
1973
master = Jenkins('http://127.0.0.1:8080')
2074
version = master.get_version()

requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
ansible==2.7.11
1+
ansible==2.8.6
22
docker-py==1.10.6
3-
molecule==2.20.1
4-
python-jenkins==1.4.0
5-
testinfra==1.19.0
3+
molecule==2.22
4+
python-jenkins==1.5.0
5+
testinfra==3.2.0

tasks/apt/install.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
shell: "/bin/false"
1313
state: present
1414

15+
# This package is only available for Xenial (16.04), see:
16+
# https://packages.ubuntu.com/search?keywords=python-software-properties
1517
- name: Install apt PPA dependencies
1618
apt:
1719
name: python-software-properties
20+
when: ansible_distribution_release == "xenial"
1821

1922
- name: Install the Debian keyring
2023
apt:

tasks/configure-jobs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
path: "{{ jenkins_home }}/jobs/{{ item }}"
99
state: directory
1010
mode: 0777
11+
owner: "{{ jenkins_config_owner }}"
12+
group: "{{ jenkins_config_group }}"
1113
with_items: "{{ jenkins_jobs }}"
1214

1315
- name: Jobs are present
1416
copy:
1517
src: "{{ jenkins_source_dir_jobs }}/{{ item }}/config.xml"
1618
dest: "{{ jenkins_home }}/jobs/{{ item }}/config.xml"
1719
mode: 0777
20+
owner: "{{ jenkins_config_owner }}"
21+
group: "{{ jenkins_config_group }}"
1822
with_items: "{{ jenkins_jobs }}"

0 commit comments

Comments
 (0)