How to set up Jenkins on an EC2 Instance using BashScript
Tags: jenkins, bashscript, cicd, devops
Published: 2023-05-25 18:55:30

**Introduction**

[Jenkins](https://www.jenkins.io/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8fbzlx9t89jtk91bktee.PNG)

```
#!/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
```


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fbuegk2e1oyeymhgleg.PNG)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fi6tdgsfp2eqq8ygmxj.PNG)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xnzzeqbhn23pvdul6l76.PNG)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2geta4j4k2quoqpgt2q.PNG)

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**

- [Jenkins Official Documentation](https://www.jenkins.io/doc/book/installing/linux/#debianubuntu)

- [AWS Documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html?icmpid=docs_ec2_console#user-data-shell-scripts)