Project

General

Profile

Actions

Create a random crontab

Within drone it is not possible to create a cronjob for a repository in a random manner. If you select
@weeky or @daily these jobs will be all started on midnight on the specific date. If you only
have so much computing power available (for example in a kubernetes cluster) this can create some
serious problems. To overcome this problem, the following script creates a cronjob at a random time
and random day (in case of a weekly crontab).

Requirements:

  • installed and configured drone cli tool
  • linux
  • bash

`
#!/bin/bash

OCCURANCE=$1
REPO=$2
BRANCH=$3
CNAME=$4

if [ -z $CNAME ]; then
echo "Usage: droneCreateRandomCron.sh "
echo -e "\t occurance = 1 = Daily | 0 = Weekly"
exit 1
fi

HOUR=$(($RANDOM % 24))

if [ $1 -eq 1 ]; then

DAY='*'

else

DAY=$(($RANDOM % 6))

fi

CRONTAB="0 0 ${HOUR} * * ${DAY}"
echo "Crontab: ${CRONTAB}"

drone cron add --branch $BRANCH $REPO $CNAME "${CRONTAB}"
`

Tags:

Updated by Dominik Meyer 26 days ago · 1 revisions