Search code examples
jenkinsjenkins-pipelinebitbucketjenkins-plugins

Jenkins pipeline checkout wrong revision


I'm trying to implement a new Jenkins pipeline using Jenkinsfile.

The GIT repository does not have a Jenkinsfile on every branch, just under a specific one (as I'm currently building CI stuff for a legacy repository).

Jenkins Pipeline is created with following configuration:

  • Discard old builds (I don't want to manually cleanup)
  • Bitbucket webhook trigger (on push event)
  • Pipeline script from SCM
    • Bitbucket server (using Bitbucket server plugin)
    • Authentication stuff
    • Branches to build: **
    • Script path: Jenkinsfile
    • Lightweight checkout is disabled

The webhook is automatically created in my Bitbucket repository, and builds are triggered just fine (when I push modification on the branch with the Jenkinsfile).

However, Jenkins does not checkout the commit sent in the hook (different HASH from another branch). As a result, build crashes as Jenkinsfiles do not exist on another branches (and will never exist on old legacy branches).

 > git fetch --tags --force --progress -- http://mybitbucket:port/scm/vs/myrepo.git +refs/heads/*:refs/remotes/myrepo/* # timeout=10
Seen branch in repository myrepo/old-branch-a
Seen branch in repository myrepo/old-branch-b
...
Seen 40 remote branches
 > git show-ref --tags -d # timeout=10
Checking out Revision 0a781a1bd4ad194aae911ce26d1ec4cd1e73dd76 (myrepo/old-branch-x)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 0a781a1bd4ad194aae911ce26d1ec4cd1e73dd76 # timeout=10
Commit message: "my commit message"
First time build. Skipping changelog.
Posting build status of INPROGRESS to mybitbucket for commit id [0a781a1bd4ad194aae911ce26d1ec4cd1e73dd76] and ref 'null'
ERROR: /var/jenkins_home/workspace/myrepo@script/Jenkinsfile not found

Technical context:

I cannot understand why this revision is chosen over the one sent in the hook.

Thanks a lot for your help!


Solution

  • I was misled all along.

    Turn out that I need to use Multibranch pipelines instead of regular ones. It works well and create webhooks automatically as desired (using the right Scan Multibranch Pipeline Triggers configuration).

    I'm still frustrated by the behaviour of regular pipelines, which makes no sense to me and seems poorly documented.