Declarative Jenkinsfile for pullrequests.

157 views
Skip to first unread message

Kai Mildner

unread,
Sep 19, 2017, 5:37:01 AM9/19/17
to Jenkins Users

Hi,


i have a Jenkinsfile in my repository. On Jenkins i setted up a Multibranch Pipeline Project for the repository.

Everything is fine, it builds via Jenkinsfile for everey branch i create and triggers also on push etc.

Now i want to also build at pull requests. So the pull request is triggering the build.

The Problem is now that my checkout is not working for the pull request.


$class: 'GitSCM', branches: [[name: '${BRANCH_NAME}']], doGenerateSubmoduleConfigurations: false, extensions: [ [ $class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: true ], [ $class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false, recursiveSubmodules: true, reference: '', trackingSubmodules: false ], [ $class: 'CleanBeforeCheckout' ] ], submoduleCfg: [], userRemoteConfigs: [ [ credentialsId: 'xxxxxxxxxxxxxxxxxxxxxxx', url: 'my.git' ] ]


So now i have the problem that the branch name is "PR-[number]" when i create a pull request. So checking out that branch is not possible, because it not exists. I also tried it with 


branches: [[name: '**']]

But then nothing builds.

The git server is a local Bitbucket server.


What can i do to get also pullrequests Building?


Regards


Kai

Joshua Noble

unread,
Sep 19, 2017, 12:26:39 PM9/19/17
to Jenkins Users
Are you using scripted or declarative pipelines to do this? I highly suggest using the declarative format. What Git source control provider are you using? If you're using GitHub, ensure that you're using the GitHub Branch Source plugin too as it will automatically do the checkouts for you and find the proper refspecs for branches and PR's.

Kai Mildner

unread,
Sep 20, 2017, 2:40:06 AM9/20/17
to Jenkins Users
I'm using the declarative Format, maybe with scripted parts? I'm not sure. Here is a short example how i do the checkout.

pipeline{
    agent {label 'master'}
    triggers { pollSCM('H 0 1 1 0') }
    options{
        buildDiscarder(logRotator(numToKeepStr: '3'))
        timeout(time: 1, unit: 'HOURS')
        skipDefaultCheckout()
        retry(3)
    }
    environment{
        INSTALL_DIR='my/install/dir'
    }
    stages{
        stage('Checkout'){
            steps{
                script{
                    try{
                        cleanWs notFailBuild: true  
                        checkout(
                            [

                                $class: 'GitSCM',
                                branches: [[name: '${BRANCH_NAME}']],
                                doGenerateSubmoduleConfigurations: false,
                                extensions:
                                [
                                    [
                                        $class: 'CloneOption',
                                        depth: 0,
                                        noTags: false,
                                        reference: '',
                                        shallow: true
                                    ],
                                    [
                                        $class: 'SubmoduleOption',
                                        disableSubmodules: false,
                                        parentCredentials: false,
                                        recursiveSubmodules: true,
                                        reference: '',
                                        trackingSubmodules: false
                                    ],
                                    [
                                        $class: 'CleanBeforeCheckout'
                                    ]
                                ],
                                submoduleCfg: [],
                                userRemoteConfigs:
                                [
                                    [
                                        credentialsId: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
                                        url: 'my.git'
                                    ]
                                ]
                            ]
                        )
                    }catch(e){
                        String error = e.toString()
                        echo error
                        currentBuild.result = 'Failure'
                        emailext body: '${DEFAULT_CONTENT}', subject: '${DEFAULT_SUBJECT}', recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'UpstreamComitterRecipientProvider']]
                        throw e
                    }
                    finally{
                        cleanWs notFailBuild: true
                    }
                }
            }
        }
    }
}

The Git source Control Provider is a local Bitbucket Server. The plugin i used in the multibranch Pipeline Project on Jenkins is Bitbucket to get the build results and builds also on the bitbucket overviews.



Reply all
Reply to author
Forward
0 new messages