Jenkins pipeline how to get the branch which triggers the build? - jenkins

I have a Jenkins pipeline job (not multi-branch pipeline), one of the logic inside my pipeline is, when there is a merge request on the gitlab scm, I want to build something based on the source branch of the merge request which triggered this build.
My question is, inside the Jenkinsfile, how do I dynamically get this source branch and checkout the code and create a workspace for me to run builds on this branch?

Are you using the GitLab plugin? If so, when a build is triggered using the webhook it gets access to a few variables.
https://github.com/jenkinsci/gitlab-plugin#defined-variables
One of them is the branch that triggered the build:
Is env.gitlabSourceBranch what you're looking for?

Related

Build a Jenkins job on a merge request to master

In GitLab, whenever someone creates a Merge Request from some branch into master, I need that event to trigger a Jenkins job (Freestyle, yeah I know..) and pass the branch name as a parameter for the Jenkins build.
I've looked and it seems like the closest thing to it is the Jenkins CI integration in GitLab. Thing is, These are the options to configure in the Jenkins CI integration, and it doesn't seem like it allows to trigger it only when the Merge Request is for master or to pass any parameters.
Is it possible to configure it so that only if a Merge Request to master occurs it would trigger the Jenkins job with the source branch as a parameter? Is there another/better way of doing it?
Thanks ahead!
confing in your projects jenkins:
config in your gitlab:
or orther my answer

Jenkins Multibranch Pipeline: Differentiate between build triggered by branch indexing vs webhook

I have a Jenkins multibranch pipeline for github and currently it triggers builds whenever a branch is indexed. I have certain actions in my pipeline that I only want triggered whenever there are changes to a branch (on a webhook push). So I was wondering if there's a Jenkins environment variable that will allow me to distinguish between the two different type of builds. Or if there's a git solution to check when the last change was made to the branch and differentiate it that way

Jenkins being run on all branches in repo instead of only the branch changed

I am pretty new to Jenkins, and when I make a pull request, the Github webhook triggers a rebuild on all branches in the repo. However, I only want the branch associated with the pull request to be built. I suspect that all branches are built due to nothing specified in the "Branch specifier."
Is there a way to specify to build only the branch the pull request was made to?
Thanks!
To resolve your issue try to use multibranch pipeline job or use filter in simple pipeline job.
To use multibranch pipeline job you need to install Pipeline: Multibranch plugin.
Pipeline job example
In simple pipeline job choose "Build when a change is pushed to GitLab" in "Build trigger" section -> Advanced -> Filter branches by name

How can I trigger a Jenkins pipeline run from a GitHub webhook and still allow manual builds?

I am using GitHub pull request builder to trigger pipeline builds from GitHub webhooks when developers make a PR.
To make sure I pull the pipeline script related to the PR I am using Pipeline script from SCM and specifying ${sha1} as the branch specifier. The issue is that when I trigger the build manually, Jenkins is trying to pull a ${sha1} branch (since the env variable doesn't exist).
Is there a way to default to master when triggering the build manually?

Jenkins BlueOcean only to build using a single branch for build?

When I build a multibranch pipeline job using BlueOcean it looks into my repository and reads all of my Jenkinsfile for each branch. to initiates multiple build, but I only want to be able to build a job based on a certain branch. Is there a way to only build blueocean?
Thanks
I sounds like your job is a Multibranch Pipeline.
If you create a job that is just a Pipeline (rather than Multibranch Pipeline), you can specify a single specific branch to build.

Resources