bitbucket-pullrequest-builder-plugin icon indicating copy to clipboard operation
bitbucket-pullrequest-builder-plugin copied to clipboard

can't get the plugin working with pipeline

Open tonytvo opened this issue 5 years ago • 12 comments

Hi,

for some reason, I couldn't get the pipeline working with bitbucketpr, here's my pipeline

pipeline {
    agent any 

    triggers {
        bitbucketpr(projectPath:'',
            bitbucketServer:'https://git.xxx.com',
            cron:'* * * * *',
            credentialsId:'xxxx',
            username:'',
            password:'',
            repositoryOwner:'xxxOwner',
            repositoryName:'xxxRepository',
            branchesFilter:'s:r:^finr',
            branchesFilterBySCMIncludes:false,
            ciKey:'jenkins',
            ciName:'Jenkins',
            ciSkipPhrases:'',
            checkDestinationCommit:false,
            approveIfSuccess:true,
            cancelOutdatedJobs:true,
            commentTrigger:'test this please')
    }

    stages {
        stage('Build') { 
            steps {
                echo "build"
            }
        }
        stage('Test') { 
            steps {
                echo "Test" 
            }
        }
    }
}

Although it seems to work for me when I did it through freestyle jobs, is there any recommendation?

Also, how can I debug this on my jenkins? one way I could see is to checkout this project, build it and upload the hci manually to my jenkins and test it out, is there any gotchas I need to watch out for?

tonytvo avatar Jul 13 '19 18:07 tonytvo

I've never tried pipeline like that. I have always configured the trigger via the GUI:

Untitled

CodeMonk avatar Jul 18 '19 16:07 CodeMonk

@CodeMonk What version of the plugin are you using? I'm on 1.4.30 and am unable to get it to work with a SCM'ed pipeline. I have the trigger configured through the GUI and use the Git portion of the Source Code Management config to set up the Git repository to pull from.

However, the sourceBranch and targetBranch variables are not defined.

Possibly related to #130 ?

ejrgilbert avatar Aug 02 '19 17:08 ejrgilbert

After looking a bit more, it seems the injectedEnvVars.txt isn't getting generated in any of the builds. And the console output for the Jenkins jobs don't have the line [EnvInject] - Loading node environment variables. This would make sense for why the environment variables aren't getting defined.

So it seems that the BitbucketAdditionalParameterEnvironmentContributor isn't getting triggered for some reason for pipeline builds? Hopefully this helps a bit.

ejrgilbert avatar Aug 14 '19 13:08 ejrgilbert

Finally figured this out! I had to add the trigger via the Jenkins UI, then configure the job to checkout the pipeline from SCM via the UI, then add a checkout stage inside the pipeline to specify what to checkout which looks like this. Note, the types of quotation marks you use is important:

checkout([
    $class: 'GitSCM',
    branches: [[name: "*/${env.sourceBranch}"]],
    doGenerateSubmoduleConfigurations: false,
    extensions: [
        [
            $class: 'PreBuildMerge'],
            options: [mergeRemote: 'origin', mergeTarget: "${env.targetBranch}"]
    ],
    submoduleCfg: [],
    userRemoteConfigs: [[
        credentialsId: 'place-cred-id-here',
        refspec: '+refs/pull-requests/*: refs/remotes/origin/pr/*',
        url: "ssh://git@bitbucket-url-here:7999/${env.destinationRepositoryOwner}/${env.destinationRepositoryName}.git"
    ]]
])

The refspec pulls in the pr branches. This is important if your PRs are across forks!

ejrgilbert avatar Aug 27 '19 13:08 ejrgilbert

Awesome - great find!

But, is there a way we could make things easier from the plugin side? Is there a way to give Jenkins enough information to have checkout scm simply work?

-Dave

On Tue, Aug 27, 2019 at 7:42 AM ejrgilbert [email protected] wrote:

Finally figured this out! I had to add the trigger via the Jenkins UI, then configure the job to checkout the pipeline from SCM via the UI, then add a checkout stage inside the pipeline to specify what to checkout which looks like this. Note, the types of quotation marks you use is important:

checkout([ $class: 'GitSCM', branches: [[name: "/${env.sourceBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [ [ $class: 'PreBuildMerge'], options: [mergeRemote: 'origin', mergeTarget: "${env.targetBranch}"] ], submoduleCfg: [], userRemoteConfigs: [[ credentialsId: 'place-cred-id-here', refspec: '+refs/pull-requests/: refs/remotes/origin/pr/*', url: "ssh://git@bitbucket-url-here:7999/${env.destinationRepositoryOwner}/${env.destinationRepositoryName}.git" ]] ])

The refspec pulls in the pr branches. This is important if your PRs are across forks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nishio-dens/bitbucket-pullrequest-builder-plugin/issues/205?email_source=notifications&email_token=AAA72VQBCB4OEII3SW6WRSLQGUVKZA5CNFSM4IDAERX2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5HYY3Y#issuecomment-525306991, or mute the thread https://github.com/notifications/unsubscribe-auth/AAA72VUQPKBSP4XWLRFAUK3QGUVKZANCNFSM4IDAERXQ .

CodeMonk avatar Aug 28 '19 19:08 CodeMonk

Hey, @tonytvo , I think the reason it's been broken is because of a merge squash.

Check out #211

Are you able to build and test a plugin? I'm going to hand test it when I have some time at work, but, it would be really helpful if you could test too.

It may turn the above back into checkout scm

CodeMonk avatar Aug 28 '19 20:08 CodeMonk

This seems related to an issue I posted today: #215

Any other way to get this to work then the above mentioned work around?

JasonDictos avatar Dec 23 '19 22:12 JasonDictos

That merge squash was fixed several weeks ago.

Are you still having issues?

CodeMonk avatar Dec 30 '19 18:12 CodeMonk

I seem to have the same issue. Plugin version 1.5.0 As you can see this is triggered by the PR. But the subsequent SCM definition for the "Pipeline script from SCM" doesn't recognize the environment variable. The build.xml in the Jenkins job directory does contain the 'sourceBranch' variable as far as I can make out.

Any idea?

<a href="https://bitbucket.org/xxxxx/xxxx/pull-request/43">#43 xx</a> hudson.plugins.git.GitException: Command "git fetch --tags --progress --prune origin +refs/heads/${sourceBranch}:refs/remotes/origin/${sourceBranch}" returned status code 128: stdout: stderr: fatal: Couldn't find remote ref refs/heads/${sourceBranch} fatal: The remote end hung up unexpectedly at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2429) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2043) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:80) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:568) at jenkins.plugins.git.GitSCMFileSystem$BuilderImpl.build(GitSCMFileSystem.java:352) at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:197) at jenkins.scm.api.SCMFileSystem.of(SCMFileSystem.java:173) at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:115) at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:69) at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:299) at hudson.model.ResourceController.execute(ResourceController.java:97) at hudson.model.Executor.run(Executor.java:429) Finished: FAILURE

RayOei avatar Feb 27 '20 23:02 RayOei

I have the same problem. Plugin version 1.5.0

How should I set up the followings?

  • Jenkins pipeline job configuration GUI
  • trigger definition in Jenkinsfile
  • checkout step in Jenkinsfile

aetos382 avatar Mar 18 '20 06:03 aetos382

  1. I use the gui and pipeline build
  2. The trigger definition is identical to the documentation: builder configured with creds, and repo name and account set correctly. Then, I use a jenkinsfile from the SCM.
  3. Finally, the checkout step I use in my Jenkinsfile is: checkout scm

There used to be issues with checkout scm, but, those were fixed in 1.5.0

CodeMonk avatar Mar 25 '20 16:03 CodeMonk

Keep in mind, you CANNOT click the rebuild button on a PR build. The branch information will not be present. You have to either push a new commit, rebase a new commit, or comment on the PR with the trigger phrase

CodeMonk avatar Mar 25 '20 16:03 CodeMonk