- Install Jenkins on an EC2 (if you haven’t already) with an Ubuntu image and add ports 22 and 8080
- Create an EC2 in your Public Subnet of VPC
- Add ports 22 and 5000
- Install the following packages
- default-jre
- python3-pip
- python3.10-venv
- nginx
Access the UI of the Jenkins server EC2 (i.e. public_ip:8080) and select the Build Executor status in the bottom left corner of the menu.
Now enter the configurations below:
-
Name: awsDeploy
-
Description: Deployment server
-
Number of executors: 1
-
Remote root directory: /home/ubuntu/agent
-
Labels: awsDeploy
-
Usage: only build jobs with label…
-
Launch method: launch agents via ssh
-
Host: {Enter the public IP of your EC2 in the Public subnet and not this text}
-
Credentials:
- Select “Add” => “Jenkins” => Kind: “SSH username with private key”
- Enter the ID, Description, username (JenkinsAgent, Deployment agent server, ubuntu)
-
To add the key, select “Enter Directly” => select “add” => copy and paste the private key of the aws key pair used when creating your Jenkins server
into the white box and save.
-
Host key verification strategy: non verifying verification strategy
-
Availability: keep this agent online as much as possible
-
Save the configurations and wait for Jenkins to connect to the agent.
SSH into your Jenkins Agent EC2, your second EC2, and edit the “/etc/nginx/sites-enabled/default” configuration file.
Navigate to the server block and change port 80 with 5000
server {
listen 805000 default_server;
listen [::]:805000 default_server;
** Next navigate to the location block and replace it with the text below:**
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
pipeline {
agent any
stages {
stage ('Build') {
steps {
sh '''#!/bin/bash
python3 -m venv test3
source test3/bin/activate
pip install pip --upgrade
pip install -r requirements.txt
export FLASK_APP = application
flask run &
'''
}
}
stage ('test') {
steps {
sh '''#!/bin/bash
source test3/bin/activate
py.test --verbose --junit-xml test-reports/results.xml
'''
}
post {
always {
junit 'test-reports/results.xml'
}
}
}
stage ('Clean') {
agent {
label 'awsDeploy'
}
steps {
sh '''#!/bin/bash
if [[$(ps aux | grep - i "gunicorn" | tr -s " " | head -n 1 | cut -d " " -f 2) != 0]]
then
ps aux | grep -i "gunicorn" | tr -s " " | head - n 1 | cut -d " " -f 2 > pid.txt
kill $(cat pid.txt)
exit 0
fi
'''
}
}
stage ('Deploy') {
agent {
label 'awsDeploy'
}
steps {
keepRunning {
sh '''#!/bin/bash
pip install -r requirements.txt
pip install gunicorn
python3 -m gunicorn -w 4 application:app -b 0.0.0.0 --daemon
'''
}
}
}
}
}
Configure a multi branch pipeline Make sure you connect Jenkins to your GitHub Repo and then start your build!
$ sudo apt-get updates
$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get install default-jre git nodejs npm maven libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev
libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb -y
One of the challenges I faced while completing this deployment was not being able to link my Jenkins sever and Jenkins agent in addition to multiple failed builds. Why? For multiple reasons stated below, so be sure not to repeat my mistakes. When trying to establish a connection between my Jenkins server and agent nodes I struggled and eventually through some troubleshooting efforts with my colleagues I realized that I was using the wrong SSH key pair while setting up my Jenkins credentials. Also, I noticed I was using the wrong user and root directory in my Jenkins credentials set-up. I entered “jenkins” and “/home/jenkins” in the username and root directory fields instead of “ubuntu” and “home/ubuntu/agent”.
Another challenge I encountered was getting my Cypress test to pass. According to the logs from the console output my Cypress configuration file could not be located in the /home/ubuntu/agent02/Workspace folder.
https://www.youtube.com/watch?v=RkaqRsockfg https://www.youtube.com/watch?v=lOdrdV0eDrs https://www.jenkins.io/doc/book/installing/linux/#debianubuntu https://installati.one/ubuntu/22.04/python3.10-venv/