ghprb-plugin icon indicating copy to clipboard operation
ghprb-plugin copied to clipboard

GHPRB Job DSL for pipelines breaks with Job DSL >= 1.70

Open pch-envision opened this issue 6 years ago • 13 comments

GHPRB Job DSL for pipelines breaks with Job DSL >= 1.70

If I understand correctly, the GHPRB Job DSL example tracks the GitHub repo specified in the scm, git, remote context - see the example Job DSL below taken from the plugin site.

However, support for the scm{} context (and others) has been dropped for Pipeline Jobs in Job DSL 1.70 (see https://github.com/jenkinsci/job-dsl-plugin/wiki/Migration#migrating-to-170).

If scm{} is deprecated, it would seem that the remote for tracking must be specified some other way.

pipelineJob('example') {
    scm {
        git {
            remote {
                github('test-owner/test-project')
                refspec('+refs/pull/*:refs/remotes/origin/pr/*')
            }
            branch('${sha1}')
        }
    }
    triggers {
        githubPullRequest {
            admin('user_1')
            admins(['user_2', 'user_3'])
            userWhitelist('[email protected]')
            userWhitelist(['[email protected]', '[email protected]'])
            orgWhitelist('my_github_org')
            orgWhitelist(['your_github_org', 'another_org'])
            cron('H/5 * * * *')
            triggerPhrase('OK to test')
            onlyTriggerPhrase()
            useGitHubHooks()
            permitAll()
            autoCloseFailedPullRequests()
            allowMembersOfWhitelistedOrgsAsAdmin()
            extensions {
                commitStatus {
                    context('deploy to staging site')
                    triggeredStatus('starting deployment to staging site...')
                    startedStatus('deploying to staging site...')
                    statusUrl('http://mystatussite.com/prs')
                    completedStatus('SUCCESS', 'All is well')
                    completedStatus('FAILURE', 'Something went wrong. Investigate!')
                    completedStatus('PENDING', 'still in progress...')
                    completedStatus('ERROR', 'Something went really wrong. Investigate!')
                }
            }
        }
    }
}

pch-envision avatar Aug 29 '18 21:08 pch-envision

Is there a solution to this?

tushar-nallan avatar Oct 09 '18 12:10 tushar-nallan

@tushar-acharya I haven't checked recently, but I've been using an older (< 1.70 version) of Job DSL as a workaround until this is resolved.

pch-envision avatar Oct 09 '18 22:10 pch-envision

Deprecation in job dsl should not force you to use an older version, they will still work. What is the replacement from job-dsl?

bjoernhaeuser avatar Oct 21 '18 18:10 bjoernhaeuser

To be clear, scm does not appear to be deprecated for freestyle jobs. It is deprecated in Pipeline jobs. GHPRB is not designed for pipeline.

The DSL mentioned in the issue description is a freestyle job so it will continue to work.

It is nit clear how the Job DSL for GHPRB breaks. Can you elaborate?

samrocketman avatar Oct 21 '18 19:10 samrocketman

@samrocketman sorry, the example job was not a great example. I have modified it to make it a pipeline job. I use Job DSL to create Pipeline Jobs. I would like these jobs to be triggered by GHPRB; this works before Job DSL 1.70, but not after, since scm context is removed for pipeline jobs.

pch-envision avatar Oct 22 '18 01:10 pch-envision

Last I checked, the GitHub branch source is the preferred way to configure projects for pipelines using multibranch pipelines. That’s using plugins other than GHPRB. Have you considered switching to multibranch pipelines?

Other than that recommendation I’ll have to try out configuring a single pipeline job myself before I can comment further.

samrocketman avatar Oct 22 '18 04:10 samrocketman

Having just helped diagnose a problem updating a pipeline job to seed correctly with a newer version of the Job DSL plugin, I think what you want is pipelineJob -> definition -> cpsScm -> scm. I've only tested it with Job DSL 1.67 so far, but it looks like it should continue working past the deprecation of the top-level scm context you're worried about; all of those contexts are still present in the 1.70 documentation.

jmbowman avatar Nov 09 '18 15:11 jmbowman

A temporary hack is by modifying the xml from within the dsl specification with configure:

Closure addGitSCM(String gitUrl) {
    return {
        it / 'properties' / 'com.coravy.hudson.plugins.github.GithubProjectProperty'(plugin: '[email protected]') {
            projectUrl(gitUrl)
        }
    }
}

then use it as this:

pipelineJob(projectName) {
    description "Creates project for PR"
    configure addGitSCM(gitUrl) // this is a hack because below does not work
    // other steps
}

nautilussu avatar Dec 20 '18 10:12 nautilussu

@samrocketman

That’s using plugins other than GHPRB. Have you considered switching to multibranch pipelines?

We have considered this but the DSL for multibranchPipelineJob() is extremely limited https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.workflow.BranchSourcesContext.github

See the comment in configure {} block below. We can't see a way to forward without adding submodules to our cookbook repos and trying to checkout submodules. We'd rather not do this, we rather have a job DSL method to define a second repo for our pipeline scripts (pipelineJob() supported this in job DSL at/before 1.7.0)

New Way, doesn't support multi git repos:

// Generate PR push pipeline jobs for all of the repos passed in via REPO_LIST

def repoList = "${REPO_LIST}".trim().replaceAll('"', '').split(",")

println "Generating PR push jobs for the following repos:: $repoList"
repoList.each {
    def cookbook = it
    multibranchPipelineJob("chef-${cookbook}-PR-push") {
        description("Chef push pipeline generator")
        branchSources {
            github {
                buildOriginPRHead() // Build fork PRs (unmerged head).
                // buildOriginPRMerge() // Build fork PRs (merged with base branch).
                buildOriginBranchWithPR() // Build origin branches also filed as PRs.
                checkoutCredentialsId('fffff-fffff-fffff-fffff')
                // scanCredentialsId('github-ci')
                repoOwner('myOwner')
                repository("${cookbook}")
            }
        }
        configure {
            // my Jenkinsfile doesn't exist in the chef Cookbooks for 900 repositories... 
            // how can we set a seperate git repo for shared scripts? we don't want to use submodules
            it / factory(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory') {
                owner(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject', reference: '../..')
                scriptPath("jenkins/[where ever you want]/Jenkinsfile")
            }
        }
    }
 }

Here is the old way that worked until support was dropped in job dsl.

repoList.each {
  def cookbook = it
  pipelineJob("chef-${cookbook}-PR-push") {
    scm {
     git {
       remote {
         github("organization/${cookbook}", 'ssh')
         refspec('+refs/pull/*:refs/remotes/origin/pr/*')
         credentials('ffffff-fffff-fffff-ffff)
       }
       branch('${ghprbActualCommit}')
       extensions {
         relativeTargetDirectory("${cookbook}")
       }
       configure { gitScm ->
         gitScm / 'extensions' << 'hudson.plugins.git.extensions.impl.UserExclusion' {
           excludedUsers('mybotuser')
         }
       }
     }
    }
    triggers {
      githubPullRequest {
        admin('')
        orgWhitelist(['orgname'])
        triggerPhrase('jenkins push to \\S* \\S*')
        useGitHubHooks()
        onlyTriggerPhrase()
        extensions {
          commitStatus {
            context('Push cookbook pipeline')
            startedStatus()
            statusUrl()
          }
        }
      }
    }
    definition {
      cpsScm {
        lightweight(true)
        scriptPath('jenkins/[where ever you want]/Jenkinsfile')
        scm {
          git {
            branch('master')
            remote {
              github("organization/jenkins-tools", 'ssh')
              credentials('ffff-fff-fff-ffffff)
            }
          }
        }
      }
    }
  }
}

jondkelley avatar Jun 26 '19 23:06 jondkelley

Which support was dropped from job-dsl?

bjoernhaeuser avatar Jul 02 '19 19:07 bjoernhaeuser

i also notices that the example in the README.md is not working anymore.

for the scm part, you now have to use the "checkout".

example:

pipeline {
    agent {label "BuildNode"}

    stages {
        stage('cleanup') {
            steps {
                deleteDir()
            }
        }
        stage('fetch') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: 'refs/heads/master']],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'project']],
                    submoduleCfg: [],
                    userRemoteConfigs: [[
                        credentialsId: '$CREDENTIAL_UUID',

                        url: '[email protected]:Example/Repository.git'
                    ]]
                ])
            }
        }
    }
}

but i have no clue how to get the trigger working using the DLS, the stuff in the README.md trows syntax errors.

schlitzered avatar Dec 10 '20 15:12 schlitzered

This is still working with latest Job-DSL 1.77 and Jenkins 2.263.1 Inspired from https://github.com/jenkinsci/ghprb-plugin/issues/785

pipelineJob("example") { properties { githubProjectUrl("https://github.com/ebuzzing/test") triggers { githubPullRequest { triggerPhrase('retest.*') useGitHubHooks(true) permitAll(true) displayBuildErrorsOnDownstreamBuilds(true) cron('') extensions { commitStatus { context('Jenkins') } } } } } throttleConcurrentBuilds { maxPerNode(1) maxTotal(1) } logRotator { numToKeep(100) } definition { cps { script(readFileFromWorkspace('example.groovy')) } } }

github-daniel-stoian avatar Dec 17 '20 13:12 github-daniel-stoian

@danielstoian you still get a deprecation warning

nocode99 avatar Jan 12 '21 17:01 nocode99