To keep this in the same job is going to require a little bit of groovy coding. Since your using MultiBranch pipeline this can all live in your Jenkinsfile
First, Set your cron as Vitalii mentions, this will kick of the job on schedule.
properties([
pipelineTriggers([cron('0 0 * * *')])
])
Next, when this job is triggered by the schedule, we want to adjust the params its running with. So first we need to check what caused the build. This will probably require Security script approval.
List causes = currentBuild.rawBuild.getCauses().collect { it.getClass().getCanonicalName().tokenize('.').last() }
If this contains 'TimerTriggerCause'
then we want to update the parameter.
if (causes.contains('TimerTriggerCause') {
setBooleanParam("EXECUTE_TESTS", true)
}
We wrote a function in a shared lib for this, you can put it in the same Jenkinsfile if you wish (At the bottom outside the pipeline logic):
/**
* Change boolean param value during build
*
* @param paramName new or existing param name
* @param paramValue param value
* @return nothing
*/
Void setBooleanParam(String paramName, Boolean paramValue) {
List<ParameterValue> newParams = new ArrayList<>();
newParams.add(new BooleanParameterValue(paramName, paramValue))
try {
$build().addOrReplaceAction($build().getAction(ParametersAction.class).createUpdated(newParams))
} catch (err) {
$build().addOrReplaceAction(new ParametersAction(newParams))
}
}
And let the job continue as normal. When it gets to evaluating params.EXECUTE_TESTS, this will now be true (instead of the default false).
Note: May need to import model for the value
import hudson.model.BooleanParameterValue
Putting that all together (Just cobbled the bits together quickly for an overall picture), Your jenkinsfile would end up something like this
#!groovy
import hudson.model.BooleanParameterValue
List paramsList = [
choice(name: 'ACCOUNT_NAME', choices: ['account1', 'account2'].join('\n'), description: 'A choice param'),
string(name: 'PARAM', defaultValue: 'something', description: 'A string param'),
booleanParam(defaultValue: false, name: 'EXECUTE_TESTS', description: 'Checkbox'),
]
properties([
buildDiscarder(logRotator(numToKeepStr: '20')),
pipelineTriggers([cron('0 18 * * *')]), // 4am AEST/5am AEDT
disableConcurrentBuilds(),
parameters(paramList)
])
ansiColor {
timestamps {
node {
try {
causes = currentBuild.rawBuild.getCauses().collect { it.getClass().getCanonicalName().tokenize('.').last() }
if (causes.contains('TimerTriggerCause') {
setBooleanParam("EXECUTE_TESTS", true)
}
stage('Do the thing') {
// Normal do the things like build
}
stage('Execute tests if selected') {
if (params.EXECUTE_TESTS == true) {
// execute tests
} else {
echo('Tests not executed (Option was not selected/False)')
}
}
} catch (err) {
throw err
} finally {
deleteDir()
}
}
}
}
/**
* Change boolean param value during build
*
* @param paramName new or existing param name
* @param paramValue param value
* @return nothing
*/
Void setBooleanParam(String paramName, Boolean paramValue) {
List<ParameterValue> newParams = new ArrayList<>();
newParams.add(new BooleanParameterValue(paramName, paramValue))
try {
$build().addOrReplaceAction($build().getAction(ParametersAction.class).createUpdated(newParams))
} catch (err) {
$build().addOrReplaceAction(new ParametersAction(newParams))
}
}