0

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

  1. For maintaining jenkins pipeline scripts.
  2. 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:

enter image description here

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: enter image description here

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 enter image description here 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"
    }
}
}
SparkOn
  • 8,806
  • 4
  • 29
  • 34

1 Answers1

0

Well the suffering comes to an end. Posting this answer for anyone struggling with similar sort of issues.

  1. Make sure you uncheck all other types of trigger, the only checked one should be pull request builder.
  2. 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.

enter image description here

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!')
            }
        }
    }
}
}
SparkOn
  • 8,806
  • 4
  • 29
  • 34