0
votes

I am new Jenkins. I was able to configure commit-based job trigger using Freestyle job. This way, any new commit to GitHub was triggering the given job.

But when it comes to pipeline job, I am not able to achieve the same. Please help regarding the same.

In Build Triggers section of pipeline, I have enabled GitHub hook trigger for GITScm polling.

pipeline{
    agent {
        node 'npm-linux'
    }

    options {
        timeout(time: 15, unit: 'MINUTES')
        disableConcurrentBuilds()
    }

    stages {
        stage('build') {
            steps {
                sh 'git clone link'
                sh 'mvn clean install'
            }
        }
    }
}
1
Did you try the suggestion? - Technext

1 Answers

1
votes

As you're able to successfully see the freestyle job getting triggered on commit, we know for sure that GitHub has been configured correctly. Now, to fix the declarative pipeline issue, you have to make use of triggers in your pipeline code.

For example,

pipeline {
    agent any

    triggers {
        // Instead of '* * * * *', you may use 'H/2 * * * *' which will check for source code changes every two minutes
        pollSCM '* * * * *'
    }

    stages {
        stage ('Echo') {
            steps {
                echo 'Hello, World!'
            }
        }
    }
}

Note:

  • In Build Triggers section of your job configuration, you still have to keep the check box enabled for GitHub hook trigger for GITScm polling
  • Even after the above setting, the declarative pipeline job did not trigger automatically when i pushed my commit. When i ran the job manually once, thereafter things went fine. All subsequent commits triggered the pipeline job. So, just manually trigger the job once and things should be fine.
  • Also remember that polling is resource intensive so it is generally not a good idea to use it. However, in this case, where you are using GitHub, i'm not sure whether post-commit functionality can be configured using any way other than GitHub Webhooks. And sadly, just with GitHub Webhooks enabled, pipeline job wasn't getting triggered without the help of triggers directive.