Jenkins Job DSL for beginners

This post helps you to create a basic Jenkins job-dsl groovy file for a previously created readymade free-style Jenkins job. Follow previous post to set up a main DSL job to automate all the Jenkins jobs:

  • Create a Jenkins Job-DSL groovy file, your_jenkins_job_name.groovy 
freeStyleJob('your_jenkins_job_name') {
    description 'your project description'
    logRotator(numToKeep = 100)
    label('your_automation_fleet')
    scm {
       git {
        remote {
            url('git@github.com:your_username/your_repository.git')
            refspec('+refs/heads/*:refs/remotes/origin/*')
            
            # provide the credentials to let you clone git repo
            credentials('xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx')
        }
        branch('master')
    }
}

# insert multiple shell files to execute sequentially
steps {
  shell(readFileFromWorkspace('folder/jenkins_configure.sh'))
  shell(readFileFromWorkspace('folder/archive_reports.sh'))
}

# enable Jenkins Global passswords
wrappers {
    injectPasswords{ injectGlobalPasswords() }
    colorizeOutput()
    timestamps()
}

# publish a conslidated html report in the Jenkins dashboard
publishers {
   publishHtml {
    report('your_project/reports/') {
        reportName('Report Title')
        reportFiles('your_report_1.html, your_report_2.html, your_report_3.html')
        allowMissing()
        keepAll()
      }
    }
  }
}
  • The above Groovy script can also be generated by readymade through a Jenkins plugin, XML Job to Job DSL 
  • Go to Manage Jenkins -> Manage Plugins -> Available
  • Search for XML Job to Job DSL Plugin, install and restart Jenkins after download

  • Now, you will be finding an option to convert any of your Jenkins job to DSL
  • Click on the XML Job to DSL link, select the job to be converted, and click on the Convert selected to DSL button

  • Finally, download the DSL file

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s