0% found this document useful (0 votes)
25 views8 pages

Examples of Jenkins Jobs

Uploaded by

Arun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

Examples of Jenkins Jobs

Uploaded by

Arun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Examples of Jenkins Jobs

1. Freestyle Project

Scenario: You have a simple Java application that needs to be compiled and tested.

Steps:

1. Create Freestyle Project:


○ Go to Jenkins dashboard.
○ Click on "New Item".
○ Enter a name (e.g., "JavaAppBuild").
○ Select "Freestyle project" and click "OK".
2. Configure SCM:
○ In the "Source Code Management" section, select "Git".
○ Enter the repository URL (e.g.,
https://github.com/username/java-app.git).
○ Add credentials if necessary.
3. Add Build Step:
○ In the "Build" section, click "Add build step".
○ Select "Execute shell".

Enter the shell command:


mvn clean install

4. Add Post-build Action:


○ In the "Post-build Actions" section, click "Add post-build action".
○ Select "Archive the artifacts".
○ Enter target/*.jar to archive the built JAR files.
5. Save and Build:
○ Click "Save".
○ On the project page, click "Build Now" to run the job.
2. Pipeline

Scenario: You need a multi-stage build process that compiles, tests, and deploys a Node.js application.

Steps:

1. Create Pipeline Project:


○ Go to Jenkins dashboard.
○ Click on "New Item".
○ Enter a name (e.g., "NodeAppPipeline").
○ Select "Pipeline" and click "OK".
2. Define Pipeline Script:
○ In the "Pipeline" section, select "Pipeline script".

Enter the following script:


pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/username/node-app.git'
}
}
stage('Build') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
stage('Deploy') {
steps {
sh 'npm run deploy'
}
}
}
}

3. Save and Build:


○ Click "Save".
○ On the project page, click "Build Now" to run the pipeline.
3. Multibranch Pipeline

Scenario: You want to automatically create and manage pipelines for each branch in a Git repository.

Steps:

1. Create Multibranch Pipeline Project:


○ Go to Jenkins dashboard.
○ Click on "New Item".
○ Enter a name (e.g., "MultiBranchNodeApp").
○ Select "Multibranch Pipeline" and click "OK".
2. Configure Branch Sources:
○ In the "Branch Sources" section, click "Add source".
○ Select "Git".
○ Enter the repository URL (e.g., https://github.com/username/node-app.git).
○ Add credentials if necessary.
3. Define Pipeline Script:
○ Ensure that each branch contains a Jenkinsfile at the root of the repository.

Example Jenkinsfile content:


pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
stage('Deploy') {
steps {
sh 'npm run deploy'
}
}
}
}

4. Save and Scan Repository:


○ Click "Save".
○ Jenkins will scan the repository and create jobs for each branch.
4. Folder

Scenario: You have multiple projects for different teams and want to organize them into
folders.

Steps:

1. Create Folder:
○ Go to Jenkins dashboard.
○ Click on "New Item".
○ Enter a name (e.g., "TeamAProjects").
○ Select "Folder" and click "OK".
2. Create Jobs Inside the Folder:
○ Enter the newly created folder.
○ Click on "New Item".
○ Create jobs (Freestyle, Pipeline, Multibranch Pipeline, etc.) inside the
folder as needed.
3. Organize Other Folders and Jobs:
○ Repeat the process to create more folders and organize jobs under them.

Summary

● Freestyle Project: Simple build and test tasks.


● Pipeline: Multi-stage processes with complex workflows.
● Multibranch Pipeline: Automated pipelines for multiple branches.
● Folder: Organize and manage multiple jobs and projects.

By following these examples, you can leverage different types of Jenkins jobs to fit
various CI/CD requirements and organize your Jenkins environment effectively.
A daily-used CI/CD Jenkins job typically includes the basic steps to build, test,
and deploy an application. This job automates the entire process from code commit to
deployment, ensuring continuous integration and continuous delivery/deployment.
Here’s a step-by-step example of a basic Jenkins pipeline for a CI/CD process:

Example Jenkins Pipeline

Jenkinsfile
pipeline {
agent any

environment {
// Define any environment variables needed
BRANCH_NAME = 'main'
REPO_URL = 'https://github.com/your-repo/your-project.git'
}

stages {
stage('Checkout') {
steps {
// Checkout code from the version control system
git branch: "${BRANCH_NAME}", url: "${REPO_URL}"
}
}

stage('Build') {
steps {
// Run the build script (e.g., using Maven, Gradle, npm)
sh './build.sh' // or "mvn clean install" for a Maven
project
}
}

stage('Test') {
steps {
// Run the test script (e.g., unit tests, integration
tests)
sh './test.sh' // or "mvn test" for a Maven project
}
}
stage('Package') {
steps {
// Package the application (e.g., create a JAR/WAR file)
sh './package.sh' // or "mvn package" for a Maven project
}
}

stage('Deploy') {
steps {
// Deploy the application to the target environment (e.g.,
dev, staging, production)
sh './deploy.sh' // deployment script
}
}
}

post {
always {
// Archive the build artifacts
archiveArtifacts artifacts: '**/target/*.jar',
allowEmptyArchive: true

// Publish test results


junit 'reports/**/*.xml'

// Clean up workspace
cleanWs()
}
success {
// Send notification on success (e.g., via email, Slack)
echo 'Build and Deployment Successful'
}
failure {
// Send notification on failure (e.g., via email, Slack)
echo 'Build and Deployment Failed'
}
}
}
Breakdown of Pipeline Stages

1. Checkout:
○ Fetches the latest code from the specified branch of the Git repository.
2. Build:
○ Compiles the code and generates build artifacts. This can involve running
a build tool like Maven, Gradle, or npm.
3. Test:
○ Executes automated tests to verify the code's correctness. This typically
includes unit tests, integration tests, and other types of tests defined for
the project.
4. Package:
○ Packages the compiled code into a deployable format, such as a JAR or
WAR file.
5. Deploy:
○ Deploys the packaged application to a target environment, such as
development, staging, or production.

Post Actions

● Archive Artifacts: Stores the build artifacts in Jenkins for later retrieval.
● Publish Test Results: Generates and publishes test reports.
● Clean Workspace: Cleans up the workspace to ensure a fresh environment for
the next build.
● Notifications: Sends notifications based on the build outcome (success or
failure).

Common Plugins for CI/CD Pipelines

1. Git Plugin: Integrates Jenkins with Git repositories to fetch code.


2. Pipeline Plugin: Allows defining complex CI/CD pipelines using the Jenkinsfile.
3. JUnit Plugin: Publishes test results in JUnit format.
4. Artifact Deployer Plugin: Deploys build artifacts to remote locations.
5. Email Extension Plugin: Sends email notifications based on build status.
6. Slack Notification Plugin: Sends build notifications to Slack channels.
7. Docker Plugin: Integrates Jenkins with Docker for building and deploying
containerized applications.
Example Shell Scripts

build.sh
#!/bin/bash
# Example build script for a Java project using Maven
mvn clean install

test.sh
#!/bin/bash
# Example test script for a Java project using Maven
mvn test

package.sh
#!/bin/bash
# Example package script for a Java project using Maven
mvn package

deploy.sh
#!/bin/bash
# Example deploy script
# Deploy the application to a server
scp target/your-app.jar user@your-server:/path/to/deploy/
ssh user@your-server 'bash -s' < ./deploy_commands.sh

This basic CI/CD Jenkins job ensures that every code change goes through a
consistent process of building, testing, packaging, and deploying, thereby maintaining
code quality and reducing the chances of errors in production.

You might also like