Jenkinsfile trigger whenever there is a new commit in the branch - jenkins

https://www.jenkins.io/doc/book/pipeline/syntax/#triggers
I have a multibranch job that scans branches and create a pipeline from Jenkinsfile in the branch.
I want a build in the branch to trigger automatically upon a commit being merged into that branch.
i looked at the trigger documenation. it says
"The triggers currently available are cron, pollSCM and upstream."
i think both cron and pollSCM will trigger even if there is no new commit and they are time based so it wouldnt be upon when a commit is merged

If you are using Git/Bibtbucket for your repository you could use the build trigger, based on webhook connection between your Git/Bitbucket repo and Jenkins instance (In the settings of the repo -> Webhooks and there -> new hook with this URL -> http://Jeknins-url/bitbucket-hook/ ) and this will allow the Jenkins to build whenever a push to that project is made. It will run only the job for the branch which got the push command and the merging to a branch is considered as a push, so i assume this will resolve your problem.

Related

I have a use case which causes issues with Jenkins free style project poll scm

I have a free style project which will trigger on commits. I have kept poll scm which polls for commits in remote repository. This jenkins job actually make some changes to a file after checkout and then commits+push back to remote repository.
This push by jenkins job is actually triggering back a new build due to scm poll for new commits is set in job. I want this job not to trigger again for self commit by job itself
It is not clear if you are using Git. In case you are and expect that the file will be always be changed by Jenkins, Git plugin in freestyle has options to ignore certain file paths.
If this specific file is expected to edited by user other than Jenkins than only solution I can think of is through conditional logic in jenkins pipeline-as-code.

Jenkins pipeline build branch triger

I have gitlab with commit hook to Jenkins. The hook is triggered on any commit in any branch. Jenkins has three branches to build.
And I have a problem. If I create any other branch and push it, the hook will trigger the Jenkins and Jenkins will build a branch (master, develop or hotfix) with the newest commit. How can I forbid the build for all other branches except the specified three? I know about when, I need to cancel the build, not just one stage
I found an item in the submenu that is responsible for which branches are used to activate the build.

Trigger jenkins pipeline on hook trigger when changes are pushed to specific branch

I have a project on GitHub with branch master, develop and Develop_Sprint1. I have a pipeline to checkout a code, build and test.
I need to trigger this job with webhook trigger when changes are made to specific branch like develop.
Can you please help me in setting this.

how do I create Master + PR branch and run some tests on it?

I am new to jenkins.
I want to build pipeline/jenkins job where once developer has pull request approved by other developers with 1 LGTM, I would build a new Master + the PR as new Master and run smoke tests.
If it passes, I will merge to the Master.
Thanks in advance!
You would need to setup a multibranch pipeline on a dedicate repo.
That dedicated repo would be one where (master+PR) branches are pushed to: their new branch would trigger the multibranch pipeline.
In order to push such branches (master+PR), you need another job which will:
periodically fetch from the origina repo
check for any PR branch with a +1 LGTM
create a new branch from master and merge that PR branch
push the resulting new branch to the dedicated remote repo mentioned before.

Jenkins pipeline automatic branch detection

I am migrating my old Jenkins free-style job to multi-branch pipeline. I also want to use GitLab hook with them.
My problem is the branch detection. I am doing it manually but I want it to be automatic: when a new branch is pushed to git, GitLab trigger a Jenkins job that trigger the branch detection if the branch parameter from GitLab is not known for Jenkins at the moment. Is this possible to do it or doesn't this exist?
FYI: I tried to launch the multi-branch pipeline job but Jenkins says:
ERROR: No parameterized job named XXX found.
Enable "Build Periodcally" in your multibranch job configuration and the branch indexing will automatically started.
What you really need is a branch source plugin for GitLab with webhook integration, which is tracked as an RFE in JIRA.
Failing that, use a plain Git branch source and configure GitLab to send Jenkins notifications to /git/notifyCommit (IIRC) as documented on the Git plugin wiki. Need specify only a url, no other details. The branch indexing this triggers should both detect new or removed branches, and changes to the head of an existing branch, and schedule builds accordingly.
You can set webhook in GitLab for push events and URL like http://<yourserver>/git/notifyCommit?url=<URL of the Git repository>.
See https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-Pushnotificationfromrepository
GitLab notifies Jenkins on push events which should trigger branch detection also for multibranch pipeline.
I didn't receive the answer I wanted and I ran into this issue today that answered the question :
https://github.com/jenkinsci/gitlab-plugin/issues/298
TLDR: Multi-branch pipeline are not supported yet to be triggered by gitlab commit easily. There is a workaround. Look at the link above.

Resources