如何通过app.json配置Heroku Scheduler脚本与执行频率实现一键部署?
Short answer: No, you can't directly define Scheduler tasks (script name + frequency) through the addons field in app.json—the addons config only enables the Scheduler add-on for your app, it doesn't handle task-specific settings. But there's a reliable workaround to auto-configure tasks when using the "Deploy to Heroku" button.
The Workaround: Use Postdeploy Scripts
You can leverage Heroku's postdeploy script (defined in app.json) to automatically create Scheduler tasks right after deployment completes. Here's how to set this up:
1. Update Your app.json
Add the Scheduler add-on and define a postdeploy script that creates your task. You have two options for this script: using the Heroku CLI, or calling the Heroku API directly (more reliable, no CLI dependency).
Option A: Using Heroku CLI
{ "name": "Your App", "description": "Auto-configured with Heroku Scheduler", "addons": [ { "plan": "scheduler:standard" } ], "scripts": { "postdeploy": "heroku scheduler:add --command 'node path/to/your-script.js' --frequency 'daily' --time '09:00' --app $HEROKU_APP_NAME" }, "env": { "HEROKU_APP_NAME": { "description": "Auto-set by Heroku during deployment", "required": true } } }
- The
$HEROKU_APP_NAMEvariable is automatically populated by Heroku during the deployment process. - Adjust
--commandto match your script (e.g.,python script.pyfor Python apps),--frequency(options:hourly,daily,every X minutes), and--time(for daily tasks, specify a UTC time like09:00).
Option B: Using Heroku API (No CLI Dependency)
If you want to avoid relying on the Heroku CLI being available in the build environment, use a curl call to the Heroku API:
{ "name": "Your App", "description": "Auto-configured with Heroku Scheduler", "addons": [ { "plan": "scheduler:standard" } ], "scripts": { "postdeploy": "curl -n -X POST https://api.heroku.com/apps/$HEROKU_APP_NAME/scheduler/jobs \ -H \"Content-Type: application/json\" \ -H \"Accept: application/vnd.heroku+json; version=3\" \ -d '{ \"command\": \"node path/to/your-script.js\", \"schedule\": \"daily\", \"time_zone\": \"UTC\" }'" }, "env": { "HEROKU_APP_NAME": { "description": "Auto-set by Heroku during deployment", "required": true } } }
- For more granular scheduling, replace
"schedule": "daily"with a cron expression (e.g.,"0 9 * * *"for daily at 9 UTC). - The
-nflag tells curl to use Heroku's built-in credentials in the deployment environment, so you don't need to add API keys manually.
2. Key Notes
- Multiple Tasks: To add multiple Scheduler tasks, just chain commands in the
postdeployscript (separate with&&). - Script Paths: Double-check your script's relative path—make sure it matches where the file lives in your deployed app.
- Testing: Before sharing the "Deploy to Heroku" button, test the postdeploy script manually by running it in a local Heroku environment to confirm tasks are created correctly.
Why app.json Can't Do This Directly
Heroku's addons configuration in app.json only handles enabling the add-on itself, not configuring its specific resources (like Scheduler tasks). Task definitions are tied to your app's runtime, not the add-on's initial provisioning.
内容的提问来源于stack exchange,提问作者tuck1s

