Jenkins pipeline checkout wrong revision - jenkins

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:
Jenkins 2.303.3 running in a docker container
Bitbucket server integration plugin
I cannot understand why this revision is chosen over the one sent in the hook.
Thanks a lot for your help!

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.

Related

Gitlab-jenkins webhook triggers build with wrong branch

I've been trying to setup a build-trigger with a gitlab web-hook to my Jenkins freestyle project,
For that,
In Jenkins, added gitlab connection, added the ssh url and specified the branch I need to build in the git plugin's Branches to build section,
Also, with the Gitlab plugin installed, opt for the Build Triggeres > gitlab webhook url
As the next step, created an outgoing webhook in gitlab and added the url,
Now the webhook works and the build gets triggered on repo changes,
But the problem is Jenkins Builds the Project on different Branch when it gets triggered from web hook,
What I've done so far,
On checking further, when there is changes in multiple branches ie, origin/master, origin/feature, origin/test, The first one in the list ie the master getting build instead of the specified test branch.
When built from jenkins directly, It pulls the correct branch, no issues there,
I've modified the configuration as per this stack-overflow answer. such as mentioning the 'refspec' and adding advanced clone behaviours and shallow clone, but nothing seems to work,
comparing the git commands executed at the time of a direct jenkins build and a gitlab webhook-triggered build shows this difference,(with the refspec modifications)
# on successful build
> git fetch --no-tags --progress --depth=5 -- git#gitlab.com:<project_name>.git +refs/heads/test:refs/remotes/origin/test # timeout=10
> git rev-parse origin/test^{commit} # timeout=10
# when triggered via webhook (it switches to master at last)
> git fetch --no-tags --progress --depth=5 -- git#gitlab.com:<project_name>.git +refs/heads/test:refs/remotes/origin/test # timeout=10
> git config remote.origin.url git#gitlab.com:<project_name>.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/test:refs/remotes/origin/test # timeout=10
Avoid second fetch
> git rev-parse remotes/origin/master^{commit} # timeout=10
All branches with changes are showing in the build section instead of the test branch,
which is the only branch I need this project to pull from the remote,
Using Jenkins version 2.303.3 JDK 8, plugins version Gitlab: 1.5.22, Git : 4.10.0,
Can someone suggest any solution for this,
As of now, it is building the wrong branch only at the time of testing web-hook from gitlab,
On a regular change, the desired branch is build as specified in Jenkins and in gitlab, be sure to use the branch filter in gitlab if doing like this,
It is evident that when testing the web-hook, gitlab sends triggers for all branches that got changes, what happens in Jenkins I assume is it builds the default branch when receiving such triggers,

Jenkins pipeline script from a branch and a specific commit does not work

I work with Jenkins and git-gerrit.
I know how to fetch Jenkins pipeline file from a master and take a specific git commit through a jenkins parameter.
I am also able to clone Jenkins pipeline from a specific branch.
I do not know how to combine 1 and 2.
How can I fetch Jenkins pipeline from a specific branch and specify specific commit?
The picture below shows configuration which is not working.
If I replace branch_1 with a master and I enter a commit sha1 from master branch it works.
If I remove the commitId and leave branch_1 I get the tip of branch_1 in my workspace.
If I I leave branch_1 and commitId from branch_1 like on the picture it does not work.
Error I get is:
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
ERROR: Maximum checkout retry attempts reached, aborting
Finished: FAILURE

BitBucket Team Multibranch pipeline job is ignoring PRs

I'm using the Bitbucket Branch Source Plugin to automatically configure multibranch pipeline jobs for every repo under a specific project directory within Bitbucket. One repo contains a valid Jenkinsfile. The master branch for that repo builds and deploys fine. However, pull requests for that repo are not being built. I see the following logs:
Connecting to <URL> using <credentials>
Looking up repositories of team <Project>
Proposing test-project
Connecting to <URL> using <credentials>
Looking up <Project>/test-project for branches
Checking branch master from <Project>/test-project
Met criteria
Looking up <Project>/test-project for pull requests
Checking PR from ~<user>/test-project and branch feature/thing
Does not meet criteria
The specified branch for the pull request does contain a Jenkinsfile, so I do not understand why it says that the criteria are not met. Any suggestions?
The "Automatic branch project triggering" option is set to the default .*
I'm using the following plugins to Jenkins:
Bitbucket Branch Source Plugin 1.8
Branch API Plugin 1.10.2
GIT Plugin: 2.4.0
Pipeline 2.4
Pipeline: Multibranch 2.8
Pipeline: SCM Step 2.2
SCM API Plugin 1.3
... others omitted for brevity
Well, I feel dumb.
Jenkins did not have read permission on <user>'s fork of the repo containing the branch for the PR.
When you make a PR, all the users with access to the repository receiving the PR are able to view it without issue, so this may be an issue with Bitbucket Server itself (I'm on 4.2.0) not allowing those users to have remote read access to that branch once it has been included in a PR.

Get Git commit from "upstream" build in manually triggered Jenkins job

I have a Build job in Jenkins that checks out a specific Git commit and packages it for deployment as artifacts.
There is a later Deployment job that takes the built artifacts and actually deploys the code. It also does a sparse Git checkout of a specific directory containing deployment scripts. After successful completion, we write a Git tag.
The problem is that the tag is being written to the HEAD of master, not to the hash of the commit used for the original upstream build. (master is the branch defined in the job configuration.)
Is there a way to get the upstream SCM information if it's not passed directly through a parameterized trigger? I can see commits listed in the build.xml file that Jenkins generates in the build directory; is there a way to read this information from the downstream job?
I realize that it's not really "downstream", since it's manually triggered. We do have a selector that defines UPSTREAM_BUILD and UPSTREAM_PROJECT, though.
If you are using the Copy Artifact plugin, you could write a file with the commit hash during the Build job and read it back in during the Deployment job:
# Build
echo ${GIT_COMMIT} > COMMIT_HASH
# Deployment, after copying COMMIT_HASH into the workspace
git checkout $(cat COMMIT_HASH)

Jenkins trigger builds due to fail polling

I am using Jenkins to push NodeJS project to Heroku. My project configuration has 2 git repositories setup, one is the source code and the other is heroku's app git repository.
The problem is that jenkins is polling on both repositories and when it fail to find the branch I'm looking for in the code repository is fails with the following error:
Using strategy: Default
[poll] Last Built Revision: Revision 1234 (origin/qa)
using GIT_SSH to set credentials Jenkins SSH Key
> git -c core.askpass=true ls-remote -h git#heroku.com:my-app.git qa # timeout=10
[poll] Couldn't get remote head revision
Done. Took 3 sec
Changes found
As you can see it fail to find head revision for the heroku git repository and decide changes found.
How can I make Jenkins poll only on the source code repository?
I've update my GIT plugin to version 2.4.0 and it seem to solve the problem.

Resources