placeholderfeatureplaceholdersliderplaceholderthumb

Automating Daily Site Rebuilds

by Patrick Chong

Steps #

  1. Create a new project at https://script.google.com/.

  2. Paste one of the scripts below into Code.gs.
    a. Simple Daily
    b. Complex Daily
    c. Complex Generic

  3. Modify as desired with URLs/webhooks from Vercel/Netlify etc.

  4. Test code by running dailyCron function from editor (will be prompted to authenticate on the first time).

  5. Go to Triggers on the left sidebar (has the clock icon).

  6. Select Add Trigger and modify the settings based on the image below:
    Daily Cron trigger settings

  7. Hit Save and you’re done!

Simple Daily #

let dailyList = ['<insert url>']

function dailyCron() {
dailyList.forEach((url) => {
let options = {
method: 'get',
}
let response = UrlFetchApp.fetch(url, options)
Logger.log({ url, response }) // log the URL and the response
})
}

Complex Daily #

let dailyList = [
{
url: '<insert url>',
options: {
method: 'get',
},
},
{
url: '<insert url>',
options: {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify({
message: 'hello world',
}),
},
},
]

function dailyCron() {
dailyList.forEach((item) => {
let response = UrlFetchApp.fetch(item.url, item.options)
Logger.log({ url: item.url, response })
})
}

Complex Generic #

let dailyList = [
{
url: '<insert url>',
options: {
method: 'get',
},
},
{
url: '<insert url>',
options: {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify({
message: 'hello world',
}),
},
},
]

let monthlyList = [
/* similar structure to dailyList */
]

function dailyCron() {
cronHelper(dailyList)
}

function monthlyCron() {
cronHelper(monthlyList)
}

function cronHelper(list) {
list.forEach((item) => {
let response = UrlFetchApp.fetch(item.url, item.options)
Logger.log({ url: item.url, response })
})
}

Notes #

  • Inspired by Trigger a Netlify Build Every Day with IFTTT. Created the code in google-apps-script so that it’s more flexible/extensible.

  • If the desire is to save the results, then creating the script in connection to Google Sheets would work better.

  • Function is named dailyCron/monthlyCron after the Linux cron utility

  • An webtool that works for simple use cases is: https://cron-job.org/en/

Limitations of google-apps-script #