Examples of Jenkins Jobs
Examples of Jenkins Jobs
1. Freestyle Project
Scenario: You have a simple Java application that needs to be compiled and tested.
Steps:
Scenario: You need a multi-stage build process that compiles, tests, and deploys a Node.js application.
Steps:
Scenario: You want to automatically create and manage pipelines for each branch in a Git repository.
Steps:
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
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:
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
// 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).
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.