Migrating Jenkins Freestyle Jobs to Pipeline: A Step‑by‑Step Guide
This article explains why and how to convert Jenkins Freestyle projects to Pipeline jobs, covering trigger mechanisms, global and repository configurations, shell build steps, and a complete scripted pipeline example that automates building, testing, reporting, and email notifications.
Many companies use Jenkins for continuous integration, testing, and deployment, often relying on Freestyle projects which have limitations; the author describes migrating all Freestyle jobs to Pipeline projects to overcome these constraints.
Jobs are triggered when developers push updates to the repository, initiating a binary build followed by a unit‑test job that checks code coverage; because coverage testing is time‑consuming, the tasks are split into separate jobs.
The article outlines the global Jenkins configuration, GitHub repository settings, webhook activation, and shell‑based build steps, illustrated with screenshots.
It then explains the motivations for converting to Pipeline, notably the inability of Freestyle to poll multiple SCM repositories via webhook, and presents a full Pipeline script that consolidates the previously separate tasks.
WSPACE = '/var/jenkins/workspace/Directory_Name/'
BRWSPACE = '/var/jenkins/workspace/'
pipeline {
agent {
node {
label 'Node_Name'
customWorkspace "${WSPACE}"
}
}
//清空构建目录
stages {
stage('Cleaning up the previous directory') {
steps {
echo 'Deleteing the directory'
sh "rm -rf /var/jenkins/workspace/Directory_Name/* "
}
}
// 下载代码和依赖
stage('Checking out build repo and its dependencies') {
steps {
dir("${WSPACE}/RepoName") {
git branch: 'master', credentialsId: 'UserName', url: 'https://github.com/path/repo.git'
}
dir("${WSPACE}/dir") {
git branch: 'master', credentialsId: 'UserName', url: 'https://github.com/path/repo1.git'
}
dir("${WSPACE}/dir3") {
git branch: 'master', credentialsId: 'UserName2', url: 'https://github.com/path/repo4.git'
}
}
}
//执行构建
stage('Versioning and executing the build') {
steps {
dir("${WSPACE}/repo1") {
script {
sh label: '', script: '''/usr/bin/env
cd /var/jenkins/workspace/
original=`cat patch_info`
MAJOR=`cat patch_info | cut -d "." -f1`
MINOR=`cat patch_info | cut -d "." -f2`
PATCH=`cat patch_info | cut -d "." -f3`
New_Value=`expr $PATCH + 1`
New_Version=$MAJOR.$MINOR.$New_Value
sed -i "s/$original/$New_Version/g" patch_info
echo "$New_Version"
cd /var/jenkins/workspace/path/repo4/
echo "Starting the Unit Testing"
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
make format
make clean build
if make unit-test ; then
cd /var/jenkins/workspace/path/repo1/dir
else
cd /var/jenkins/workspace/path/repo2/dir2
fi
if make unit-test ; then
echo " unit testing completed"
fi
'''
}
}
}
}
//发布HTML报告
stage('Publish HTML Report') {
steps {
dir("/jenkins/workspace/") {
script {
sh label: '', script: '''/usr/bin/env
perl /jenkins/generate_build_meta_data.pl -jr http://gitlab.com:8080 -bNum ${BUILD_NUMBER} -bName ${JOB_NAME} -o /tmp -t /jenkins/template.html
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
cd /var/jenkins/workspace/path/repo1/service/
go tool cover -html=c.out -o coverage.html
cd /var/jenkins/workspace/path/repo2/dir3
go tool cover -html=c.out -o output.html
'''
publishHTML([allowMissing:false, alwaysLinkToLastBuild:false, keepAll:false, reportDir:'/tmp/${JOB_NAME}/${BUILD_NUMBER}', reportFiles:'${JOB_NAME}-${BUILD_NUMBER}-manifest.html', reportName:'Email Output Subject', reportTitles:''])
}
}
}
}
//发送邮件
stage('Send Email') {
steps {
dir("${WSPACE}/repo4") {
emailext attachmentsPattern: '**/coverage.html,**/dir4.html', body: '${FILE, path="/tmp/${JOB_NAME}/${BUILD_NUMBER}/${JOB_NAME}-${BUILD_NUMBER}-manifest.html"}', subject: 'Unit testing Email Output ${BUILD_NUMBER} Successful', to: "[email protected], [email protected]"
}
}
}
}
}The pipeline job’s staged output is presented in an easily understandable way, making the process transparent; the author concludes that choosing between Freestyle and Pipeline depends on specific needs, with Pipeline offering greater flexibility for customized CI/CD workflows.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
