4

I have a multi-branch declarative pipeline on Jenkins, which gets triggered by repo events from my Bitbucket cloud account and I'd like it to report the result back to Bitbucket.

My Jenkinsfile looks as follows:

pipeline {
    agent any
    stages {
        stage('Prepare Env') {
            steps {
                bitbucketStatusNotify(buildState: 'INPROGRESS')
                sh "npm i"
            }
            post {
                success {
                    bitbucketStatusNotify(buildState: 'SUCCESSFUL')
                }
                failure {
                    bitbucketStatusNotify(buildState: 'FAILED')
                }
            }
        }
    }
}

I discovered that the second and all consecutive commits to a pull request cause a condition when Jenkins build fails when notifying Bitbucket of the build result with a message: java.lang.Exception: None or multiple repos.

This is somehow coherent with one of the first lines in the log saying: Fetching changes from 2 remote Git repositories.

However, I actually only have one repository and no idea why Jenkins recognises it as multiple.

How I can make my build stop seeing multiple repos and succeed?

Full log follows:

Branch event
Checking out git https://bitbucket.org/mycompany/myproject.git https://bitbucket.org/mycompany/myproject.git into /var/lib/jenkins/workspace/myproject_PR-48-TV7ILAHCKATC6ZK26DHNTXI6KKLEGO3RSNDKAL2363W5GU5TABFQ@script to read Jenkinsfile
 > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from 2 remote Git repositories
 > /usr/bin/git config remote.origin.url https://bitbucket.org/mycompany/myproject.git # timeout=10
Fetching without tags
Fetching upstream changes from https://bitbucket.org/mycompany/myproject.git
 > /usr/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials Migrated by JIRA Plugin
 > /usr/bin/git fetch --no-tags --progress https://bitbucket.org/mycompany/myproject.git +refs/heads/VE-3869-dodawanie-edycja-i-usuwanie-aktu:refs/remotes/origin/PR-48
 > /usr/bin/git config remote.upstream.url https://bitbucket.org/mycompany/myproject.git # timeout=10
Fetching without tags
Fetching upstream changes from https://bitbucket.org/mycompany/myproject.git
using GIT_ASKPASS to set credentials Migrated by JIRA Plugin
 > /usr/bin/git fetch --no-tags --progress https://bitbucket.org/mycompany/myproject.git +refs/heads/master:refs/remotes/upstream/master
Merging remotes/upstream/master commit 98a73e74fed491c44dcb1b361a1eedb737e8960c into PR head commit bd199dfbe53f7a9cb0b71b45e048b98f4f173835
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f bd199dfbe53f7a9cb0b71b45e048b98f4f173835
 > /usr/bin/git merge 98a73e74fed491c44dcb1b361a1eedb737e8960c # timeout=10
 > /usr/bin/git rev-parse HEAD^{commit} # timeout=10
Merge succeeded, producing e402581b4779a8ce8e4d0b3af0a543ae99c1dbd4
Checking out Revision e402581b4779a8ce8e4d0b3af0a543ae99c1dbd4 (PR-48)
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f e402581b4779a8ce8e4d0b3af0a543ae99c1dbd4
Commit message: "Merge commit '98a73e74fed491c44dcb1b361a1eedb737e8960c' into HEAD"
 > /usr/bin/git rev-list --no-walk 760dbaed862dfc7416eb5d2e158499cb6ad941cb # timeout=10
[Bitbucket] Notifying pull request build result
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/myproject_PR-48-TV7ILAHCKATC6ZK26DHNTXI6KKLEGO3RSNDKAL2363W5GU5TABFQ
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
 > /usr/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from 2 remote Git repositories
 > /usr/bin/git config remote.origin.url https://bitbucket.org/mycompany/myproject.git # timeout=10
Fetching without tags
Fetching upstream changes from https://bitbucket.org/mycompany/myproject.git
 > /usr/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials Migrated by JIRA Plugin
 > /usr/bin/git fetch --no-tags --progress https://bitbucket.org/mycompany/myproject.git +refs/heads/VE-3869-dodawanie-edycja-i-usuwanie-aktu:refs/remotes/origin/PR-48
 > /usr/bin/git config remote.upstream.url https://bitbucket.org/mycompany/myproject.git # timeout=10
Fetching without tags
Fetching upstream changes from https://bitbucket.org/mycompany/myproject.git
using GIT_ASKPASS to set credentials Migrated by JIRA Plugin
 > /usr/bin/git fetch --no-tags --progress https://bitbucket.org/mycompany/myproject.git +refs/heads/master:refs/remotes/upstream/master
Merging remotes/upstream/master commit 98a73e74fed491c44dcb1b361a1eedb737e8960c into PR head commit bd199dfbe53f7a9cb0b71b45e048b98f4f173835
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f bd199dfbe53f7a9cb0b71b45e048b98f4f173835
 > /usr/bin/git merge 98a73e74fed491c44dcb1b361a1eedb737e8960c # timeout=10
 > /usr/bin/git rev-parse HEAD^{commit} # timeout=10
Merge succeeded, producing e74140b80595d0bf0ee1da066a6b34dab9797f2a
Checking out Revision e74140b80595d0bf0ee1da066a6b34dab9797f2a (PR-48)
 > /usr/bin/git config core.sparsecheckout # timeout=10
 > /usr/bin/git checkout -f e74140b80595d0bf0ee1da066a6b34dab9797f2a
Commit message: "Merge commit '98a73e74fed491c44dcb1b361a1eedb737e8960c' into HEAD"
 > /usr/bin/git rev-list --no-walk 760dbaed862dfc7416eb5d2e158499cb6ad941cb # timeout=10
First time build. Skipping changelog.
[Bitbucket] Notifying pull request build result
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare Env)
[Pipeline] bitbucketStatusNotify
Post stage
[Pipeline] bitbucketStatusNotify
Error when executing failure post condition:
java.lang.Exception: None or multiple repos
    at org.jenkinsci.plugins.bitbucket.scm.GitScmAdapter.getCommitRepoMap(GitScmAdapter.java:53)
    at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusHelper.createBuildStatusResources(BitbucketBuildStatusHelper.java:87)
    at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusHelper.createBuildStatusResources(BitbucketBuildStatusHelper.java:140)
    at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusHelper.notifyBuildStatus(BitbucketBuildStatusHelper.java:231)
    at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusNotifierStep$Execution.run(BitbucketBuildStatusNotifierStep.java:206)
    at org.jenkinsci.plugins.bitbucket.BitbucketBuildStatusNotifierStep$Execution.run(BitbucketBuildStatusNotifierStep.java:152)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
    at hudson.security.ACL.impersonate(ACL.java:290)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
Stage "Build" skipped due to earlier failure(s)
030
  • 10,842
  • 12
  • 78
  • 123
ohaleck
  • 661
  • 4
  • 20
  • Is the pipeline script that you posted compete? I cannot find the repository clone part. – Joerg S Sep 28 '18 at 03:56
  • @JoergS there was a stage containing a `git` step before, but I realized it works as well without it, since repository is being managed by the Bitbucket branch source plugin that's configured outside the Jenkinsfile. – ohaleck Sep 28 '18 at 13:26
  • If you say "but I realized it works as well without it" you mean that you were able to solve the issue? – Joerg S Sep 28 '18 at 16:10
  • No. The realisation came before the issue appeared. I removed the 'Git' step, which did not make the build worse. It seems legit since I would have to pass some kind of branch descriptor to Jenkins in order to have it build my pull requests. Anyway, even adding the 'Git' step back does not fix the problem. It still says '2 remote Git repositories' and fails with the 'None or multiple repos' exception. – ohaleck Sep 28 '18 at 17:10
  • Checking the log it presumably treats the PR and the PR Target as two different repositories. Are those in the same Bitbucket remote or is the pull request originated from some fork? – Joerg S Sep 29 '18 at 08:55
  • @JoergS they're on the same remote, Bitbucket itself. – ohaleck Oct 01 '18 at 08:26
  • I assumed that. Is it also the same repository or is the PR originated from some forked repository? – Joerg S Oct 01 '18 at 13:21
  • @JoergS PR originates from a clone on developer's machine. Not sure if this should be treated as the same repository, or the clone. Anyway, it gets merged to a branch on BitBucket, before PR is created. – ohaleck Oct 02 '18 at 13:54
  • Somehow to me it looks like the merge is done in Jenkins. That may be the reason it sees two repositories. Maybe you can try play with the merge settings in Jenkins in order to use the Bitbucket merge instead of having Jenkins do the merge. There’s another thread about that - however the other thread is about multibranch projects. I’ll search for the link maybe it’ll be of some help here as well. – Joerg S Oct 03 '18 at 07:04
  • See my answer here on how to pull the merged commit from Bitbucket instead of letting Jenkins to the merge. https://stackoverflow.com/a/51915362/4279361 . Maybe you may want to consider going for multibranch Pipeline using the Bitbucket branch source plugin which does not have THIS issue. Of course it has other issues, though :( – Joerg S Oct 03 '18 at 07:14
  • @JoergS I'm already using the plugin. Nacesk's answer below fixed the problem for me! – ohaleck Oct 09 '18 at 13:45
  • Good to know. Somehow I must have missed the fact you’re already using that plugin. – Joerg S Oct 10 '18 at 07:29

2 Answers2

7

Short answer: Remove all the bitbucketStatusNotify calls. Notifications are implicit as it's a bitbucket job.

Long answer: After having spent 3 hours with the same error, we saw the problem is related to the "bitbucketStatusNotify" plugin.

The error you pointed "Fetching changes from 2 remote Git repositories" seems to be related to how jenkins manage the PR jobs, and it makes the bitbucketStatusNotify plugin fail.

As it's a bitbucket plugin job, the notifications to bitbucket are done automatically, so if you remove the bitbucketStatusNotify calls, it should work as expected

nacesk
  • 86
  • 2
0

This is fixed by this PR but as of 1.4.1 is not tagged yet for release

https://github.com/jenkinsci/bitbucket-build-status-notifier-plugin/pull/43

Erik van Velzen
  • 6,211
  • 3
  • 23
  • 23