Skip to content

Commit d36013d

Browse files
authored
Merge pull request #681 from oracle-devrel/alcampag/ansible-jenkins
Resource manager stack to deploy Jenkins with Ansible playbook
2 parents abac334 + 25af306 commit d36013d

File tree

12 files changed

+836
-0
lines changed

12 files changed

+836
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ansible-jenkins
2+
3+
## Getting started
4+
5+
This Terraform code provisions a new OCI instance and installs Jenkins directly through an Ansible playbook.
6+
To optimize and be more cost-efficient, the instance shape is locked to VM.Standard.A1.Flex, but this code
7+
can eventually be modified or forked.
8+
9+
## Features and limitations
10+
* Get quickly started with the latest Jenkins version on OCI
11+
* Manage plugins and the installation through Ansible and Jenkins Configuration as Code
12+
* Tested on Oracle Linux 8
13+
* Instance generated only if it is in a public subnet network
14+
* Port 22 must be opened on the instance, as OCI Resource Manager will need to connect to the instance through SSH
15+
* Jenkins port can't be between 0 and 1024, as those are Linux reserved ports and would require further configurations to be exposed
16+
* To access Jenkins, the instance and Jenkins port must be reachable
17+
* As the instance will be updated, it will take a while during the first run
18+
19+
Although these limitations might not fit every use case, the code can be used as a reference and there are ways to lift them.
20+
21+
[![Deploy to Oracle Cloud](https://oci-resourcemanager-plugin.plugins.oci.oraclecloud.com/latest/deploy-to-oracle-cloud.svg)](https://cloud.oracle.com/resourcemanager/stacks/create?zipUrl=https://github.com/oracle-devrel/technology-engineering/raw/main/app-dev/devops/ansible-jenkins/ansible-jenkins-rm.zip)
22+
Binary file not shown.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
# Copyright (c) 2023 Oracle and/or its affiliates.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or data
8+
# (collectively the "Software"), free of charge and under any and all copyright
9+
# rights in the Software, and any and all patent rights owned or freely
10+
# licensable by each licensor hereunder covering either (i) the unmodified
11+
# Software as contributed to or provided by such licensor, or (ii) the Larger
12+
# Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
# one is included with the Software (each a "Larger Work" to which the Software
17+
# is contributed by such licensors),
18+
# without restriction, including without limitation the rights to copy, create
19+
# derivative works of, display, perform, and distribute the Software and make,
20+
# use, sell, offer for sale, import, export, have made, and have sold the
21+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
# either these or other terms.
23+
#
24+
# This license is subject to the following condition:
25+
# The above copyright notice and either this complete permission notice or at
26+
# a minimum a reference to the UPL must be included in all copies or
27+
# substantial portions of the Software.
28+
#
29+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
# SOFTWARE.
36+
37+
- name: Install and configure Jenkins
38+
hosts: all
39+
remote_user: opc
40+
# collections:
41+
# - oracle.oci
42+
vars:
43+
jenkins_port: "{{ jenkins_port }}"
44+
jenkins_casc_path: /var/lib/jenkins/jenkins_config.yaml
45+
jenkins_plugins: "configuration-as-code job-dsl github credentials workflow-multibranch workflow-aggregator pipeline-stage-view git oracle-cloud-infrastructure-devops oracle-cloud-infrastructure-compute bouncycastle-api ssh-credentials"
46+
jenkins_admin_pwd: "{{ jenkins_admin_pwd }}"
47+
48+
49+
tasks:
50+
- set_fact:
51+
public_ip: "{{ instance_host }}"
52+
- name: Instance public ip
53+
debug:
54+
var: public_ip
55+
- block:
56+
- name: Add Jenkins yum repository
57+
ansible.builtin.yum_repository:
58+
name: jenkins-rpm-lts
59+
description: Jenkins RPM packages
60+
baseurl: http://pkg.jenkins.io/redhat-stable
61+
become: true
62+
63+
- name: Import jenkins key
64+
ansible.builtin.rpm_key:
65+
state: present
66+
key: https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
67+
become: true
68+
69+
- name: yum update
70+
yum:
71+
name: '*'
72+
state: latest
73+
become: true
74+
75+
- name: Install git
76+
yum:
77+
name: git
78+
state: present
79+
become: true
80+
81+
- name: Install java
82+
yum:
83+
name: java-17-openjdk
84+
state: present
85+
become: true
86+
87+
- name: Install jenkins dependencies
88+
yum:
89+
name: fontconfig
90+
state: present
91+
become: true
92+
93+
- name: Install Jenkins
94+
ansible.builtin.yum:
95+
name: jenkins
96+
state: latest
97+
become: true
98+
99+
- name: Start jenkins
100+
ansible.builtin.systemd:
101+
daemon_reload: yes
102+
enabled: true
103+
name: jenkins
104+
state: started
105+
become: true
106+
107+
- name: Get Jenkins CLI
108+
get_url:
109+
url: http://localhost:8080/jnlpJars/jenkins-cli.jar
110+
dest: /home/opc/jenkins-cli.jar
111+
mode: "0777"
112+
113+
- name: Get initial admin password
114+
command: "cat /var/lib/jenkins/secrets/initialAdminPassword"
115+
register: result
116+
become: true
117+
- set_fact:
118+
initial_admin_pass: "{{ result.stdout }}"
119+
120+
- name: Check if plugins folder is empty before proceeding
121+
find:
122+
paths: '/var/lib/jenkins/plugins/'
123+
register: pluginsFound
124+
125+
- name: Install plugins
126+
shell: |
127+
java -jar jenkins-cli.jar -s http://127.0.0.1:{{ jenkins_port }}/ -auth admin:{{ initial_admin_pass }} install-plugin {{ jenkins_plugins }}
128+
when: pluginsFound.matched == 0 # Only install the plugin with default admin password if it is a first installation
129+
130+
- name: Copy Jenkins CasC configs
131+
template:
132+
src: ./templates/jenkins_config.yaml.j2
133+
dest: "{{ jenkins_casc_path }}"
134+
owner: opc
135+
group: opc
136+
mode: '0644'
137+
become: true
138+
139+
- name: Create jenkins.service.d directory
140+
file:
141+
path: /etc/systemd/system/jenkins.service.d/
142+
state: directory
143+
owner: root
144+
group: root
145+
mode: 0755
146+
become: true
147+
148+
- name: Copy jenkins.service drop-in
149+
template:
150+
src: ./templates/jenkins.service.j2
151+
dest: /etc/systemd/system/jenkins.service.d/override.conf
152+
owner: root
153+
group: root
154+
mode: 0644
155+
become: true
156+
157+
- name: Install plugins
158+
shell: |
159+
java -jar jenkins-cli.jar -s http://127.0.0.1:{{ jenkins_port }}/ -auth admin:{{ jenkins_admin_pwd }} install-plugin {{ jenkins_plugins }}
160+
when: pluginsFound.matched > 0
161+
162+
- name: Restart Jenkins
163+
systemd:
164+
daemon_reload: yes
165+
name: jenkins
166+
state: restarted
167+
become: true
168+
169+
- name: Add firewall rules
170+
shell: |
171+
firewall-cmd --permanent --zone=public --add-service=jenkins
172+
firewall-cmd --zone=public --add-port=50000/tcp --permanent
173+
firewall-cmd --reload
174+
become: true
175+
176+
rescue:
177+
- import_tasks: rollback.yaml
178+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
# Copyright (c) 2023 Oracle and/or its affiliates.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or data
8+
# (collectively the "Software"), free of charge and under any and all copyright
9+
# rights in the Software, and any and all patent rights owned or freely
10+
# licensable by each licensor hereunder covering either (i) the unmodified
11+
# Software as contributed to or provided by such licensor, or (ii) the Larger
12+
# Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
# one is included with the Software (each a "Larger Work" to which the Software
17+
# is contributed by such licensors),
18+
# without restriction, including without limitation the rights to copy, create
19+
# derivative works of, display, perform, and distribute the Software and make,
20+
# use, sell, offer for sale, import, export, have made, and have sold the
21+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
22+
# either these or other terms.
23+
#
24+
# This license is subject to the following condition:
25+
# The above copyright notice and either this complete permission notice or at
26+
# a minimum a reference to the UPL must be included in all copies or
27+
# substantial portions of the Software.
28+
#
29+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35+
# SOFTWARE.
36+
37+
- name: Remove Jenkins yum repository
38+
ansible.builtin.yum_repository:
39+
name: jenkins-rpm-lts
40+
description: Jenkins RPM packages
41+
baseurl: http://pkg.jenkins.io/redhat-stable
42+
become: true
43+
44+
- name: Remove jenkins key
45+
ansible.builtin.rpm_key:
46+
state: absent
47+
key: https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
48+
become: true
49+
50+
- name: Uninstall Jenkins
51+
ansible.builtin.yum:
52+
name: jenkins
53+
state: absent
54+
become: true
55+
56+
- name: Remove firewall rule
57+
shell: |
58+
firewall-cmd --permanent --zone=public --remove-service=jenkins
59+
firewall-cmd --reload
60+
become: true
61+
62+
- name: Uninstall java
63+
yum:
64+
name: java-17-openjdk
65+
state: absent
66+
become: true
67+
68+
- name: Uninstall jenkins dependencies
69+
yum:
70+
name: fontconfig
71+
state: absent
72+
become: true
73+
74+
- name: Uninstall git
75+
yum:
76+
name: git
77+
state: absent
78+
become: true
79+
80+
- name: Remove Jenkins configs
81+
file:
82+
path: "{{ jenkins_casc_path }}"
83+
state: absent
84+
become: true
85+
86+
- name: Remove Jenkins CLI jar
87+
file:
88+
path: "/home/opc/jenkins-cli.jar"
89+
state: absent
90+
91+
- name: Remove jenkins service drop-in
92+
file:
93+
path: "/etc/systemd/system/jenkins.service.d/override.conf"
94+
state: absent
95+
become: true
96+
97+
- name: Remove jenkins home folder content
98+
file:
99+
path: "/var/lib/jenkins/"
100+
state: absent
101+
become: true
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) 2023 Oracle and/or its affiliates.
4+
#
5+
# The Universal Permissive License (UPL), Version 1.0
6+
#
7+
# Subject to the condition set forth below, permission is hereby granted to any
8+
# person obtaining a copy of this software, associated documentation and/or data
9+
# (collectively the "Software"), free of charge and under any and all copyright
10+
# rights in the Software, and any and all patent rights owned or freely
11+
# licensable by each licensor hereunder covering either (i) the unmodified
12+
# Software as contributed to or provided by such licensor, or (ii) the Larger
13+
# Works (as defined below), to deal in both
14+
#
15+
# (a) the Software, and
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software (each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
# without restriction, including without limitation the rights to copy, create
20+
# derivative works of, display, perform, and distribute the Software and make,
21+
# use, sell, offer for sale, import, export, have made, and have sold the
22+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
# either these or other terms.
24+
#
25+
# This license is subject to the following condition:
26+
# The above copyright notice and either this complete permission notice or at
27+
# a minimum a reference to the UPL must be included in all copies or
28+
# substantial portions of the Software.
29+
#
30+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
# SOFTWARE.
37+
38+
export ANSIBLE_HOST_KEY_CHECKING=False
39+
40+
ansible-playbook play.yaml --extra-vars "instance_host=$HOST jenkins_port=$JENKINS_PORT jenkins_admin_pwd=$JENKINS_ADMIN_PWD" --private-key ./private.key -i $HOST,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[Service]
2+
Environment="JAVA_OPTS=-Djava.awt.headless=true -Dcasc.jenkins.config={{ jenkins_casc_path }} -Djenkins.install.runSetupWizard=false"
3+
Environment="JENKINS_PORT={{ jenkins_port }}"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
jenkins:
2+
systemMessage: |
3+
Welcome to our Jenkins build service!
4+
5+
slaveAgentPort: 50000
6+
crumbIssuer:
7+
standard:
8+
excludeClientIPFromCrumb: true
9+
securityRealm:
10+
local:
11+
allowsSignup: false
12+
users:
13+
- id: "admin"
14+
password: {{ jenkins_admin_pwd }}
15+
authorizationStrategy:
16+
loggedInUsersCanDoAnything:
17+
allowAnonymousRead: false
18+
numExecutors: 0
19+
mode: EXCLUSIVE
20+
21+
unclassified:
22+
location:
23+
url: "http://{{ public_ip }}:{{ jenkins_port }}"

0 commit comments

Comments
 (0)