Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure a Jenkins Pipeline to be triggered by polling SubVersion?

We have been using Jenkins for Continuous Integration for some time. A typical build job specifies the SVN repository and credentials in the "Source Code Management" section, then in the "Build Triggers" section we enable "Poll SCM" with a polling schedule of every 10 minutes (H/10 * * * *). We have updated to the latest version of Jenkins and are looking to set up pipeline builds. A typical pipeline script looks like:

node {
    stage 'Build'
    build job: 'MyApplication Build'
    stage 'Deploy to test environment'
    build job: 'MyApplication Deploy', parameters: [
        [$class: 'StringParameterValue', name: 'DatabaseServer', value: 'DatabaseServer1'],
        [$class: 'StringParameterValue', name: 'WebServer', value: 'WebServer1']
    ]
    stage 'RunIntegrationTests'
    build job: 'MyApplication Test', parameters: [
        [$class: 'StringParameterValue', name: 'DatabaseServer', value: 'DatabaseServer1'],
        [$class: 'StringParameterValue', name: 'WebServer', value: 'WebServer1']
    ]
}

When the pipeline job is triggered manually then everything runs fine, however we would like this pipeline to be run every time a new revision is checked in to the SVN repository. The pipeline configuration does have a "poll SCM" build trigger option, but does not have a "Source Code Management" section where you can specify your repository. How can we achieve this?

like image 308
Jon Lawson Avatar asked Jun 08 '16 11:06

Jon Lawson


People also ask

How does Jenkins integrate with subversion?

Now create a Jenkins build job. Select New Item, give the build project a name such as svn-tomcat-demo, select Maven project, and click OK. Under source code management, select Subversion and enter your SVN repository URL and credential. Please download the sample code and check the code into your SVN server.

What syntax does Jenkins use to schedule build job or SVN polling?

CRON is a syntax used to schedule a Build job or SVN polling in Jenkins.

What is polling in Jenkins?

"Poll SCM" polls the SCM periodically for checking if any changes/ new commits were made and shall build the project if any new commits were pushed since the last build, whereas the "build" shall build the project periodically irrespective to whether or not any changes were made.


4 Answers

Using a Jenkins Declarative Pipeline script, you can configure a job to poll an SVN repository URL every 10 minutes as follows:

pipeline {
    agent any
    triggers {
        pollSCM 'H/10 * * * *'
    }
    stages {
        stage('checkout') {
            steps {
                checkout([
                    $class: 'SubversionSCM', 
                    additionalCredentials: [], 
                    excludedCommitMessages: '', 
                    excludedRegions: '', 
                    excludedRevprop: '', 
                    excludedUsers: '', 
                    filterChangelog: false, 
                    ignoreDirPropChanges: false, 
                    includedRegions: '', 
                    locations: [[
                        credentialsId: 'mySvnCredentials', 
                        depthOption: 'infinity',
                        ignoreExternalsOption: true, 
                        local: '.', 
                        remote: 'http://example.com/svn/url/trunk']], 
                    workspaceUpdater: [$class: 'CheckoutUpdater']
                ])
            }
        }
    }
}

The pollSCM trigger should automatically poll all SCM Repository URLs associated with your build, including URLs specified by checkout steps, the URL of your Declarative Pipeline script from SCM, and the URL of your Global Pipeline Libraries. If you truly want the pipeline to be run for every single revision however, you'll need to set up a post-commit hook instead.

like image 108
heenenee Avatar answered Oct 13 '22 19:10

heenenee


The solution that I have found to work is:

  1. Move the pipeline script into a file (the default is JenkinsFile) and store this in the root of my project in SubVersion.
  2. Set my pipeline job definition source to "Pipeline script from SCM", enter the details of where to find my project in SubVersion as per a normal Jenkins build job, and set the Script Path to point at the JenkinsFile containing the pipeline script.
  3. Set the build trigger of the pipeline job to "Poll SCM" and enter a schedule.
  4. Manually run the pipeline job

It seemed to be step 4, manually running the pipeline job that caused the poll trigger to pick up the correct repository to poll. Before that it didn't seem to know where to look.

like image 45
Jon Lawson Avatar answered Oct 13 '22 21:10

Jon Lawson


As an alternative to when the pipeline script is not part of the project or is defined in the job, you can add poll: true to your checkout stage.

Example:

stage('checkout') {
    checkout(
        changelog: true, 
        poll: true, /*This is the important option*/
        scm: [
            $class: 'SubversionSCM', 
            filterChangelog: false, 
            ignoreDirPropChanges: false,
            locations: [...], /*ommited for obvious reasons*/
            workspaceUpdater: [$class: 'CheckoutUpdater']
        ])
}

After the first run it will start polling from this SCM also as from the SCM where the pipeline is if it is the case.

This option is documented at https://jenkins.io/doc/pipeline/steps/workflow-scm-step/#code-checkout-code-general-scm , in the very end of the page without details.

like image 40
Felipe Nascimento Avatar answered Oct 13 '22 21:10

Felipe Nascimento


I think you need a Checkout stage before before your Build stage, which consists of the SCM information. This allows the job to Poll SCM at the desired interval and run the pipeline.

You can even use Pipeline script, without having the pipeline codes to store as a JenkinsFile in SCM.

Below is my SVN Checkout stage pipeline code before my Build stage:

stage('Checkout') {
    checkout([$class: 'SubversionSCM', 
        additionalCredentials: [], 
        excludedCommitMessages: '', 
        excludedRegions: '', 
        excludedRevprop: '', 
        excludedUsers: 'buildbot', 
        filterChangelog: false, 
        ignoreDirPropChanges: false, 
        includedRegions: '', 
        locations: [[credentialsId: 'b86bc2b6-994b-4811-ac98-0f35e9a9b114', 
            depthOption: 'infinity', 
            ignoreExternalsOption: true, 
            local: '.', 
            remote: "http://svn/something/trunk/"]],
        workspaceUpdater: [$class: 'UpdateUpdater']])
}

Works for my pipeline job though. Hope this helps.

like image 23
zionyx Avatar answered Oct 13 '22 21:10

zionyx