Issues with multibranch pipeline job DSL - jenkins

I am having issues with multibranch pipeline for job DSL plugin to automate the creation of multibranch pipeline job.
The piece am having issues with is how to let set the path to the Jenkinsfile on the repo. I have looked online for documentation but found nothing to help. I have even tried to get example scripts but multibranch job DSL scripts are rare on the internet. Matter of fact could not find any that has Jenkinsfile set in it
jobs.groovy
folderName = "${JENKINS_PATH}"
folder(folderName)
multibranchPipelineJob("${folderName}/jenkins_multibranch_devops") {
branchSources {
git {
remote("https://gitlab.com/${REPO_PATH}")
credentialsId('gitlab_credentials')
includes('*')
}
}
configure { project ->
project / factory {
scriptPath('jenkins/Jenkinsfile')
}
}
orphanedItemStrategy {
discardOldItems {
numToKeep(14)
}
}
}
Here is what i have and its failing because i am obviously missing some stuffs which is why am looking for help
What am i missing and where can i get documentation if i plan on adding more and more to this jobs.groovy file and want to know how to know what stuffs to add because current doc page doesn't help at all

You can set it using this:
multibranchPipelineJob("${folderName}/jenkins_multibranch_devops") {
branchSources {
git {
remote("https://gitlab.com/${REPO_PATH}")
credentialsId('gitlab_credentials')
includes('*')
}
}
factory {
workflowBranchProjectFactory {
scriptPath('jenkins/Jenkinsfile')
}
}
orphanedItemStrategy {
discardOldItems {
numToKeep(14)
}
}
}
Documentation is available through the Job DSL API viewer in your jenkins installation: https://{your-jenkins}/plugin/job-dsl/api-viewer/index.html

Related

Job DSL plugin | Shared Library | Pipeline jobs | Github Hook not working

Please bear with me the description might be long but it might give a clean picture of the intent and issue.
I have used Job DSL Plugin to create a seeder job, which in turns creates two new Jobs. I have 2 separate repositories
For maintaining jenkins pipeline scripts.
For actual code to build.
First I have created a pipeline job in jenkins which in turns creates view and 2 jobs. Config shown below:
The Jenkinsfile given below uses Job DSL plugin api, reads the groovy script and creates the required 2 jobs.
node('master') {
checkout scm
jobDsl targets: ['dsl/seedJobBuilder.groovy'].join('\n'),
removedJobAction: 'IGNORE',
removedViewAction: 'IGNORE',
lookupStrategy: 'SEED_JOB'
}
seedJobBuilder.groovy creates a dsl pipeline job whose task would be to build the actual codebase.
listView('Build Pipelines') {
description('All build and deploy jobs')
jobs {
names(
'build',
'deploy',
)
}
columns {
status()
weather()
name()
lastSuccess()
lastFailure()
lastDuration()
buildButton()
}
}
def buildCommerce = pipelineJob('build') {
properties {
githubProjectUrl("${projectRepo}") // url of actual code repo not the jenkins script repo
}
definition {
cpsScm {
scm {
git {
remote {
url("${pipelineRepo}") // jenkins script repo url
credentials("somecredentials")
}
branch('${JENKINS_SCRIPT_BRANCH}')
}
scriptPath('pipelines/pipelineBuildEveryDay.groovy')
lightweight(false)
}
}
}
triggers {
githubPush()
}
}
Config of the above job created by Job DSL:
This job reads the pipelineBuildEveryDay groovy script, checkout the actual codebase and build and deploy.
The place where I am struggling is how do we trigger build on this second job through github hook or through ghprb. Since I don't want to manipulate manually the second job and the git url of the job is the script repo URL not the codebase URL. Is it possible to do this even? If yes what am I missing?
I have the webhook configured
pipelineBuildEveryDay.groovy
pipeline {
libraries {
lib("shared-library#${params.JENKINS_SCRIPT_BRANCH}")
}
agent {
node {
label 'master'
}
}
options {
skipDefaultCheckout(true) // No more 'Declarative: Checkout' stage
}
stages {
stage('Crazy Build Pipeline') {
tools {
jdk 'java11'
}
stages {
stage('Prepare build name') {
steps{
script{
currentBuild.displayName = "${currentBuild.number}-build"
}
}
}
stage('Checkout') {
steps {
cleanWs()
script {
checkoutRepository("${projectDir}", "${params.PROJECT_TAG}", "${params.PROJECT_REPO}")
}
}
}
stage('Run Tests') {
steps {
echo "Running test coming soon..."
}
}
}
}
}
// post build actions
post {
success {
echo "success"
}
failure {
echo "failure"
}
}
}
Well the suffering comes to an end. Posting this answer for anyone struggling with similar sort of issues.
Make sure you uncheck all other types of trigger, the only checked one should be pull request builder.
The part which screwed me was the Project URL. In my case in SCM part the github url was of the Jenkins-scripts repository URL not the URL of the codebase I want to build. So I tried to use my codebase repository URL in Github Project URL textbox.
But the real problem was using repository URL in the format 'https://code-base-repo-url.git' instead it should be 'https://code-base-repo-url'. Sounds stupid? Yeah I know!
Finally the complete Job config pipeline script if it helps:
def pipelineRepo = 'https://jenkins-script-repo'
def projectRepo = 'https://code-base-repo-url'
def projectTag = '${GIT_BRANCH}'
def buildCommerce = pipelineJob('build') {
properties {
githubProjectUrl("${projectRepo}")
}
definition {
cpsScm {
scm {
git {
remote {
url("${pipelineRepo}")
credentials("use-your-own-user-pass-cred")
}
branch('${JENKINS_SCRIPT_BRANCH}')
}
scriptPath('pipelines/pipelineBuildEveryDay.groovy')
lightweight(false)
}
}
}
triggers {
githubPullRequest {
admin('use_your_own_admin')
triggerPhrase('build please')
useGitHubHooks()
permitAll()
displayBuildErrorsOnDownstreamBuilds()
extensions {
commitStatus {
context('Jenkins')
completedStatus('SUCCESS', 'All is well')
completedStatus('FAILURE', 'Something went wrong. Investigate!')
completedStatus('ERROR', 'Something went really wrong. Investigate!')
}
}
}
}
}

Multibranch Pipeline job configuration with logrotator using DSL

I'm using jenkins job dsl to configure my multibranch pipeline jobs. Actually my all settings are working except logRotator. My aim is to delete the old builds and keep a particular number of build. I can use
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
}
in freestyle job for this purpose. The multibranch pipeline job configure section not having discardold build section as an option in UI. Is there any way I can use the logRotator without adding this to my jenkins file.
I've added the following section in my code to implement the buildDiscarder functionality in multibranch pipeline jobs.
multibranchPipelineJob("job") {
branchSources {
branchSource {
source {
bitbucket {
credentialsId("myid")
repoOwner("iam")
repository("job")
traits {
headWildcardFilter {
includes("branchestoinclude")
excludes("toexclude")
}
}
}
}
strategy {
defaultBranchPropertyStrategy {
props {
buildRetentionBranchProperty {
buildDiscarder {
logRotator {
daysToKeepStr("-1")
numToKeepStr("8")
artifactDaysToKeepStr("-1")
artifactNumToKeepStr("-1")
}
}
}
}
}
}
}
}
In Jenkins job dsl, the multibranchPipelineJob has an option to add the following lines for discarding old builds.
orphanedItemStrategy {
discardOldItems { numToKeep(10) }
}

Jenkins dsl configure block makes duplicate tabs

I'm trying to create a job DSL which creates a multibranch-pipeline job,
The job is being created successfully but there are some missing configurations in the multi-pipeline job so I tried to use the "configure" block.
The configure block indeed was created but it created a duplicate "tag" of jenkins.branch.BranchSource I guess I am missing something' I tried tons of manipulations but nothing worked for me.
Any advice?
This is my groovy DSL:
multibranchPipelineJob('TestDocker_pipeline_DSL') {
branchSources {
git {
remote(gitUrl)
credentialsId('Dev_Builder_ssh')
//includes("(V[0-9]+.[0-9]+([.]+[0-9]+)*)|(master)")
}
configure {
it / sources / data / "jenkins.branch.BranchSource" << "jenkins.plugins.git.GitSCMSource" {
id("8fd33e1d-07b6-4cc4-8f1c-a18d955b4b6e")
remote(gitUrl)
credentialsId('Dev_Builder_ssh')
traits{
"jenkins.scm.impl.trait.RegexSCMHeadFilterTrait"{
regex("V[0-9]+.[0-9]+([.]+[0-9]+)*)|(master)")
}
}
}
}
}
factory {
workflowBranchProjectFactory {
scriptPath('main/Docker/DockerJenkinsfileSlave.groovy')
}
}
orphanedItemStrategy {
discardOldItems {
numToKeep(3)
}
}
}
And this is the job XML being created:
Well After a lot of struggling I think that my problem was that I didn't define some of the TAGS as plugins in the groovy DSL and removing the "git" section also helped.
So the Final groovy that finally worked was this one:
branchSources {
configure {
it / sources / data / "jenkins.branch.BranchSource" << source (class: "jenkins.plugins.git.GitSCMSource", plugin:"git#3.9.2") {
remote(gitUrl)
credentialsId('Dev_Builder_ssh')
includes('*')
excludes('')
ignoreOnPushNotifications(false)
traits{
"jenkins.scm.impl.trait.RegexSCMHeadFilterTrait"{
regex("(V[0-9]+.[0-9]+([.]+[0-9]+)*)|(master)")
}
}
}
}
}
Which resulted this beutifull XML job:

Job DSL to create "Pipeline" type job

I have installed Pipeline Plugin which used to be called as Workflow Plugin earlier.
https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin
I want to know how can i use Job Dsl to create and configure a job which is of type Pipeline
You should use pipelineJob:
pipelineJob('job-name') {
definition {
cps {
script('logic-here')
sandbox()
}
}
}
You can define the logic by inlining it:
pipelineJob('job-name') {
definition {
cps {
script('''
pipeline {
agent any
stages {
stage('Stage 1') {
steps {
echo 'logic'
}
}
stage('Stage 2') {
steps {
echo 'logic'
}
}
}
}
}
'''.stripIndent())
sandbox()
}
}
}
or load it from a file located in workspace:
pipelineJob('job-name') {
definition {
cps {
script(readFileFromWorkspace('file-seedjob-in-workspace.jenkinsfile'))
sandbox()
}
}
}
Example:
Seed-job file structure:
jobs
\- productJob.groovy
logic
\- productPipeline.jenkinsfile
then productJob.groovy content:
pipelineJob('product-job') {
definition {
cps {
script(readFileFromWorkspace('logic/productPipeline.jenkinsfile'))
sandbox()
}
}
}
I believe this question is asking something how to use the Job DSL to create a pipeline job which references the Jenkinsfile for the project, and doesn't combine the job creation with the detailed step definitions as has been given in the answers to date. This makes sense: the Jenkins job creation and metadata configuration (description, triggers, etc) could belong to Jenkins admins, but the dev team should have control over what the job actually does.
#meallhour, is the below what you're after? (works as at Job DSL 1.64)
pipelineJob('DSL_Pipeline') {
def repo = 'https://github.com/path/to/your/repo.git'
triggers {
scm('H/5 * * * *')
}
description("Pipeline for $repo")
definition {
cpsScm {
scm {
git {
remote { url(repo) }
branches('master', '**/feature*')
scriptPath('misc/Jenkinsfile.v2')
extensions { } // required as otherwise it may try to tag the repo, which you may not want
}
// the single line below also works, but it
// only covers the 'master' branch and may not give you
// enough control.
// git(repo, 'master', { node -> node / 'extensions' << '' } )
}
}
}
}
Ref the Job DSL pipelineJob: https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob, and hack away at it on http://job-dsl.herokuapp.com/ to see the generated config.
This example worked for me. Here's another example based on what worked for me:
pipelineJob('Your App Pipeline') {
def repo = 'https://github.com/user/yourApp.git'
def sshRepo = 'git#git.company.com:user/yourApp.git'
description("Your App Pipeline")
keepDependencies(false)
properties{
githubProjectUrl (repo)
rebuild {
autoRebuild(false)
}
}
definition {
cpsScm {
scm {
git {
remote { url(sshRepo) }
branches('master')
scriptPath('Jenkinsfile')
extensions { } // required as otherwise it may try to tag the repo, which you may not want
}
}
}
}
If you build the pipeline first through the UI, you can use the config.xml file and the Jenkins documentation https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob to create your pipeline job.
In Job DSL, pipeline is still called workflow, see workflowJob.
The next Job DSL release will contain some enhancements for pipelines, e.g. JENKINS-32678.
First you need to install Job DSL plugin and then create a freestyle project in jenkins and select Process job DSLs from the dropdown in the build section.
Select Use the provided DSL script and provide following script.
pipelineJob('job-name') {
definition {
cps {
script('''
pipeline {
agent any
stages {
stage('Stage name 1') {
steps {
// your logic here
}
}
stage('Stage name 2') {
steps {
// your logic here
}
}
}
}
}
''')
}
}
}
Or you can create your job by pointing the jenkinsfile located in remote git repository.
pipelineJob("job-name") {
definition {
cpsScm {
scm {
git {
remote {
url("<REPO_URL>")
credentials("<CREDENTIAL_ID>")
}
branch('<BRANCH>')
}
}
scriptPath("<JENKINS_FILE_PATH>")
}
}
}
If you are using a git repo, add a file called Jenkinsfile at the root directory of your repo. This should contain your job dsl.

How to use Job DSL with Accurev SCM?

I am using the following groovy script to create a Job DSL that uses Accurev as SCM.
Please let me know how should the correct script look like.
job('payer-server') {
scm {
accurev {
/**What to insert here **/
}
}
triggers {
scm('H/15 * * * *')
}
steps {
maven {
goals('-e clean install')
mavenOpts('-Xms256m')
mavenOpts('-Xmx512m')
properties skipTests: true
mavenInstallation('Maven 3.3.3')
}
}
}
Currently there is no built-in support for Accurev SCM. Someone already filed a feature request as JENKINS-22138.
But you can use a Configure Block to generate the necessary config XML. There is an example for configuring Subversion, which can be adapted to Accurev.
job('example') {
configure { project ->
project.remove(project / scm) // remove the existing 'scm' element
project / scm(class: 'hudson.plugins.accurev.AccurevSCM') {
serverName('foo')
// ...
}
}
triggers {
// ...
}
steps {
// ...
}
}
Please leave a comment on the feature request to describe which options of Accurev SCM you need to configure initially.

Resources