问题
Below is my jenkins file:
#!groovy
node('EJ2Release') {
try {
deleteDir()
stage('Import') {
git url: 'https://gitlab.syncfusion.com/essential-studio/ej2-groovy-scripts.git', branch: 'master', credentialsId: env.JENKINS_CREDENTIAL_ID
shared = load 'src/shared.groovy'
}
stage('Checkout') {
checkout scm
shared.getProjectDetails()
shared.gitlabCommitStatus('running')
}
if(shared.checkCommitMessage()) {
stage('Install'){
shared.install()
}
stage('Build') {
sh 'npm run build'
}
stage('Publish') {
shared.publish()
}
if(shared.isProtectedBranch()) {
stage('SampleBrowser') {
shared.triggerSampleBrowserBuild()
}
}
}
shared.gitlabCommitStatus('success')
deleteDir()
}
catch(Exception e) {
println(e)
shared.gitlabCommitStatus('failed')
deleteDir()
error('Build Failed')
}
}
Below is my groovy script - https://github.com/kumaresan-subramani/jenkins-build/blob/master/groovy
I want to trigger my Jenkins build every 2 hours, i have tried so many option but no luck for me. Can anyone have any suggestion to resolve this.
I have tried like below in jenkins file:
#!groovy
node('EJ2Release') {
try {
deleteDir()
triggers {
pollSCM 'H/10 * * * *'
}
stage('Import') {
git url: 'https://gitlab.syncfusion.com/essential-studio/ej2-groovy-scripts.git', branch: 'master', credentialsId: env.JENKINS_CREDENTIAL_ID
shared = load 'src/shared.groovy'
}
----------------etc----------
}
回答1:
The pollSCM
-trigger just triggers your build if there is any commit from your SCM that have to be build.
If you want to trigger every 2 hours also if there is no commit, you have to use the cron
-trigger:
triggers {
cron('0 */2 * * *')
}
回答2:
Two problems here, the first is as RNoB writes in his answer, you should use cron instead of pollSCM. The second is that you are confusing declarative and scripted pipeline (see my answer here for more information).
To set cron trigger in scripted pipeline you should use the properties step (which can be used to set most properties and triggers for a job (see pipeline-syntax generator to see what is available in your installation). So, the following should be sufficient:
properties([
pipelineTriggers([
cron('H H/2 * * *')
])
])
node('EJ2Release') {
try {
deleteDir()
stage('Import') {
git url: 'https://gitlab.syncfusion.com/essential-studio/ej2-groovy-scripts.git', branch: 'master', credentialsId: env.JENKINS_CREDENTIAL_ID
shared = load 'src/shared.groovy'
}
----------------etc----------
}
Note that I placed the properties step outside of the node allocation, this means that the step is executed as soon as possible after the job starts. And once again, this will only work in scripted pipeline.
来源:https://stackoverflow.com/questions/60001629/how-do-i-trigger-jenkis-build-for-every-2hours-through-jenkins-file