How to handle complex Google Cloud Scheduler crons using Cloud Build

What's the use case?

A client wanted to schedule a cron job to run every first Wednesday of every month, which would usually have been possible if we used special characters like W,L etc. or a cron expression like “0 6 */31,1-7 * WED “

The challenge is that both of these options are currently not supported in Google Cloud Scheduler. 

So what can we do? First, let's cover some background context around the tools we'll be referencing, including crons and Google Cloud Scheduler, and then we'll dive into the solution. 

Background and context

Google Cloud Scheduler is used to schedule virtually any job, including batch, big data jobs, cloud infrastructure operations, and more. You can automate everything, including retries in case of a failure to reduce manual toil and intervention. 

Cloud Build executes your builds on Google Cloud infrastructure. Cloud Build can import source code from Cloud Storage, Cloud Source Repositories, GitHub, or Bitbucket, execute a build to your specifications, and produce artifacts, such as Docker containers or Java archives. 

App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. You can choose from several popular languages, libraries, and frameworks to develop your apps, and then let App Engine take care of provisioning servers and scaling your app instances based on demand.              

Furthermore, there are currently two types of crons available... 

1. Legacy-based, where you need an App Engine service running and you'll use a cron.yaml file to schedule the crons.

bhardwaju_0-1701823683253.png

2. The second type of cron available is the current generation Cloud Scheduler, where you need to provide the cron expression and select the http link or source to trigger it with a particular timezone. 

bhardwaju_1-1701823683312.png

Okay, now that we have a better understanding of the tools at hand, let's dive into the solution. 

Solution

It is currently not recommended to use the legacy-based cron job approach. This approach can also mean additional cost implications, since it needs an App Engine service running.

Our recommendation is to have another cron job scheduled to run to trigger a Cloud Build, which will run on every last week of the previous month and trigger the below inline yaml within a Cloud Build job.

cloudbuild.yaml 

 

steps:
  - id: testing
    name: gcr.io/google.com/cloudsdktool/cloud-sdk
    entrypoint: /bin/bash
    args:
    - -c
    - |
      firstwed=$(for i in {1..7} ; do     LC_ALL=C date '+%Y-%m-%d %a' -d "$(date +%Y%m0${i}) next month" ;   done | awk '/Wed/ {print $1}')
      crondate=$(cut -d "-" -f3 <<< "$firstwed")      
      gcloud scheduler jobs update http test --location=us-central1 --schedule="0 6 $crondate * *" --uri="https://hello-my4abq-uc.a.run.app/"
options:
  logging: CLOUD_LOGGING_ONLY

 


We hope you found this helpful. If you have any questions, please leave a comment below and we'd be happy to help. Thank you! 

Authored by:

  • Utkarsh Bhardwaj (@bhardwaju) Senior Solutions Consultant, Google
  • Abhishek Patil (@psabhishek), Cloud Migration Consultant, Google
  • Komal Bisht (@bishtkomal), Cloud Migration Consultant, Google
Version history
Last update:
‎12-05-2023 05:27 PM
Updated by: