Convert scripted pipeline to declarative pipeline in Jenkins

34 views Asked by At

Can someone please help me in converting the following scripted pipeline to declarative pipeline in Jenkins

properties(
    [
        parameters([
                string(defaultValue: 'abc,def,ghi', name: 'NAMES')
        ])   
    ]
)

node {
    
def namesList = params.NAMES.tokenize(',')

namesList.each { name ->
    
        stage('build'){
            echo "build for ${name}"
        }
        stage('test'){
            echo "test for ${name}"
        }
        stage('deploy'){
            echo "deploy for ${name}"
        }
    
}
}

Thanks in advance

I tried seaching some online options for conversion but I am not getting any useful info. I need to use the declarative pipeline for jenkins and unable to convert existing script.

1

There are 1 answers

0
M B On

The docs should help you with this. Basically, we can take this code block from the doc and fit it to your needs:

pipeline {
    agent any
    parameters {
        string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
    }
    stages {
        stage('Example') {
            steps {
                echo "Hello ${params.PERSON}"
            }
        }
    }
}

Replace the parameters with the params you have and the stages with your scripted stages. You get something like this:

pipeline {
    agent any
    
    parameters {
        string(defaultValue: 'abc,def,ghi', description: '', name: 'NAMES')
    }
    
    stages {
        stage('Build') {
            steps {
                script {
                    def namesList = params.NAMES.tokenize(',')
                    
                    namesList.each { name ->
                        echo "build for ${name}"
                    }
                }
            }
        }
        stage('Test') {
            ...
        }
        ...
    }
}