nishio-dens / bitbucket-pullrequest-builder-plugin

Bitbucket Pull Request Builder Plugin for Jenkins
Other
125 stars 145 forks source link

can't get the plugin working with pipeline #205

Open tonytvo opened 5 years ago

tonytvo commented 5 years ago

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?

CodeMonk commented 5 years ago

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

Untitled

ejrgilbert commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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!

CodeMonk commented 5 years ago

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 notifications@github.com 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 commented 5 years ago

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

JasonDictos commented 4 years ago

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

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

CodeMonk commented 4 years ago

That merge squash was fixed several weeks ago.

Are you still having issues?

RayOei commented 4 years ago

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

aetos382 commented 4 years ago

I have the same problem. Plugin version 1.5.0

How should I set up the followings?

CodeMonk commented 4 years ago
  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 commented 4 years ago

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