How to delay a job in a CI/CD gitlab pipeline until a certain time?

Viewed 102
2 Answers

You could approximate this with a dynamic child pipeline. The job that generates the configuration can calculate how much time is inbetween the current time and the desired deploy time and embed that in the generated config for the start_in parameter.

As an example:

create_deploy_pipeline:
  stage: build
  script:
  # modify this line or write a script that suits your needs
  # here, we calculate the number of seconds between now and "5AM tomorrow"
  - seconds_until_deploy=$(( $(date +%s -d "tomorrow 05:00") - $( date +%s ) ))
  - |
    cat > dynamic.yml << EOF
    deploy_job:
      script:
        - ./deploy.sh
      when: delayed
      start_in: ${seconds_until_deploy} seconds
    EOF
artifacts:
    paths:
      - dynamic.yml


deploy-pipeline:
  stage: test
  trigger:
    include:
      - artifact: dynamic.yml
        job: create_deploy_pipeline

Keep in mind that you will need to consider the timezone used by your runner (or calculate using UTC time) to get an accurate calculation.

What you are probably looking for is a scheduled pipeline. Scheduled pipelines benefit from execution based on cron clauses. You can limit the execution of your complete pipeline definition on a step by step basis like this.

EDIT: Avoiding duplicate pipeline runs has been documented here. If you tag your deployments you could build a command like this:

git log --oneline $(git describe --tags --abbrev=0 @^)..@

This will tell you if there are any commits since the last tag. This could be a Job Control feature to figure out, whether to run again or not.

Related