What is Jenkins?
Jenkins is an open-source continuous integration-continuous delivery and deployment (CI/CD) automation DevOps tool written in Java. It is used to implement CI/CD workflows, called pipelines.
Jenkins is a tool used for automation, and it is an open-source server that allows all the developers to build, test and deploy software. It works or runs on Java as it is written in Java. By using Jenkins we can continuously integrate projects(jobs) or end-to-endpoint automation.
Jenkins achieves Continuous Integration with the help of plugins. Plugins allow the integration of Various DevOps stages. If you want to integrate a particular tool, you need to install the plugins for that tool. For example Git, Maven 2 project, Amazon EC2, HTML publisher, etc.
Jenkins Jenkins is an open-source project written in Java, that runs on Windows, macOS, and other Unix-like OS. It is a free community supported and might be your first choice tool for CI (Continuous Integration).
Jenkins automates the entire software development life cycle.
Jenkins was originally developed by Sun Microsystem in 2004 under the name Hudson.
The project was later named Jenkins when oracle bought Microsystems.
It can run on any major platform without any compatibility issues.
Wherever developers write code, we integrate all that code and then build, test, and deliver/deploy to the client. This process is called CI/CD.
Because of CI, bugs will be reported fast and get rectified fast. So the entire software development happens fast.
Workflow of Jenkins
We can attach Git, Maven, Selenium, and Artifactory plugins to Jenkins.
Once developers put code in GitHub, Jenkins pulls that code and sends it to Maven for Build or other build tools.
Once the build is done, Jenkins pulls that code and sends it to Selenium for testing.
Once testing is done, Jenkins will pull that code and send it to Artifactory as per requirement.
We can also deploy with Jenkins.
Advantages of Jenkins
It has a lot of plugins available.
You can write your plugin.
You can use community plugins.
Jenkins is not just a tool. It is a framework, i.e: you can do whatever you want. All you need is just plugins.
We can attach slaves (nodes) to Jenkins’s master. It instructs others (slaves) to do Jobs. If slaves are not available, Jenkins itself does the Job.
Jenkins also behaves as a Cron Server replacement, i.e- can do the scheduled task.
It can create Labels.
Node
- A machine that is part of the Jenkins environment and capable of executing Pipelines or jobs. Both the Controller and Agents are considered to be Nodes.
Agent
Agent in Jenkins « An agentis typically a machine, or container, which connects to a Jenkins controller and executes tasks when directed by the controller
Pipeline - A pipeline is a collection of steps or jobs which are interlinked with one another in a sequence.
Declarative: Declarative is a more recent and advanced implementation of a pipeline as a code.
Scripted: Scripted was the first and most traditional implementation of the pipeline as a code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy.
How to set up Jenkins in Linux AWS EC2 instance
For setting Jenkins and also its node visit the below link
https://devopswithnilesh.hashnode.dev/setting-up-jenkins-master-node-on-aws
Freestyle Projects
Freestyle Projects, also known as Freestyle jobs, are one of the traditional and straightforward ways to configure and automate tasks in Jenkins. They provide a graphical interface that allows users to define build steps, post-build actions, and other configurations through a web-based form. Freestyle Projects are a good choice for simple build and automation tasks where the workflow can be represented using a series of build steps.
Key Components of a Freestyle Project:
Project Configuration: When creating a Freestyle Project, you'll typically configure the following settings:
General Settings: Project name, description, and other basic information.
Source Code Management (SCM): Choose the version control system (e.g., Git, Subversion) and configure repository details.
Build Triggers: Specify conditions that trigger the build, such as code commits, schedule, or manual initiation.
Build Environment: Set environment variables or tools required for the build.
Build: Define a series of build steps, such as compilation, testing, or packaging, using the graphical interface.
Post-build Actions: Specify actions to be taken after the build, like archiving artifacts, sending notifications, or triggering other jobs.
Build Steps: Freestyle Projects allow you to define build steps graphically by adding build actions in the order they should execute. Common build steps include:
Shell/Batch Commands: Execute shell or batch scripts.
Execute Windows Batch Command: Execute Windows-specific batch commands.
Invoke Ant/Maven/Gradle: Use build tools like Ant, Maven, or Gradle for project-specific tasks.
Execute Windows PowerShell: Execute PowerShell scripts on Windows systems.
Execute Groovy script: Execute Groovy scripts for custom automation.
Post-build Actions: After the build is complete, you can specify post-build actions to perform tasks like:
Archive Artifacts: Store important build artifacts for future reference or deployment.
Send Email Notifications: Notify team members about build results.
Publish JUnit Test Results: Display test results in Jenkins and generate reports.
Trigger Other Projects: Trigger downstream jobs or projects upon successful completion.
Deploy to Servers: Deploy the built artifacts to servers or environments.
Example of a free style project:-
Pre-requisite
Jenkins is installed
Jenkins is up and running.
Docker plugins are installed in Jenkins
Create a new Jenkins freestyle project for your app.
Log in to your Jenkins instance.
Click on "New Item" to create a new project.
Enter a name for your project (e.g...)
Select "Freestyle project" and click "OK".
On the project configuration page, you can specify the details of the project, such as the source code management system, build triggers, and build actions. In the GitHub project write your GitHub project repository URL.
Save the changes
Manually build the project by clicking on the "Build Now" link on the project's main page. This will start the building project.
Once the build is completed, you can view the output commands that are executed in the "Console Output".
same project as above with docker compose
Create a Jenkins project to run the "docker-compose up -d" command to start the multiple containers defined in the compose file
add a build step "docker-compose down" command to stop and remove the containers defined in the compose file.
Then add the "docker-compose up -d" command.
Sometimes it doesnot refresh container (so we add below command in docker compose)
docker-compose up -d <service_name>
docker-compose up -d --no-deps --build web ( web is taken from dockercomose.yml file)
Build the project
Once the build is completed, you can view the output in "Console Output"
Can browse the ip:8001
How to integrate GitHub hook in Jenkins so it does continuous deployment
For GitHub-Webhook:
Go to your GitHub repository and click on Settings.
Click on Webhooks and then click on Add Webhook.
In the ‘Payload URL’ field, paste your Jenkins environment URL. At the end of this URL add /GitHub-webhook/. In the ‘Content type’ select: ‘application/json’ and leave the ‘Secret’ field empty.
example :- http://54.237.28.224:8080/github-webhook/
if it ticked that means the git hub hook connection is established
in Jenkins do below
Project 3 :-
use git hub link https://github.com/Niljay892/node_to_do_app.git
create a free-style project with the same process
hint:-
For Jenkins CICD process click on below link
https://devopswithnilesh.hashnode.dev/jenkins-declarative-pipeline-with-project
Thank you for reading this blog! Hope you find this article helpful.
Happy Learning!