DEV Community

Osinachi
Osinachi

Posted on

How to set up Jenkins on an EC2 Instance using BashScript

Introduction

Jenkins is the leading open source development and operations automation server, it provides hundreds of plugins to support building, deploying and automating any project. As a DevOps Engineer, Jenkins has become a necessary tool for Continuous Integration and Deployment, CI/CD.
There are different ways to install and configure a Jenkins server, in this article, I will demonstrate how to get a Jenkins server up and running on an Amazon EC2 Instance using a simple bash script.

Requirements

  • Basic understanding of Bash scripting

  • AWS Account with access to the Console and a basic knowledge of how to launch an EC2 instance

Lets get started! 🚀

1. Go to the EC2 dashboard on AWS Console and launch an instance.

  • Give your instance a name eg. jenkins-server

  • Select an ubuntu server

  • Create or select an existing keypair

  • Create a security group for your instance allowing ssh access on Port 22 and also TCP access on Port 8080 which is the port Jenkins listens on

  • Scroll down to Advanced details and add the script below to the user data field

  • Launch Instance

  • It will take a while before you get the 2/2 checks passed because of the installations.

Image description

#!/bin/bash

# install Java

apt update -y
apt install openjdk-11-jdk -y
apt update -y

# Import the GPG key

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null

# Add the Jenkins software repository to the source list and provide the authentication key

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

# install jenkins
apt update -y
apt install jenkins -y
systemctl enable jenkins
Enter fullscreen mode Exit fullscreen mode

2. Check if Jenkins was installed successfully.
There are two ways you can confirm that Jenkins was successfully installed.

A. Connect to the server via ssh and run a status check

  • From AWS Console, choose to connect to the server via ssh

Image description

  • Open any command line tool, change into the directory where the instance's keypair is located. My keypair is called jen.pem located in my download folder.
  • Change the file permission by running chmod 400 jen.pem
  • Connect to the server via ssh by running the next code, replace the .pem file name and the server IP. ssh -i "jen.pem" ubuntu@ec2-34-201-67-199.compute-1.amazonaws.com

Image description

  • When you've successfully connected, run the following command: sudo systemctl status jenkins and you should get a Jenkins active status.

Image description

B. Open the server IP on a web browser

  • Copy the public IP address of the server and paste on a browser and add Jenkins Port 8080. 34.201.67.199:8080 then launch and you should connect to a Jenkins page.

Image description

Your Jenkins server is ready for use! 🚀

Things to Note

  • Scripts entered as user data are run as the root user, so do not use the sudo command in the script.

  • Also, because the script is not run interactively, you cannot include commands that require user feedback (such as apt update without the -y flag).

References

Top comments (0)