Installing Jenkins on AWS Server and Creating a Free Style Project on Jenkins:
What is Jenkins:
Jenkins is an open-source automation tool written in Java with plugins built for continuous integration. Jenkins is used to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It also allows you to continuously deliver your software by integrating with a large number of testing and deployment technologies.
How to install Jenkins on AWS Server :
There are various ways by which we can install Jenkins on AWS server.?
First Approach -?
Step 1:Launch an EC2 instance on AWS.?
Step 2: Connect to your EC2 instance using the EC2 Instance Connect option in AWS
??
Step 3: In the EC2 instance Connect terminal, run the command “sudo apt update”.
SUDO - This command runs any command as an ADMIN user.
APT - The full form is Advanced Packaging Tool.?
This command is used to update all the links on the Linux server to download any softwares as the server does not have an UI and hence we require APT which contains all the links required to download any software on the server.
Step 4: Since Jenkins is a Java based application we will need to install Java, using the command shown in the below screenshot.?
Step 5: Search ‘Jenkins’ keyword on Google > Go to Jenkins homepage > Click on the “Download” option.?
Step 6: Always choose the LTS “Long Term Support” version to download, as it would be the most stable version. Copy the URL of the '.war' file.?
Step 7: On the server run ‘sudo wget’ & paste the URL copied from the Jenkins website. wget command will download the ‘.war’ file from the URL address on to the server.
Step 8: To start jenkins on the server, run the command “java -jar jenkins.war” on the server.
Step 9: By default Jenkins will run on port number 8080. To enable the port number on the server for public access.?
Go to EC2 instance > Security > Security Group > Edit inbound rules > Enable Port no 8080.?
Keep Type: ‘Custom TCP’ ; Port: 8080 ; Source: ‘Any’ and Click on ‘Save Rules’.?
Step 10: Copy the public IPV4 address from EC2 instance and paste it in any browser followed by colon and port number as shown below. The ‘Unlock Jenkins’? Page will open up.?
Step 11: Copy and paste the password from the server and Click on continue > Install suggested Plugins
Step 12: Create the admin user for Jenkins by filling in the below shown details.?
And Jenkins is installed successfully.?
NOTE: The main drawback of this method of Jenkins installation is that Jenkins would be running as the main process and we would not be able to do anything else on the server. Also, if we close the session, Jenkins would stop working. However as Dev Ops, we would want jenkins to run 24*7 on the server.
Alternate Approach -
Step 1: We can create a shell script and run Jenkins as a backend process. To create a shell script file, run the command ‘nano launchScript.sh’ on the server.
Step 2: Inside this file, type the start jenkins command, ‘java -jar jenkins.war’.?
Step 3: Save the file and press Exit (^X).?
Step 4: Run ‘ls’ command to check if the ‘launchScript.sh’ file is created.?
Step 5: Run the command ‘sh launchScript.sh’ to execute the shell script file.?
Step 6: However on doing this, Jenkins will again start as a main process which we don't want.?
Therefore we will use the ‘nohup’ command which is used to run a shell script file as a background process.
Pros/Cons of this approach of installing Jenkins:
Pros: Jenkins is running in the background and hence we can perform any other action on the server.
Cons: If we stop the server or restart the server, we will have to start Jenkins all over again however in an ideal situation we would want Jenkins to start on its own.
Preferred Approach -
Step 1:? As a hygiene, we should stop the previous Jenkins process. Run ‘ps -ef’ to list all the running processes. Run ‘kill -9’ followed by the process id of the script to stop the process.?
Step 2: Go to Ubuntu/Debian on Jenkins Download page.?
Step 3: Copy the below highlighted command and run it on the server. This will download the Jenkins debian file on the server.?
Step 4: Then copy the ‘echo deb…’ command and run it on the server.?
Step 5: Run ‘sudo apt update’’ on the server.?
Step 6: Run ‘sudo apt install jenkins’ on the server.?
领英推荐
Step 7: Run ‘systemctl status jenkins’ to check the status of jenkins.?
?Step 8: We can get the Jenkins password in the logs of ‘systemctl status jenkins’ command.?
Step 9: Copy the password from the logs or go to the location shown in above screen to get the jenkins password and paste it in the Jenkins password section.?
Jenkins is ready to get started
Step 10: Fill in all the details to create a Jenkins admin user.?
Note: This is the preferred way of installing Jenkins. In this case, even if the Jenkins server is restarted or stopped we don't have to manually start Jenkins on the server.?
How to create a Freestyle project on Jenkins :?
Now on the Jenkins UI we will create a Freestyle Project.
Step 1: Enter an item name e.g: ‘MySampleProject’ and click on ‘Freestyle project’.?
Step 2: Go to General Tab > Description - Describe your project in brief.
Step 3: In the ‘Source Code Management’ section select ‘None’ .
Step 4: Build Steps > Add build Step > Execute shell.?
In the command section of the Execute Shell, write linux commands that you intend to be executed.?
Step 5: Click on Save and then click on ‘Build Now’ option.?
Step 6: Click on the build number which you have just executed and go to Console Output where you can see the logs which Jenkins has executed on the server.?
Step 7: We can also validate the same commands and the output on the server.?
Step 8: Go to Configure > General > Discard Old builds.?
This will free up the memory by deleting unused builds.It actually manages memory space.
How to parameterize the project -?
Step 1: Go to Configure > General > Select the option -’This project is parameterized’.?
Click on ‘Add parameter’ and select ‘Choice Parameter’ from the dropdown.?
?
Step 2: Create a Jenkins variable:?
Here the variable is used to select any folder.?
Step 3: In the ‘Build Steps’ section write your linux commands and use your jenkins variable with a $ sign.?
Due to this parameterization, when we run the build we will get an option to select the ‘folderName’ and based on the value selected, the other commands would be run.?
Step 4: We can also validate the same thing on EC2 instance server.?
How to create a ‘Cron Job’ for the project -?
In layman terms, if we want to automatically run a project at a certain time of the day/week/month or at a? regular interval of time, we can use ‘cron job’ to do this task.?
A cron job is a Linux command used for scheduling tasks to be executed sometime in the future.
Step 1: Go to Configure > Build Trigger > Build Periodically > Schedule
Step 2: Set your cron job :
For Example: Here the first Cron job will run at 14:26, Asia/Dubai time.?
The first cron job was automatically executed at 14.26. This was build number six in the below screenshot.?
Here you will notice that we didn't get an option to select our folderName. The first value of jenkins variable ($foldername) i.e. Anshu was selected by default.
However, if we want to run our cron jobs for different jenkins variable then:?
Step 3: Go to Manage Jenkins > Plugins > Available Plugins > Search for ‘Parameterized scheduler’ and install it.
Installing Parameterized scheduler plugin:
Once you will install the ‘Parameterized scheduler’ plugin then we will get a new option to run build periodically with parameters under the “Build Triggers’ section.?
Step 4: In the ‘Build Periodically with Parameters’ for each cron job we can define the parameters for the variable as shown below. Please note to use % sign before the jenkins variable.
The first cron job executed at 14:35 with folderName = Ansh
.
The second cron job at 14:36, as per the set schedule.?
The third cron job at 14:37, as per the set schedule.?
Thank you for reading through till the end. I hope you learned something new today with me.?
SVP Operations @ Yango
1 年ANSHU SINGH keep up the good work !