How to postpone the work of Jenkins until a certain time?

In our current project setup, we have assignments A, B, C.

When A, B or C is successfully completed, we want to deploy the received artifacts to our development server. Therefore, we use the work D.

Because the deployment recreates the development database, we just want to get the work done every hour. Our testers are familiar with this schedule and work this way.

In fact, when D is started using A, B or C, a script is launched that waits until the server has 00 minutes. If one of the trigger jobs starts D again while it is waiting, the previous waiting script is canceled and restarted. If the script reaches 00 hours, deployment will occur.

The main problem is that work D locks the building slot for 59 minutes in the worst case.

Performing an hourly job by default is not an option, because then deployment will happen even if nothing changes.

Running a job after something has changed is also bad, because testers are used for hourly deployment.

I know that there is a "quiet time" option, but this allows me to set the wait time relative to the start time. I need a "quiet time" that delays the work until it reaches a certain time. Anyone give tips on how to achieve this?

+4
source share
1 answer

I developed the following solution:

1) Conditional BuildStep Plugin A, B C, cron D config.xml:

  • Build
    • ()
      • : shell [ , ]
        • Command

 

#!/bin/bash
echo "  This script is a placeholder to represent a build step that can succeed or not succeed"
true   # to test build success → Groovy script should be executed
#false  # to test not successful build → nothing should be done
  • Builder: Groovy script
    • ◉ Groovy

 

// From: How to delay Jenkins job until a certain time was reached?
//       http://stackoverflow.com/questions/27952216/1744774

// -----------------------------------------------------------
// Adapt these according to your environment
final String DOWNSTREAM_NAME = 'SO-27952216-Downstream-job'
final String CRON_SCHEDULE = '0 * * * *'
// -----------------------------------------------------------

final String SEPARATOR = new String(new char[8]).replace('\0', '-')
println("  ${SEPARATOR} Adapting configuration of ${DOWNSTREAM_NAME} ${SEPARATOR}")

import jenkins.model.*
import hudson.model.*

final Project DOWNSTREAM_JOB = Jenkins.instance.getItem(DOWNSTREAM_NAME)
final String DOWNSTREAM_CONFIG = DOWNSTREAM_JOB.getRootDir().getPath() + "/config.xml"  

//import hudson.triggers.*
//DOWNSTREAM_JOB.getTrigger(TimerTrigger.class).spec = CRON_SCHEDULE
// leads to:
//   ERROR: Build step failed with exception groovy.lang.ReadOnlyPropertyException: 
//       Cannot set readonly property: spec for class: hudson.triggers.TimerTrigger

import org.w3c.dom.*
import javax.xml.parsers.*
import javax.xml.xpath.*

println("  Reading ${DOWNSTREAM_CONFIG}")
Document doc = 
    DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(DOWNSTREAM_CONFIG)
XPathExpression expr = XPathFactory.newInstance().newXPath()
    .compile("/project/triggers/hudson.triggers.TimerTrigger/spec");
final org.w3c.dom.Node SCHEDULE_NODE = expr.evaluate(doc, XPathConstants.NODE)

println(String.format(
    "  Changing Build Triggers → Build periodically → Schedule from '%s' to ' %s'",
    SCHEDULE_NODE.getTextContent(), CRON_SCHEDULE))
SCHEDULE_NODE.setTextContent(CRON_SCHEDULE)

import javax.xml.transform.*
import javax.xml.transform.dom.*
import javax.xml.transform.stream.*

println("  Writing ${DOWNSTREAM_CONFIG}")
Transformer transformer = TransformerFactory.newInstance().newTransformer()
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
transformer.transform(new DOMSource(doc), new StreamResult(new File(DOWNSTREAM_CONFIG)))

println("  ${SEPARATOR} Adapted configuration of ${DOWNSTREAM_NAME} ${SEPARATOR}")

 

2) Groovy Postbuild D reset cron config.xml:

  • Build
    • [ , ]
      • Command

 

#!/bin/bash
echo "  This script is a placeholder to represent a build step"
    • Groovy Postbuild
      • Groovy Script

 

// From: How to delay Jenkins job until a certain time was reached?
//       http://stackoverflow.com/questions/27952216/1744774

// -----------------------------------------------------------
// Adapt these according to your environment
final String THIS_JOB_NAME = 'SO-27952216-Downstream-job'
final String CRON_SCHEDULE = ''
// -----------------------------------------------------------

final Object LOG = manager.listener.logger
final String SEPARATOR = new String(new char[8]).replace('\0', '-')
LOG.println("  ${SEPARATOR} Adapting configuration of ${THIS_JOB_NAME} ${SEPARATOR}")

import jenkins.model.*
import hudson.model.*

final Project THIS_JOB = Jenkins.instance.getItem(THIS_JOB_NAME)
final String THIS_JOB_CONFIG = THIS_JOB.getRootDir().getPath() + "/config.xml"  

//import hudson.triggers.*
//THIS_JOB.getTrigger(TimerTrigger.class).spec = CRON_SCHEDULE
// leads to:
//   ERROR: Build step failed with exception groovy.lang.ReadOnlyPropertyException: 
//       Cannot set readonly property: spec for class: hudson.triggers.TimerTrigger

import org.w3c.dom.*;
import javax.xml.parsers.*
import javax.xml.xpath.*

LOG.println("  Reading ${THIS_JOB_CONFIG}")
Document doc = 
    DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(THIS_JOB_CONFIG)
XPathExpression expr = XPathFactory.newInstance().newXPath()
    .compile("/project/triggers/hudson.triggers.TimerTrigger/spec")
final org.w3c.dom.Node SCHEDULE_NODE = expr.evaluate(doc, XPathConstants.NODE)

LOG.println(String.format(
    "  Changing Build Triggers → Build periodically → Schedule from '%s' to ' %s'",
    SCHEDULE_NODE.getTextContent(), CRON_SCHEDULE))
SCHEDULE_NODE.setTextContent(CRON_SCHEDULE)

import javax.xml.transform.*
import javax.xml.transform.dom.*
import javax.xml.transform.stream.*

LOG.println("  Writing ${THIS_JOB_CONFIG}")
Transformer transformer = TransformerFactory.newInstance().newTransformer()
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
transformer.transform(new DOMSource(doc), new StreamResult(new File(THIS_JOB_CONFIG)))

LOG.println("  ${SEPARATOR} Adapted configuration of ${THIS_JOB_NAME} ${SEPARATOR}")

, println() Jenkin Console Output .

: ! [JENKINS-18651 - post build script] it manager.listener.logger.println() println().


, , :

  • A, B, C D config.xml, D ; .
    • config.xml, .
    • script config.xml, → , , "" :
      qgQSe.png.
  • A B C config.xml , D, ; .
  • A, B, C D config.xml Jenkins TimerTrigger ( ) ; - , .

, upstream:

  • Groovy :
    http://jenkins/userContent/scripts/JobCronScheduleConfigurator.groovy
  • :
    • Build
      • ()
        • Builder
          • ◉ Groovy script
            http://jenkins/userContent/scripts/JobCronScheduleConfigurator.groovy

Groovy Postbuild Groovy script, ( ).

+2

All Articles