Automating Daily Site Rebuilds
Steps #
-
Create a new project at https://script.google.com/.
-
Paste one of the scripts below into
Code.gs
.
a. Simple Daily
b. Complex Daily
c. Complex Generic -
Modify as desired with URLs/webhooks from Vercel/Netlify etc.
-
Test code by running dailyCron function from editor (will be prompted to authenticate on the first time).
-
Go to Triggers on the left sidebar (has the clock icon).
-
Select Add Trigger and modify the settings based on the image below:
-
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 #
- Script runtime: 6 min/execution (all accounts)
- URL Fetch calls: 20,000/day (normal Google accounts), 100,000/day (Google Workspace accounts)
- Source: https://developers.google.com/apps-script/guides/services/quotas