Operations 6 min read

Migrating Jenkins Freestyle Jobs to Pipeline: A Step‑by‑Step Guide

This article explains why and how to replace Jenkins Freestyle projects with Pipeline jobs, covering the limitations of Freestyle, the benefits of Pipeline, and providing a complete scripted example that automates code checkout, building, testing, reporting, and email notification.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Migrating Jenkins Freestyle Jobs to Pipeline: A Step‑by‑Step Guide

Many organizations use Jenkins for continuous integration, testing, and deployment, often relying on Freestyle projects as the default job type, which have notable limitations such as single‑repository SCM polling and difficulty separating long‑running unit‑test stages.

To address these issues, the article describes the motivation for converting Freestyle jobs to Pipeline projects, allowing multiple repositories, parallel stages, and clearer visualisation of the build process.

The conversion includes configuring global settings, GitHub repository hooks, and shell‑based build steps, followed by a detailed Pipeline script that defines workspace paths, cleans previous builds, checks out multiple repositories, runs builds and unit tests, publishes HTML coverage reports, and sends email notifications.

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’s staged output is presented in an intuitive way, making it easy to follow the build progress, and the article concludes that the choice between Freestyle and Pipeline depends on project requirements, with Pipeline offering greater flexibility for complex CI/CD workflows.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdAutomationDevOpsbuildJenkins
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.