Jenkins Scripted Pipeline to trigger from changes in git directory - jenkins

I need code to jenkins scripted pipeline where I can say that pipeline is only allowed to trigger from changes in git directory and not from changes in whole repository to avoid useless triggers.
Thanks!

I understand that you want your pipeline to run only when changes are detected for a specific sub directory in your Git repository, but not for changes only affecting files outside that directory.
Theoretically, the PathRestriction class that could be used with the checkout step would offer this through includedRegions and excludedRegions.
Unfortunately, this is not working for Git in Jenkins Pipeline, see JENKINS-36195.

Related

List Git Branches on Jenkins after the pipeline build

Currently i'm using in my jenkins pipeline the List Git Branches system, and its working very well, when i'm gonna build the pipeline, i choose the branch that is automatic pulled from git repository that i specify in the pipeline configs. But i have a problem, i need to make a new pipeline model that the user gonna choose the git repository link with a jenkins choise parameter before the build starts and when the build starts, the branchs gonna be pulled and displayed to choise, so well, i'm needing a way to pause the pipeline build to user choise the especified branch and after that the proccess continues normally.

Polling from multiple repositories in Jenkins

I couldn't find an answer for my question on stack, sorry if it is a duplicate.
Let's say I have repo A on Github, where my Jenkinsfile is, and repo B on Github, where all my source code is.
For my job, I've set 'Pipeline script from SCM' from repo 'A', added 'Polling ignores commit in certain paths' with included region containing only this Jenkinsfile.
In Jenkinsfile, I'm using checkout plugin to pull repo 'B' with added PathRestriction extension.
Also I've added pollSCM to 'triggers' with value 'H/5 * * * *'.
But whenever I commit something to repo 'A' without any changes to this Jenkinsfile, build is triggered anyway.
My question is: How can I setup my build to trigger only, when Jenkinsfile in repo 'A' or my source code is changed?
There are two ways to trigger a build only when specific file changes.
 1. script hook: On git server side, create a hook that will check if the last commit changed the Jenkinsfile, if true, send a post to jenkins job configured with 'Trigger build remotely'.
 2. Commit: Create a repo only with the Jenkinsfile, so every commit on this repo will be a change on Jenkinsfile.
I strong advise you to change your approach and use Jenkins shared library, this is a much better way to share and manager pipeline across multiple projects / jobs

How to run Jenkins pipeline automatically when "git push" happens for specific folder in bitbucket

I have started using Jenkins recently and there is only scenario where I am stuck. I need to run "Jenkins pipeline" automatically when git push happens for specific folder in master branch. Only if something is added to specific folder, than pipeline should run.
I have already tried SCM with Sparse checkout path, and mentioned my folder, but that's not working.
I am using GUI free style project, I dont know groovy.
I had the same issue and I resolved it by configuring the Git poll.
I used poll SCM to trigger a build and I used the additional behavior of Jenkins Git plugin named "Polling ignores commits in certain paths" > "Included Regions" : my_specific_folder/.*
By the way, using Sparse checkout path allows jenkins to checkout only the folder you mentioned.

Jenkins is checking out the entire SVN repo twice

I have a Jenkins Pipeline setup, and a Jenkins file which has the below content:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Hey'
}
}
}
}
A post commit hook triggers the Jenkins build successfully, and I can see it starting from the Jenkins UI. It states that it is checking out the repo to read from the Jenkins file and it stores the checkout in the workspace#script folder on the server.
Checking out svn https://<svn_server>/svn/Test/Core into C:\Program Files (x86)\Jenkins\jobs\CI_Build\workspace#script to read JenkinsPipeline/Jenkinsfile
Checking out a fresh workspace because C:\Program Files (x86)\Jenkins\jobs\CI_Build\workspace#script doesn't exist
Cleaning local Directory .
After this is complete, I make a change to a file in the repo and the build triggers via the post commit hook happily, but then it tries to checkout the entire code base again into a folder called workspace. I would have expected that the checkout happens once and then the "use SVN update as much as possible" option would kick in and only update the changed files? Or maybe I have the wrong logic?
SVN version - 1.9.7
Jenkins version - 2.84
Jenkins has to know what is in your pipeline script before it knows if it should checkout your code. It is possible that your pipeline says not to check out the code, and you set into a subdirectory and fire off the checkout yourself. Or maybe checkout multiple repos in different places. Until Jenkins sees your Jenkinsfile, it can't know what you want. So it has to checkout the repo once to see your pipeline, then again to do the work.
With git (and maybe some versions of other repo plugins) lightweight or sparse checkouts are supported, so then it only grabs the jenkinsfile instead of the entire repo. I don't think this is a supported option in SVN yet.
Lightweight checkouts are now supported by the SVN plugin, the SVN plugin was updated in version 2.12.0 to add this feature - see https://wiki.jenkins.io/display/JENKINS/Subversion+Plugin.

Jenkins change log when using multiple repositories

We are using Jenkins pipeline for job executions and pipeline code is committed to subversion repository and checkout as Jenkins library. So whenever I execute job, it will look for changes at my pipeline code repository and publish the change logs. Changes committed to application source code repository is ignoring because of this reason. Do we have any options to specify which repository change logs should be published at Jenkins?

Resources