Getting Started With Seed Job In Jenkins

What Is Seed Job

We are using Jenkins to automate our day to day things but creating jobs in Jenkins manually is kind of a manual intervention so to automate jobs creation we use Seed Job. A seed Job is a combination of Jenkins modules and groovy code.

Note: To use seed job do install the plugin name Job DSL

Creating FreeStyle Job Using Seed Job

Here is a sample seed job creating a freestyle job.

job("test_freestyle_seed_job"){
    steps {
        shell('echo Hello World!')
    }
}

or

freeStyleJob("test_freestyle_seed_job"){
    steps {
        shell('echo Hello World!')
    }
}

Creating Pipeline Job Using Seed Job

Here is a sample seed job creating a pipeline job.

pipelineJob('test_pipeline_seed_job') {
    definition {
        cpsScm {
            scm {
                git {
                    remote {
                        github('lyfofvipin/jenkins_tutorials')
                        // url('https://github.com/lyfofvipin/jenkins_tutorials.git')
                    }
                }
            }
            scriptPath('jobs/First_Jenkinsfile')
        }
    }
}

Creating Jobs Inside Folder

Here is a script creating both the jobs.

folder('test_folder') // This will create a folder first
job("test_folder/test_freestyle_job"){
    steps {
        shell('echo Hello World!')
    }
}

pipelineJob('test_folder/test_pipeline_job') {
    definition {
        cpsScm {
            scm {
                git {
                    remote {
                        url('https://github.com/lyfofvipin/jenkins_tutorials.git')
                    }
                    branches("master")
                }
            }
            scriptPath('jobs/First_Jenkinsfile')
        }
    }
}
pipelineJob('test_pipeline_job_with_script') {
    definition {
        cps{
            script("""
pipeline{
    agent 'any'
    stages{
        stage("Stage_Name"){
            steps{
                sh "echo Test Stage.; pwd; hostname"
            }
        }
    }
    post{
        success{
            cleanWs()
        }
    }
}
        """)
        }
    }
}