How to build from a specific git revision in Jenkins - jenkins

I have a Jenkins build job with
Source Code Management > GIT > Repository URL: https://GitHub_repo_url/branch_name
this will pull the the HEAD revision out of the branch.
How can I pull code of specified revision?

Related

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:
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.

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 Bitbucket Team/Project job doesn't fetch tags anymore

I have a Jenkins Bitbucket Team/Project job.
Inside the Jenkinsfile in my repository I use "git describe" to get the current tag.
After I updated to the latest Jenkins version (2.60.3) and updated most plugins, this fails because Jenkins doesn't fetch tags anymore.
This is what I had before the update:
Branch event
Checking out git https://bitbucket.org/xxxxxxx to read Jenkinsfile
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url https://bitbucket.org/xxxxx.git # timeout=10
Fetching upstream changes from https://bitbucket.org/xxxx.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials Admin user to clone bitbucket repositories
> git fetch --tags --progress https://bitbucket.org/xxxxx.git +refs/heads/master
This is what I have now:
Branch event
Checking out git https://bitbucket.org/xxxx.git into /var/lib/jenkins/jobs/xxxx/jobs/xxx/branches/master/workspace#script to read Jenkinsfile
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository https://bitbucket.org/xxxxx.git
> git init /var/lib/jenkins/jobs/xxx/jobs/xxxx/branches/master/workspace#script # timeout=10
Fetching upstream changes from https://bitbucket.org/xxx.git
> git --version # timeout=10
using GIT_ASKPASS to set credentials Admin user to clone bitbucket repositories
> git fetch --no-tags --progress https://bitbucket.org/xxxx.git +refs/heads/master:refs/remotes/origin/master
This is the plugins I have installed:
Bitbucket Branch Source Plugin 2.2.3
Bitbucket Plugin 1.1.5
Git Client plugin 2.5.0
Git plugin 3.5.1
The default behaviour changed. It's not fetching tags anymore (uses --no-tags).
I couldn't find any settings to force jenkins to fetch tags.
In Bitbucket Team/Project settings, you can setup "Advanced clone behaviours":
Shallow clone
...
Do not fetch tags
"Do not fetch tags" is not selected. So I'd think that it should fetch tags by default...
Anyone having similar issue?
How can I force Jenkins to fetch tags in a Bitbucket Team/Project job?
I created a Jenkins issue and got some explanations (thanks to Stephen Connolly): https://issues.jenkins-ci.org/browse/JENKINS-46736
The solution is just to add the "Advanced Clone Behaviours" and to leave "Do not fetch tags" unchecked.

How to clone the code from master branch in Bitbucket?

I am using Bitbucket in my project. When I am cloning the code on my local machine with my branch URL I am getting the updated code of my branch and unable to see the changes in the other branches. How to clone the updated code from all branches in Bitbucket?
First check which branch you're on and if you have already checked out the master branch:
git branch
You should see something like, although the names will probably be different:
dev
master
* test
Switch to the master branch. If master WAS NOT listed in the previous step run the following:
git checkout -b master origin/master
If master WAS listed just run:
git checkout master
You will now be on the master branch. Now do a git pull for the latest updates.

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