Jenkins Demo For Beginners

ST Forza
6 min readNov 10, 2023

I was working on Jenkins CI/CD projects using AWS Cloud for a production project of Laravel PHP. I wanted to share this quick start guide to using Jenkins and introduce you to this powerful workhorse tool in the world of DevOps.

A Quick Intro to Jenkins:

Jenkins is an open-source automation server used for continuous integration and continuous delivery (CI/CD) in software development. It automates tasks like code building, testing, and deployment, streamlining the development process. Jenkins supports extensibility through plugins, enabling integration with various tools and platforms. Its distributed architecture allows tasks to be executed across multiple machines, and its user-friendly web interface simplifies configuration and monitoring of automation pipelines. With widespread adoption and an active community, Jenkins is at the heart of the modern DevOps workflow, and a versatile choice for organizations aiming to enhance their software delivery workflows.

Here are the steps to set up Jenkins on AWS EC2 instance for a Laravel PHP project:

1. Launch an EC2 instance on AWS.

2. Install Jenkins on the EC2 instance.

3. Install the necessary plugins for Jenkins.

Create a new Jenkins job and configure it to build the Laravel PHP project and test it by deploying it via a Jenkins pipeline.

Prerequisites:

1. Familiarity with AWS EC2

2. AWS Debian based EC2 instance

Steps:

1. Launch an EC2 instance on AWS

→ Login inside your AWS account

→ Search for EC2 instance in service search bar

→ Now click on launch new instance button

→ Give name to the instance

→ Select ubuntu image

→ Select the instance type: t2.micro or medium, whichever one you want

→ Click on create ‘new key pair’ to connect with instance via SSH

→ Give the appropriate name to the key pair select which ever key pair type, file format and click on Create key pair button

→ Now from the Network Settings section, create a Security Group and give it any name you want

→ Open port 8080 for Jenkins and port 8000 for Laravel, and launch the instance

→ Now to connect with your instance by clicking on the connect option after selecting the instance

→ From the EC2 instance connect tab, click on the connect button, and it will open a new tab with the instance terminal

2. Install Jenkins on the EC2 instance

→ Now run following commands in the terminal

sudo apt update
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt update
sudo apt install jenkins

→ Get back to the instance tab, and copy the Public IPV4 Address by selecting instance and open it in new tab. Like this (http://xx.xx.xx.xx:8080)

→ Now to unlock Jenkins, copy the Administrator Password by running the following command in terminal tab of the instance, and pass it in the tab. Then click on the continue button

sudo cat  /var/lib/jenkins/secrets/initialAdminPassword

3. Install the necessary plugins for Jenkins

→ Now select install Jenkins with the suggested plugins

→ Setup your user to login inside Jenkins

4. Lets create our first pipeline

→ Click on create job

→ Select pipeline option and give name to the pipeline

→ Enter the pipeline code inside the definition box, or copy and paste the code provided below, inside the pipeline definition box. Then click on save button

pipeline {
agent any

stages {
stage('Prerequsits & Essential Tools') {
steps {
// OS essentail tool
sh 'sudo apt-get -y install software-properties-common apt-transport-https git gnupg sudo nano wget curl zip unzip tcl inetutils-ping net-tools'

// PHP & its required extensions
sh 'sudo add-apt-repository ppa:ondrej/php'
sh 'sudo apt-get install -y php8.2 php8.2-fpm php8.2-bcmath php8.2-curl php8.2-imagick php8.2-intl php-json php8.2-mbstring php8.2-mysql php8.2-xml php8.2-zip'

// Composer installation to build and run project
sh '''sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"'''
sh '''sudo php -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"'''
sh 'sudo php composer-setup.php'
sh '''sudo php -r "unlink('composer-setup.php');"'''
sh 'sudo mv composer.phar /usr/local/bin/composer'
}
}
stage ('Build Project'){
steps {
sh 'sudo composer create-project laravel/laravel sample-app'
}
}
stage ('Deploy Project'){
steps {
sh 'cd sample-app && sudo nohup php artisan serve &'
}
}
stage('Testing'){
steps {
script {
def response = httpRequest 'http://localhost:8000'
if (response.status == 200) {
currentBuild.result = 'SUCCESS'
} else {
error 'Received a non-200 response code'
}
}
}
}
}

post {
success {
sh 'echo "Project is running successfully."'
}
}
}

→ Trigger the build by clicking on the ‘Build Now’ button from the side bar

→ Now you will see the results of pipeline

→ To access your application, select the EC2 instance under the Instance tab. Look for the public IPv4 address and copy it

→ Put port 8000 at the end the IP address like this (http://xx.xx.xx.xx:8000) inside the search bar. Congratulations your Laravel application is now deployed successfully

If this tutorial was helpful for you, please applaud and share it with others.

--

--

ST Forza
ST Forza

Written by ST Forza

DevOps Engineer and Cybersecurity Analyst

Responses (5)