ghprb-plugin
ghprb-plugin copied to clipboard
GHPRB Job DSL for pipelines breaks with Job DSL >= 1.70
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!')
}
}
}
}
}
Is there a solution to this?
@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.
Deprecation in job dsl should not force you to use an older version, they will still work. What is the replacement from job-dsl?
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 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.
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.
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.
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
}
@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)
}
}
}
}
}
}
}
Which support was dropped from job-dsl?
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.
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')) } } }
@danielstoian you still get a deprecation warning