Jenkins is an open-source project written in Java that runs on Windows, macOS, and
other Unix-like operating systems. The first point of attraction in Jenkins is that it is
open source. Apart from that, it is community-supported and primarily deployed
on-premises, but it can also run-on cloud servers. Its integrations with Docker and
Kubernetes take advantage of containers to roll out even more frequent releases.
In our project, we will use terraform to create our server on digital ocean and we will do the configuration and installation with Ansible.
Prerequisites
- Terraform already install
- Ansible already install
- A digital ocean account
Deploying the virtual machine
Once you get your token, you can generate an RSA key with ssh-keygen. You just have to run the command :
For security reasons, we are going to use a variable to store this token, we do not want any random person to get access to our environment, so it is good practice to store it in an environment variable.
We will call it DO_PAT, to do so, we run the command
export DO_PAT="your_personal_access_token"
To use the DigitalOcean provider with Terraform, you have to tell Terraform about it and configure the plugin with the proper credential variables. Create a file called provider.tf, which will store the configuration for the provider:
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
variable "do_token" {}
variable "pvt_key" {}
provider "digitalocean" {
token = var.do_token
}
data "digitalocean_ssh_key" "terraform" {
name = "terraform"
}
you can create your file server.tf, this file will have the config for your server in digital ocean.
resource "digitalocean_droplet" "server-1" {
image = "ubuntu-22-10-x64"
name = "jenkins"
region = "tor1"
size = "s-2vcpu-4gb"
ssh_keys = [
data.digitalocean_ssh_key.terraform.id
]
}
Setting the TF_LOG environment variable to 1 will enable detailed logging of what Terraform is trying to do. You can set it by running:
Initialize Terraform for your project by running:
terraform init
Run the terraform plan command to see the execution plan, or what Terraform will attempt to do to build the infrastructure you described. You will have to specify the values for your DigitalOcean Access Token and the path to your private key, as your configuration uses this information to access your Droplet to install Nginx. Run the following command to create a plan:
terraform plan \
-var "do_token=${DO_PAT}" \
-var "pvt_key=$HOME/.ssh/id_rsa"
run terraform apply command to execute the current plan:
terraform apply \
-var "do_token=${DO_PAT}" \
-var "pvt_key=$HOME/.ssh/id_rsa"
At the end of all this, you will have a droplet name Jenkins up and running in digitalOcean
Configure the Jenkins server.
below you have the playbook that will allow you to install your Jenkins server on ubuntu. Do not forget to create your inventory file, it is just a file with your server address. It tells ansible where are your servers
Just run the command:
ansible-playbook -i inventory playbook.yml
You should have a fully functional Jenkins server running on http://YOUR_SERVER_IP_ADDRESS:8080
Playbook.yml
---
- hosts: jenkins
become: true
become_user: root
tasks:
- name: Update apt repo and cache on all Debian/Ubuntu boxes
apt: update_cache=yes force_apt_get=yes cache_valid_time=3600
- name: Upgrade all packages on servers
apt: upgrade=dist force_apt_get=yes
- name: Check if a reboot is needed on all servers
register: reboot_required_file
stat: path=/var/run/reboot-required get_md5=no
- name: Reboot the box if kernel updated
reboot:
msg: "Reboot initiated by Ansible for kernel updates"
connect_timeout: 5
reboot_timeout: 300
pre_reboot_delay: 0
post_reboot_delay: 30
test_command: uptime
when: reboot_required_file.stat.exists
- name: Installing Java using Ansible
become: yes
apt:
name: "{{ packages }}"
state: present
vars:
packages:
- openjdk-11-jdk
- name: add the repository key to the system
ansible.builtin.shell: curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
args:
executable: /bin/bash
- name: add the repository key to the system
ansible.builtin.shell: echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
args:
executable: /bin/bash
- name: add the repository key to the system
ansible.builtin.shell: apt update -y
args:
executable: /bin/bash
- name: install jenkins
apt:
name: jenkins
state: latest
- name: start jenkins
service:
name: jenkins
enabled: true
state: started
inventory
[jenkins]
YOUR_SERVER_IP_ADDRESS
Comments
Post a Comment