Search code examples
jenkinsjenkins-pipelinejenkins-build-flow

How to re-write a Jenkins DSL file as a Jenkins pipeline jenkinsfile?


I have the following Jenkins DSL file:

if (params["BUILD_SNAPSHOT"] == "true") {
    parallel(
        {
            build("company-main-build-snapshot")
        },
        {
            build("1-company-worker-build-snaphsot", WORKER_NAME: "sharding-worker")
        }
    )
}
parallel (
    {
        build("company-deployment-info",
            API_KEY: "aaaaa5dd4cd58b94215f9cddd4441c391b4ddde226ede98",
            APP: "company-Staging-App")
    },
    {
        build("company-salt-role-deploy",
                ENV: "staging",
                ROLE: "app")
    },
    {
        build("company-deployment-info",
            API_KEY: "aaaaa5dd4cd58b94215f9cddd4441c391b4ddde226ede98",
            APP: "company-Staging-Shardwork")
    },
    {
        build("company-salt-workers-deploy",
                ENVIRONMENT: "staging",
                WORKER_TYPE: "shardwork")
    }
)

if (params["REST_TEST"] == "true") {
    build("company_STAGING_python_rest_test")
}

My task is to convert/rewrite this workflow file content to Jenkins pipeline Jenkinsfile.

I have some example files for reference but I'm having a hard time understanding how I should even begin...

Can anyone please shed some light on this subject?


Solution

  • First, have a good look at Jenkins pipeline documentation, it is a great start and it is providing a whole bunch of information such as Build Parameters usage or parallel steps.

    Here are a few more hints for you to explore :

    Parameters

    Just use the parameter name as a variable such as :

    if (BUILD_SNAPSHOT) {
        ...
    }
    

    Call other jobs

    You can also use build step such as :

    build job: '1-company-worker-build-snaphsot', parameters: [stringParam(name: 'WORKER_NAME', value: "sharding-worker")]
    

    Use functions

    Instead of calling downstream jobs using build steps each time, you might want to consider using pipeline functions from another Groovy script, either from your current project or even from an external, checked out Groovy script.

    As an example, you could replace your second job call from :

    build("1-company-worker-build-snaphsot", WORKER_NAME: "sharding-worker")
    

    to :

    git 'http://urlToYourGit/projectContainingYourScript'
    pipeline = load 'global-functions.groovy'
    pipeline.buildSnapshot("sharding-worker")
    

    ...of course the init phase (Git checkout and pipeline loading) is only needed once before you can call all your external scripts functions.

    In short

    To sum it up a little bit, your code could be converted to something along these lines :

    node {
        git 'http://urlToYourGit/projectContainingYourScript'
        pipeline = load 'global-functions.groovy'
    
        if(BUILD_SNAPSHOT) {
            parallel (
                phase1: { pipeline.buildMainSnapshot() },
                phase2: { pipeline.buildWorkerSnapshot("sharding-worker") }
            )
        }
        parallel (
            phase1: { pipeline.phase1(params...) },
            phase2: { pipeline.phase2(params...) },
            phase3: { pipeline.phase3(params...) },
            phase4: { pipeline.phase4(params...) }
        )
        if (REST_TEST) {
            pipeline.finalStep()
        }
    }