“Build Periodically” with a Multi-branch Pipeline in Jenkins

做~自己de王妃 提交于 2019-12-17 08:37:08

问题


I'm running Jenkins 2 with the Pipeline plugin. I have setup a Multi-branch Pipeline project where each branch (master, develop, etc.) has a Jenkinsfile in the root. Setting this up was simple. However, I'm at a loss for how to have each branch run periodically (not the branch indexing), even when the code does not change. What do I need to put in my Jenkinsfile to enable periodic builds?


回答1:


If you use a declarative style Pipeline and only want to trigger the build on a specific branch you can do something like this:

String cron_string = BRANCH_NAME == "master" ? "@hourly" : ""

pipeline {
  agent none
  triggers { cron(cron_string) }
  stages {
    // do something
  }
}

Found on Jenkins Jira




回答2:


If you are using a declarative style Jenkinsfile then you use the triggers directive.

pipeline {
    agent any
    triggers {
        cron('H 4/* 0 0 1-5')
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}



回答3:


I was able to find an example illustrating this an discarding old builds, which is also something I wanted.

Jenkinsfile in jenkins-infra/jenkins.io:

properties(
    [
        [
            $class: 'BuildDiscarderProperty',
            strategy: [$class: 'LogRotator', numToKeepStr: '10']
        ],
        pipelineTriggers([cron('H/30 * * * *')]),
    ]
)



回答4:


This is working for me:

  triggers {
    cron(env.BRANCH_NAME == 'development' ? 'H */12 * * *' : '')
  }

See more in this article




回答5:


For Paramertized periodic runs or scheduled triggers, one could use as follows.

triggers{
    parameterizedCron env.BRANCH_NAME == "develop" ? '''H 03 * * * % buildSlave=vm1;testSlave=vm2;HYPERVISOR=vbox;VERSION=10.5.0.0
H 03 * * * % buildSlave=vm1;testSlave=vm2;HYPERVISOR=workstation;VERSION=10.5.0.0''' : ""
}


来源:https://stackoverflow.com/questions/39168861/build-periodically-with-a-multi-branch-pipeline-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!