Unit Iii Continuous Integration Using Jenkins Notes
Unit Iii Continuous Integration Using Jenkins Notes
Unit Iii Continuous Integration Using Jenkins Notes
Contents
1. Install & Configure Jenkins ------------------------------------------------------------------------------------------------------- 1
2. Jenkins Architecture Overview -------------------------------------------------------------------------------------------------- 4
3. Creating a Jenkins Job and Configuring a Jenkins job --------------------------------------------------------------------- 8
4. Introduction to Plugins---------------------------------------------------------------------------------------------------------- 12
5. Adding Plugins to Jenkins ------------------------------------------------------------------------------------------------------ 15
6. Commonly used plugins (Git Plugin, Parameter Plugin, HTML Publisher, Copy Artifact and Extended choice
parameters). ----------------------------------------------------------------------------------------------------------------------------- 17
7. Configuring Jenkins to work with java, Git and Maven ----------------------------------------------------------------- 21
8. Creating a Jenkins Build and Jenkins workspace ------------------------------------------------------------------------- 32
2 Marks: ----------------------------------------------------------------------------------------------------------------------------------- 36
bash
bash
bash
java –version
1
3. Add the Jenkins Repository:
bash
4. Install Jenkins:
bash
bash
7. Unlock Jenkins:
bash
- Copy the key and paste it into the Jenkins setup page.
- After unlocking Jenkins, follow the setup wizard and choose to install suggested plugins.
For Windows
1. Download Jenkins:
2
- Download the Windows Installer.
- Double-click the `.msi` file you downloaded and follow the installation wizard.
3. Install Java:
- If Java is not already installed, the installer will prompt you to install it.
4. Start Jenkins:
- Jenkins will start automatically after installation. You can also start it manually via the Windows Services
panel or from the Start menu.
6. Unlock Jenkins:
- Copy the key and paste it into the Jenkins setup page.
9. Complete Setup:
Post-Installation Configuration
- Set Up Additional Plugins: Navigate to `Manage Jenkins` > `Manage Plugins` to install additional plugins as
needed.
- Configure Security: Go to `Manage Jenkins` > `Configure Global Security` to set up security settings,
including user permissions.
- Create Jobs: Begin creating Jenkins jobs by navigating to `New Item` and selecting the type of job you want
to create.
Jenkins is now installed and configured on your system. You can start creating and managing CI/CD pipelines
to automate your workflows.
3
2. Jenkins Architecture Overview
1. Jenkins Master
Role: The Jenkins Master is the core component of Jenkins. It is responsible for the overall
management of the Jenkins environment.
Responsibilities:
Job Scheduling: The master schedules jobs for execution based on triggers, such as code
commits or time-based schedules.
Build Coordination: It dispatches build tasks to Jenkins Agents and monitors their progress.
User Interface: Provides a web-based dashboard for users to configure, monitor, and manage
Jenkins jobs, pipelines, and system settings.
Plugin Management: Handles the installation, upgrade, and configuration of plugins, which
extend Jenkins functionalities.
Build Queue Management: Manages a queue of build jobs waiting to be executed and
determines which jobs are dispatched to which agents.
4
Responsibilities:
Job Execution: Execute build tasks and run scripts or commands as specified in the Jenkins
job configurations.
Environment Isolation: Provide different environments for building and testing, which is
useful for handling diverse build requirements and testing scenarios.
Resource Allocation: Help distribute the build load across multiple machines, ensuring
efficient use of available resources and reducing the time for job execution.
Types of Agents:
Static Agents: Pre-configured machines that are always available to execute jobs.
3. Jenkins Executors
Role: Executors are components within Jenkins Agents that are responsible for running the actual
build tasks. Each agent can have multiple executors.
Responsibilities:
Concurrency: Executors allow multiple jobs to be run in parallel on the same agent. The
number of executors determines how many jobs an agent can handle simultaneously.
Resource Management: Helps optimize the use of resources on the Jenkins Agent, ensuring
that jobs do not interfere with each other and that the agent's resources are utilized effectively.
HTTP/HTTPS: Jenkins Master communicates with Agents using HTTP or HTTPS protocols.
This is the default communication method.
JNLP (Java Network Launch Protocol): Agents can also connect to the Master using JNLP,
particularly when the Agent is started in a headless environment.
Security:
SSL/TLS Encryption: Communication between the Jenkins Master and Agents can be
encrypted using SSL/TLS to secure the data transmitted over the network.
Agent Authentication: Agents authenticate with the Master using credentials or secret keys to
ensure that only authorized agents can connect and execute jobs.
5
5. Jenkins Plugins
Role: Plugins extend Jenkins' capabilities and integrate it with various tools and systems.
Types of Plugins:
Build Tools: Plugins for integrating with build tools like Maven, Gradle, and Ant.
Source Control: Plugins for version control systems like Git, Subversion, and Mercurial.
Notification: Plugins for sending notifications via email, Slack, Microsoft Teams, etc.
Testing and Reporting: Plugins for test result reporting, code quality analysis, and coverage
metrics.
Management:
Installation: Plugins can be installed and managed through the Jenkins web interface under
Manage Jenkins > Manage Plugins.
Updates: Plugins are regularly updated to fix bugs, add new features, and maintain
compatibility with Jenkins.
Role: Jobs are the basic units of work in Jenkins. They define what actions to perform, such as
building code, running tests, or deploying applications.
Pipelines:
Role: Pipelines represent more complex workflows than standard jobs and are defined using a
domain-specific language (DSL) called Pipeline DSL (Groovy-based).
Components:
o Stages: Logical divisions within a pipeline that group related steps (e.g., Build, Test,
Deploy).
o Declarative Pipeline: A simpler syntax for defining pipelines, suitable for most users.
o Scripted Pipeline: A more flexible but complex syntax for defining pipelines using
Groovy scripting.
6
7. Configuration and Storage
Configuration Files:
Jenkins Home Directory: Contains configuration files, job definitions, plugin data, and build
artifacts. Located typically at /var/lib/jenkins on Linux or C:\Program Files (x86)\Jenkins on
Windows.
Job Configurations: Stored in XML files within the jobs directory under the Jenkins home
directory.
Build Artifacts:
Storage: Build outputs, logs, and artifacts are stored in the workspace and build directories
within the Jenkins home directory.
Archiving: Jenkins allows you to archive build artifacts for future reference and retrieval.
8. Security
User Authentication:
Built-in User Database: Jenkins can use its own user database for authentication.
External Authentication: Jenkins can integrate with external systems like LDAP, Active
Directory, or OAuth providers for user authentication.
7
Authorization:
Role-Based Access Control: Configure permissions for different users and groups to control
access to Jenkins features and job configurations.
Matrix-Based Security: Allows fine-grained control over permissions for various Jenkins
components.
Open Jenkins Dashboard: Open your web browser and navigate to your Jenkins instance (e.g.,
http://localhost:8080).
o Enter a name for your job in the Enter an item name field. This name should be descriptive of
what the job will do.
o Freestyle Project: Suitable for simple build jobs. This is the most commonly used type for a
basic setup.
o Pipeline: For creating a more complex pipeline using Jenkins Pipeline DSL. Choose this if you
need a multi-step, multi-stage pipeline.
o Other Types: There are other types such as Maven, Multibranch Pipeline, etc., depending on
your needs.
Click OK: Click OK to create the job and move to the configuration page.
General Settings:
o Description: Add a description of what this job does. This helps in understanding the job's
purpose.
o Discard Old Builds: Configure settings to discard old builds to save disk space (optional).
8
Source Code Management (SCM):
o Choose SCM System: Select the version control system (e.g., Git, Subversion) that your
project uses.
o Credentials: Provide credentials if required (e.g., username and password, SSH keys).
Build Triggers:
o Build Trigger Options: Select triggers that will start the build, such as:
Build Environment:
o Configure Build Environment: Set up environment variables or configurations needed for the
build.
Build Steps:
o Add Build Step: Click on Add build step to define what actions should be performed during the
build. Examples include:
Post-build Actions:
o Add Post-build Action: Define actions to be taken after the build completes, such as:
Send Build Artifacts Over SSH: Transfer build artifacts to another server.
Pipeline Definition:
o Pipeline Script: Define your pipeline script in the Pipeline section using the Jenkins Pipeline
DSL. You can write it directly in the Pipeline Script box or refer to a Jenkinsfile stored in your
source control repository.
9
Example of a Simple Declarative Pipeline Script:
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
stage('Test') {
steps {
echo 'Testing...'
stage('Deploy') {
steps {
echo 'Deploying...'
post {
always {
} }
10
Example of a Simple Scripted Pipeline Script:
groovy
node {
stage('Build') {
echo 'Building...'
stage('Test') {
echo 'Testing...'
stage('Deploy') {
echo 'Deploying...'
stage('Cleanup') {
Save Configuration: Click Save at the bottom of the configuration page to save your job configuration.
o Monitor the build progress and view logs under the Build History section.
Build Status: Check the build status on the job page. Jenkins will indicate if the build was successful,
unstable, or failed.
View Console Output: Click on the build number in the Build History to view detailed logs and output
of the build.
11
6. Additional Configuration (Optional)
Configure Notifications: Set up notifications to alert you about build results via email or other
channels.
Manage Parameters: If your job needs parameters (e.g., build versions, environment configurations),
you can configure them under the General section by selecting This project is parameterized.
Summary
By following these steps, you can create and configure a Jenkins job tailored to your build, test, and
deployment needs. Jenkins provides flexibility to handle various types of projects and workflows,
from simple single-step builds to complex multi-stage pipelines.
4. Introduction to Plugins
Jenkins plugins are essential for extending the functionality of the Jenkins automation server. They
enable Jenkins to integrate with a variety of tools, services, and technologies, making it highly
adaptable to different development and deployment needs. Here’s an introduction to Jenkins plugins,
including their purpose, types, and how to manage them.
Enhancement: Add new features to Jenkins, such as advanced reporting, notifications, and
visualizations.
Customization: Allow customization of Jenkins workflows and interfaces to better fit specific
needs or preferences.
12
2. Build Tools Plugins:
o Maven Plugin: Supports Maven builds and integrates with Maven repositories.
3. Notification Plugins:
5. Deployment Plugins:
6. Pipeline Plugins:
o Pipeline: Groovy Plugin: Provides the ability to use Groovy scripting in Jenkins
Pipelines.
7. Security Plugins:
8. Utility Plugins:
o Build Timeout Plugin: Adds build timeout functionality to prevent builds from
hanging indefinitely.
13
Managing Jenkins Plugins
1. Installing Plugins:
Install Plugins:
o Install Without Restart: Click Install without restart to install the plugins immediately, or
choose Download now and install after restart to install them after restarting Jenkins.
2. Updating Plugins:
Update Plugins:
o Select plugins to update and click Download now and install after restart or Install without
restart.
3. Uninstalling Plugins:
Remove Plugins:
4. Plugin Configuration:
Configure Plugins:
o Some plugins require additional configuration. After installing a plugin, you may need
to configure its settings through Manage Jenkins > Configure System or through dedicated
configuration pages for specific plugins.
Minimize Plugin Use: Only install necessary plugins to avoid potential security vulnerabilities
and performance issues.
14
Backup Jenkins: Regularly back up your Jenkins configuration and plugins to prevent data
loss during updates or plugin failures.
Check Compatibility: Ensure that plugins are compatible with your Jenkins version and other
installed plugins to avoid conflicts.
Summary
Jenkins plugins play a critical role in enhancing and extending Jenkins’s capabilities. They allow
integration with various tools, improve functionality, and provide additional features to support
complex build, test, and deployment workflows. Proper management and configuration of plugins are
essential to maintaining a robust and effective Jenkins environment.
Adding plugins to Jenkins is a straightforward process that allows you to extend Jenkins's
functionality and integrate it with various tools and services. Here’s a step-by-step guide to help you
add and manage plugins in Jenkins:
From the Jenkins dashboard, go to Manage Jenkins (usually found on the left sidebar).
Available Tab: Click on the Available tab to browse or search for plugins that are available for
installation.
Search Plugins: Use the search box to find specific plugins by name or functionality. For example, if
you want to install the Git plugin, type "Git" in the search box.
4. Install Plugins
Select Plugins:
o Install without Restart: Click Install without restart to install the plugins immediately. This
option will install the plugins without requiring Jenkins to restart, allowing you to continue
working while the installation is in progress.
15
o Download Now and Install After Restart: Click Download now and install after restart if you
prefer to install the plugins after Jenkins restarts. This option is useful for installing a large
number of plugins or if you want to minimize interruptions.
Installation Progress:
o Jenkins will download and install the selected plugins. You will see the installation progress on
the screen.
o If you chose Install without restart, the plugins will be installed immediately. If you chose to
install after a restart, you will need to restart Jenkins for the changes to take effect.
Verify Installation:
o After installation, you can verify that the plugins have been installed by going to the Installed
tab in the Manage Plugins section.
Restart Jenkins:
o If you selected Download now and install after restart, you will need to restart Jenkins to complete
the installation process.
o Go to Manage Jenkins and click Restart Jenkins. Alternatively, you can restart Jenkins from the
command line or service management interface, depending on your operating system.
o Navigate to Manage Jenkins and click Configure System or find specific configuration pages for
plugins, which are often listed in the Manage Jenkins menu.
o Depending on the plugin, you may need to enter settings such as API keys, server URLs,
credentials, or other configuration parameters.
o Save your changes by clicking Save or Apply at the bottom of the configuration page.
Updating Plugins
1. Check for Updates:
o Click on the Updates tab to see if any installed plugins have available updates.
16
2. Install Updates:
o Click Download now and install after restart or Install without restart.
Uninstalling Plugins
1. Access Installed Plugins:
2. Uninstall Plugins:
o Find the plugin you want to uninstall and click Uninstall next to it.
Review Plugins: Periodically review the plugins installed in your Jenkins instance to ensure they are
still needed and actively maintained.
Backup Jenkins: Before installing or updating plugins, especially in a production environment, ensure
you have a recent backup of your Jenkins configuration and data.
By following these steps, you can effectively manage Jenkins plugins, enhancing your Jenkins setup to
better meet your needs and integrate with other tools and systems in your development and
deployment workflows.
an overview of some commonly used Jenkins plugins, including the Git Plugin, Parameterized Trigger
Plugin, HTML Publisher Plugin, Copy Artifact Plugin, and Extended Choice Parameter Plugin. This
guide will help you understand their purposes and how to use them effectively.
1. Git Plugin
Purpose: The Git Plugin integrates Jenkins with Git repositories. It enables Jenkins to pull code from
Git repositories and use it for building, testing, and deploying applications.
Key Features:
Source Code Management: Connects Jenkins jobs to Git repositories (e.g., GitHub, GitLab,
Bitbucket).
Configuration:
Source Code Management Section: In your Jenkins job configuration, go to the Source Code
Management section, select Git, and provide the repository URL.
Usage Example:
groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
Key Features:
Parameter Mapping: Map parameters from the current job to the triggered job.
Configuration:
Build Triggers Section: In your Jenkins job configuration, go to the Post-build Actions section
and select Trigger parameterized build on other projects.
Project to Build: Specify the job to trigger and define the parameters to pass.
18
Usage Example:
In the Post-build Actions section of Job A, configure it to trigger Job B and pass parameters like:
Key Features:
Customizable Report Paths: Specify where the HTML files are located in the workspace.
Configuration:
Post-build Actions Section: In your Jenkins job configuration, go to the Post-build Actions
section and select Publish HTML reports.
Report Directory: Define the directory where the HTML files are located.
Index Page: Specify the main HTML file to display (e.g., index.html).
Usage Example:
If your build generates a test report at target/site/index.html, configure the HTML Publisher Plugin
to publish it.
Key Features:
Artifact Copying: Copy build artifacts from other jobs to the current job’s workspace.
Build Parameterization: Copy artifacts based on build parameters or specific build numbers.
Configuration:
Build Steps Section: In your Jenkins job configuration, go to the Build section and add Copy
artifacts from another project.
Project to Copy From: Specify the job from which to copy artifacts.
19
Usage Example:
In Job B, add a build step to copy artifacts from Job A, specifying the artifact pattern (e.g.,
build/*.jar).
Key Features:
Extended Parameter Types: Supports parameters like multi-select lists, checkbox lists, and
dynamic choices.
Dynamic Parameters: Generate parameter options based on scripts or external data sources.
Configuration:
General Section: In your Jenkins job configuration, check This project is parameterized.
Add Parameter: Select Extended Choice Parameter and configure the parameter type (e.g., multi-
select list).
Choices Source: Define how choices are generated (e.g., script, file).
Usage Example:
Create a parameter of type Multi-line Text that allows users to select multiple options from a
dynamically generated list.
Summary
Git Plugin: Integrates Jenkins with Git repositories, allowing for source code management and
build triggering based on code changes.
Parameterized Trigger Plugin: Enables triggering of other Jenkins jobs with parameters,
facilitating complex build pipelines.
HTML Publisher Plugin: Publishes HTML reports and artifacts, making build results more
accessible and understandable.
Copy Artifact Plugin: Facilitates sharing of build artifacts between Jenkins jobs, enhancing
build workflows.
Extended Choice Parameter Plugin: Provides advanced parameter types for jobs, offering
greater flexibility in job configuration.
These plugins are commonly used to enhance Jenkins functionality, making it a powerful tool for
automating complex build and deployment processes.
20
7. Configuring Jenkins to work with java, Git and Maven
1. Install Jenkins
Download and Install Jenkins:
Install Plugins:
o Check the boxes next to these plugins and click Install without restart.
3. Configure JDK
Jenkins needs to know where your JDK is installed to compile Java projects.
2. Add JDK:
21
o You can either specify the path to the JDK installation or let Jenkins install it automatically.
For Manual Installation: Enter the path to the JDK installation directory.
For Automatic Installation: Check Install automatically and select the version of JDK
to install.
3. Save Configuration:
For Maven:
1. Install Maven:
o Download and install Maven from the Apache Maven website if it's not already installed.
o Specify the path to the Maven installation directory or let Jenkins install it automatically.
o Click Save.
For Gradle:
1. Install Gradle:
o Download and install Gradle from the Gradle website if it's not already installed.
o Specify the path to the Gradle installation directory or let Jenkins install it automatically.
o Click Save.
o Enter a name for the job and select Freestyle project or Pipeline depending on your needs.
22
o Click OK.
Select Git or Subversion and provide the repository URL and credentials.
o Build Triggers:
o Build Environment:
o Build Steps:
For Maven:
Click Add build step and select Invoke top-level Maven targets.
For Gradle:
o Post-build Actions:
For Maven, you might add a post-build action to publish JUnit test results by
specifying the path to the test result files.
o Configure environment variables if needed for your build process (e.g., setting JAVA_HOME).
23
Build Tools Configuration:
o Ensure that build tools are correctly configured in Jenkins and that paths are correctly set.
o Check the build history and logs on the job page to review the results of your builds.
Test Reports:
o Review test reports and other artifacts that were published during the build process.
Summary
Configuring Jenkins to work with Java involves installing and configuring essential plugins, setting up
JDK and build tools like Maven or Gradle, and creating Jenkins jobs that use these tools to build, test,
and package Java applications. By following these steps, you can create a robust Jenkins setup tailored
to Java development, enabling efficient automation of your build and deployment processes.
1. Install Jenkins
1. Download Jenkins:
o Visit the Jenkins download page and download the installer for your operating system.
2. Install Jenkins:
o Follow the installation instructions for your OS. After installation, Jenkins will be
accessible via a web interface (typically at http://localhost:8080).
o Check the box next to Git Plugin and click Install without restart.
24
3. Restart Jenkins (if necessary):
o If Jenkins does not automatically detect Git, click Add Git to add a Git installation.
o Provide a name for the Git installation if you are specifying a manual path.
o Specify the path to the Git executable if it is not automatically detected. You can
usually find Git at /usr/bin/git on Linux or C:\Program Files\Git\bin\git.exe on Windows.
3. Save Configuration:
o Click OK.
25
Enter the required details and click Add.
o Branch Specifier:
In the Branches to build section, specify the branch to build (e.g., main, develop, or a
specific branch like feature-branch).
Poll SCM: Use cron syntax to periodically check for changes (e.g., H/15
* * * *).
Define the build steps required for your project. For example:
For Maven: Add Invoke top-level Maven targets and specify goals like clean
install.
For Gradle: Add Invoke Gradle script and specify tasks like clean build.
Archive Artifacts: To save build outputs like JAR files, click Add post-
build action and select Archive the artifacts.
Publish Test Results: Add Publish JUnit test result report to display test
results.
26
5. Set Up Git Webhooks (Optional)
For automatic build triggering on code changes:
2. Add Webhook:
o In GitHub:
o For GitLab or Bitbucket, follow similar steps to add a webhook pointing to Jenkins.
o Check the build history and logs on the job page to review build results.
2. Build Notifications:
Summary
Configuring Jenkins to work with Git involves:
Creating and configuring Jenkins jobs with Git as the source code management system.
This setup will enable Jenkins to pull code from Git repositories, build projects, and integrate with
other tools in your CI/CD pipeline.
27
1. Install Jenkins
Download and Install Jenkins:
o Visit the Jenkins download page and download the installer suitable for your operating system.
o Open Jenkins and navigate to Manage Jenkins from the Jenkins dashboard.
o Check the box next to Maven Integration Plugin and click Install without restart.
o Download Maven from the Apache Maven website if it’s not already installed on your system.
On Windows: Extract the Maven archive and set the M2_HOME environment variable
to the Maven installation directory. Add M2_HOME/bin to your PATH environment
variable.
On Linux/Mac: Extract the Maven archive and set M2_HOME and PATH in your shell
profile (e.g., .bashrc or .zshrc).
o Add Maven:
Specify the path to the Maven installation directory or check Install automatically to let
Jenkins manage the Maven installation.
28
If you chose Install automatically, select the desired Maven version from the drop-down
menu.
o Select Freestyle project or Pipeline based on your preference and click OK.
Poll SCM: Use cron syntax to periodically check for changes (e.g., H/15 * * *
*).
GitHub hook trigger for GITScm polling: Set up a webhook in your GitHub
repository to trigger builds on code changes.
Click Add build step and select Invoke top-level Maven targets.
Choose the Maven version you configured earlier from the drop-down menu.
Enter the Maven goals to execute (e.g., clean install or clean package).
29
Example Maven Goals:
Shell
clean install
1. Additional Options:
You can configure additional options such as Maven options, properties, and
more.
Archive Artifacts: To save build outputs like JAR files, click Add post-build
action and select Archive the artifacts.
Publish Test Results: If you have test results, you can publish them using the
Publish JUnit test result report option.
30
2. Use Maven Settings in Jenkins Jobs:
o In Freestyle Projects:
In the Build section, under Invoke top-level Maven targets, you can select the custom
settings from the Settings drop-down.
o In Pipeline Jobs:
Use the withMaven block in your Jenkinsfile to specify the custom settings file.
Groovy
pipeline {
agent any
tools {
maven 'Maven 3.8.6' // Name of the Maven installation configured in Global Tool Configuration
stages {
stage('Build') {
steps {
o Review the build history and logs on the job page to monitor build results and diagnose
any issues.
2. Build Notifications:
o Configure notifications if needed to alert you about build successes or failures. You can
use plugins like Email Extension Plugin for email notifications.
31
Summary
Configuring Jenkins to work with Maven involves:
Creating and configuring Jenkins jobs to use Maven for building Java projects.
With these steps, Jenkins can efficiently manage your Maven-based builds, automating tasks such as
compiling, testing, and packaging your Java projects.
Creating a Jenkins build and configuring a Jenkins workspace involves setting up a Jenkins job to
automate the build process and understanding how Jenkins uses workspaces to manage files and
artifacts during builds. Here’s a detailed guide on how to create a Jenkins build and manage Jenkins
workspaces:
1. Open Jenkins in your web browser, typically at http://localhost:8080 or your Jenkins server URL.
2. Log in if required.
o Item Name: Enter a name for your new job (e.g., MyApp-Build).
Freestyle project: A simple and flexible way to configure basic build tasks.
3. Click OK to proceed.
2. Branch Specifier:
Poll SCM: Use cron syntax to periodically check for changes (e.g., H/15 * * * *).
GitHub hook trigger for GITScm polling: Configure a webhook in your Git
repository to trigger builds on code changes.
Click Add build step and choose from available options like:
Invoke top-level Maven targets: For Maven builds (e.g., clean install).
shell
Define the build process in a Jenkinsfile which can be stored in your repository. The file contains
stages and steps written in Groovy syntax.
Example Jenkinsfile:
Groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
33
sh 'mvn clean install'
o Archive Artifacts: To save build outputs (e.g., JAR files), click Add post-build action and
select Archive the artifacts.
o Publish Test Results: If you have test results, you can publish them using the Publish
JUnit test result report option.
2. Save Configuration:
1. Build Now:
o You can monitor the build process and view the console output.
Jenkins Workspace: Each Jenkins job has its own workspace directory where files are checked out
from the source code repository and where build artifacts are generated.
Location: Workspaces are usually located on the Jenkins server filesystem under the directory
specified by Jenkins (e.g., /var/jenkins_home/workspace).
Managing Workspace
1. View Workspace:
o Click on Workspace in the left sidebar to view the files in the workspace.
34
2. Clean Workspace:
o If you need to clean up workspace files between builds, you can use the Delete workspace before
build starts option in the job configuration.
For Freestyle projects, this can be set up in the Build Environment section by checking
Delete workspace before build starts.
o Workspace Root Directory: If you need to configure the root directory for workspaces, this is
typically set up in Jenkins' global settings or as part of the Jenkins installation configuration.
o Custom Workspace Location: For advanced use cases, you can configure jobs to use custom
workspace directories using environment variables or script commands.
groovy
pipeline {
agent {
label 'my-agent'
environment {
WORKSPACE_DIR = '/path/to/custom/workspace'
stages {
stage('Checkout') {
steps {
dir(WORKSPACE_DIR) {
checkout scm
stage('Build') {
steps {
dir(WORKSPACE_DIR) {
}
35
}
Workspace Cleanup
o You can manually delete the workspace directory from the Jenkins server if needed, though this
is usually managed automatically by Jenkins.
Automate Cleanup:
o You can use Jenkins plugins like Workspace Cleanup Plugin to automatically clean up
workspaces based on build configurations.
Summary
Creating a Jenkins build and configuring a Jenkins workspace involves:
Creating a Jenkins Job: Set up a new job, configure source code management, build triggers, build
steps, and post-build actions.
Managing Jenkins Workspace: Understand where workspaces are located, manage and clean up
workspace files, and configure custom workspace directories if needed.
With this setup, Jenkins will automate your build process and manage workspace files effectively,
enabling continuous integration and delivery for your projects.
2 Marks:
1. Jenkins Master: The central server that manages jobs, schedules builds, and monitors their
execution. It also provides the web interface for configuration and monitoring.
2. Jenkins Agents (Slaves): Machines that execute the build tasks. They can run on different
platforms and report back to the master with build results.
3. Build Jobs: Configurations that define the tasks Jenkins performs, such as compiling code,
running tests, and deploying applications.
4. Plugins: Extend Jenkins' capabilities, allowing integration with various tools and services.
5. Workspace: A directory on the agent or master where Jenkins stores files for the build
process.
36
2. Describe the process of installing Jenkins on a Linux system.
Answer:
bash
2. Download Jenkins: Visit the Jenkins download page and get the .deb package for Debian-
based systems.
bash
wget https://get.jenkins.io/debian/jenkins_2.338.1_all.deb
bash
bash
5. Access Jenkins: Open a browser and go to http://localhost:8080. Follow the setup instructions to
complete the installation.
3. What is the purpose of plugins in Jenkins, and how do they enhance Jenkins
functionality?
Answer: Plugins in Jenkins extend its functionality by integrating with external tools, providing
additional features, or enhancing existing capabilities. They enable Jenkins to:
1. Integrate with Version Control Systems: Plugins like the Git Plugin enable Jenkins to pull
code from Git repositories.
2. Support Various Build Tools: Plugins such as Maven Integration Plugin allow Jenkins to
build projects using different build tools.
3. Customize Job Configuration: Plugins like Parameter Plugin let users add build parameters
to jobs.
4. Publish Reports: Plugins like HTML Publisher allow Jenkins to publish build reports and
documentation.
37
5. Automate Tasks: Plugins such as Copy Artifact automate the process of copying artifacts
between builds or jobs.
4. Explain the difference between a Freestyle project and a Pipeline job in Jenkins.
Answer:
1. Freestyle Project:
o Configuration: Uses a graphical interface to configure build steps, triggers, and post-
build actions.
2. Pipeline Job:
o Configuration: Uses a Jenkinsfile to define the build process as code, which allows for
complex build workflows.
o Version Control: The Jenkinsfile can be stored in source control alongside the
application code, enabling versioning of build definitions.
1. Install Git Plugin: Go to Manage Jenkins > Manage Plugins, search for Git Plugin, and install it.
2. Create a New Job: Go to the Jenkins dashboard, click New Item, and create a new job
(Freestyle or Pipeline).
o Freestyle Project: In the job configuration, select Git under Source Code Management.
Enter the repository URL and credentials if needed.
o Pipeline Project: Use the checkout scm command in the Jenkinsfile to check out the Git
repository.
4. Add Build Triggers: Optionally, set up triggers like polling SCM or webhooks to
automatically start builds when changes are detected in the repository.
2. Enter Job Details: Provide a name for the job and select the type (Freestyle project, Pipeline,
etc.).
38
3. Configure Job:
o Build Triggers: Define triggers for starting builds (e.g., SCM polling, webhooks).
4. Save the Job: Click Save to store the configuration and create the job.
2. Find Plugin: In the Available tab, search for the plugin you want to add (e.g., Git Plugin).
3. Install Plugin: Check the box next to the plugin and click Install without restart or Download now
and install after restart.
5. Verify Installation: Go to Manage Jenkins > Manage Plugins > Installed tab to ensure the plugin is
listed.
39
11. How can you configure Jenkins to use Maven as a build tool?
Answer:
1. Install Maven Integration Plugin: Go to Manage Jenkins > Manage Plugins, search for Maven
Integration Plugin, and install it.
o Scroll to the Maven section and add a new Maven installation, either by specifying a
path or letting Jenkins install it automatically.
o Freestyle Project: Add a build step Invoke top-level Maven targets, select the Maven
installation, and specify goals (e.g., clean install).
o Pipeline Project: Use the mvn command in the Jenkinsfile to run Maven builds.
13. How can you configure a Jenkins job to use a specific JDK version?
Answer:
o In the JDK section, add a new JDK installation. Specify the path to the JDK installation
directory or let Jenkins install it automatically.
o In the job configuration, under the Build Environment section, select the configured JDK
from the JDK drop-down list.
14. Explain how to use the Extended Choice Parameter Plugin in Jenkins.
Answer: The Extended Choice Parameter Plugin allows you to create parameters with advanced
options in Jenkins jobs, such as:
Dynamic Choices: Provide a list of choices that can be generated from a script or external
source.
40
Use Cases: Useful for providing complex parameter options or when integrating with external
data sources.
To use it:
2. In the job configuration, add a parameter of type Extended Choice Parameter and configure its
options according to your needs.
15. What is the purpose of the Jenkins Git Plugin and how is it configured?
Answer: The Jenkins Git Plugin integrates Jenkins with Git repositories, allowing Jenkins to pull code
from Git repositories for builds. To configure it:
1. Install Git Plugin: Go to Manage Jenkins > Manage Plugins, search for Git Plugin, and install it.
2. Configure Job:
o Freestyle Project: In the job configuration, select Git under Source Code Management.
Enter the repository URL and credentials.
o Pipeline Project: Use checkout scm in the Jenkinsfile to pull the code from the Git
repository.
16. How can you set up a Jenkins job to build a Maven project?
Answer:
1. Install Maven Integration Plugin: Go to Manage Jenkins > Manage Plugins, search for Maven
Integration Plugin, and install it.
2. Configure Maven Installation: Go to Manage Jenkins > Global Tool Configuration, add a new
Maven installation, and configure its path.
o Freestyle Project: Add a build step Invoke top-level Maven targets, choose the Maven
version, and specify goals (e.g., clean package).
o Pipeline Project: Use Maven commands in the Jenkinsfile to run the build.
17. Describe how Jenkins handles build artifacts and how you can archive them.
Answer: Build artifacts are the files generated during the build process, such as JARs, WARs, or
reports. Jenkins can archive these artifacts to make them available after the build is completed.
1. Archive Artifacts:
o In the job configuration, go to the Post-build Actions section and select Archive the artifacts.
41
2. Access Artifacts: Archived artifacts are available under the Last Successful Artifacts or Last Failed
Artifacts sections on the job page.
18. What are the key differences between using a Jenkins Freestyle project and a
Jenkins Pipeline?
Answer:
Freestyle Project:
Pipeline:
19. How can you configure Jenkins to automatically trigger builds on code changes?
Answer:
1. SCM Polling:
o In the job configuration, under Build Triggers, check Poll SCM and specify a schedule
using cron syntax (e.g., H/15 * * * *).
2. Webhooks:
o For GitHub or GitLab, configure a webhook in the repository settings to notify Jenkins
of code changes. This can be done in the Build Triggers section by selecting GitHub hook
trigger for GITScm polling.
20. What is the purpose of the Parameter Plugin and how can you use it in a Jenkins
job?
Answer: The Parameter Plugin allows Jenkins jobs to accept parameters from users before starting a
build. This makes jobs more flexible and configurable.
1. Install Plugin: Install the Parameterized Trigger Plugin from Manage Jenkins > Manage Plugins.
2. Configure Parameters:
o Freestyle Project: In the job configuration, go to General and check This project is
parameterized. Add parameters like String Parameter, Choice Parameter, or Boolean Parameter.
42
o Pipeline Project: Define parameters in the Jenkinsfile using the parameters block.
Example Jenkinsfile:
groovy
pipeline {
agent any
parameters {
stages {
stage('Build') {
steps {
21. How do you set up Jenkins to use a specific JDK version for a job?
Answer:
o In the JDK section, add a new JDK installation and specify the path or let Jenkins install
it.
o Freestyle Project: In the job configuration, under Build Environment, select the desired
JDK from the JDK drop-down list.
o Pipeline Project: Use the tool command in the Jenkinsfile to specify the JDK.
Example Jenkinsfile:
groovy
pipeline {
agent any
tools {
jdk 'JDK 11' // Name of the JDK installation configured in Global Tool Configuration
}
43
stages {
stage('Build') {
steps {
sh 'javac -version'
}
}
}
}
1. Install Git Plugin: Go to Manage Jenkins > Manage Plugins, search for Git Plugin, and install it.
2. Configure Job:
o Freestyle Project: In the job configuration, select Git under Source Code Management.
Enter the repository URL and credentials if needed.
o Pipeline Project: Use checkout scm in the Jenkinsfile to check out the Git repository.
Example Jenkinsfile:
groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
checkout scm
}
}
}
}
24. How do you manage Jenkins plugins and ensure they are up to date?
Answer:
2. Check for Updates: In the Updates tab, review available plugin updates and select the ones to
update.
44
3. Install Updates: Click Download now and install after restart to update selected plugins. Jenkins
may need to restart to apply updates.
4. Manage Plugins: In the Installed tab, you can enable, disable, or uninstall plugins as needed.
25. Describe the process of creating a Jenkins Pipeline job using a Jenkinsfile.
Answer:
1. Create a New Job: From the Jenkins dashboard, click New Item and select Pipeline.
2. Configure Pipeline:
o Pipeline Definition: Under Pipeline, select Pipeline script or Pipeline script from SCM.
o Script: If using Pipeline script, enter the pipeline script directly. For Pipeline script from
SCM, provide the repository details and script path.
Example Jenkinsfile:
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
}
}
26. What is the purpose of the Copy Artifact Plugin and how can it be configured?
Answer: The Copy Artifact Plugin allows you to copy build artifacts from one Jenkins job to another.
This is useful for passing build outputs between jobs in a pipeline or multi-job workflow.
1. Install Plugin: Go to Manage Jenkins > Manage Plugins, search for Copy Artifact Plugin, and install it.
2. Configure Job:
o Freestyle Project: Add a build step Copy artifacts from another project. Configure the
source project and specify file patterns to copy.
Example Jenkinsfile:
groovy
pipeline
{
45
agent any
stages {
stage('Copy Artifacts') {
steps {
copyArtifacts('source-job-name')
1. Install Maven Integration Plugin: Go to Manage Jenkins > Manage Plugins, search for Maven
Integration Plugin, and install it.
o Add a new Maven installation, specifying the path or letting Jenkins install it
automatically.
o Freestyle Project: Add a build step Invoke top-level Maven targets, select the Maven
version, and specify goals (e.g., clean install).
28. Explain how to use Jenkins to build a Java project using Maven.
Answer:
3. Create a Job:
46
Example Jenkinsfile:
groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
29. How can you set up Jenkins to automatically clean workspaces before a build?
Answer:
1. Freestyle Project:
o In the job configuration, go to Build Environment and check Delete workspace before build
starts.
2. Pipeline Project:
Example Jenkinsfile:
groovy
pipeline {
agent any
stages {
stage('Clean') {
steps {
stage('Build') {
47
steps {
30. What is a Jenkins Pipeline and how does it differ from a Freestyle job?
Answer: A Jenkins Pipeline is a suite of plugins that support implementing and integrating continuous
delivery pipelines into Jenkins. It allows you to define complex build workflows and automate the
process using a Jenkinsfile, which is stored in source control alongside your application code.
Pipeline:
Freestyle Job:
These questions and answers cover the fundamentals and some advanced topics related to Jenkins,
providing a comprehensive understanding of its installation, configuration, and usage.
48