In an earlier post, I showed how to use azure pipelines to build a gatsby site and deploy to netlify. But it was slightly limited. A live build was deployed only when a push was made to master. There was no way to have scheduled posts go live automatically.
There are 2 ways to approach this problem.
- By using an AWS Lambda or Azure Functions. This is what I had initially and you can see the code here.
- By adding a new azure pipeline which runs on a schedule.
In this post, I will be using azure pipelines to deploy the site daily.
Note: We will be using the template files created in the Deploying a Gatsby site to Netlify using Azure Pipelines so I would suggest reading that first. Go on, I will wait.
Adding a new pipeline to schedule posts
Good, you are back.
Let’s create a new file azure-pipelines.daily.yml.
name: 'Daily Release' schedules: - cron: '0 14 * * *' displayName: Daily build branches: include: - master always: true trigger: batch: false branches: exclude: - '*' pr: none variables: - template: azure-pipelines.vars.yml jobs: - template: azure-pipelines.common.yml parameters: deployScriptParams: '--prod' netlifySiteId: $(netlify.siteId) netlifyToken: $(netlify.token) message: 'Daily build'
- name: The name for this pipeline.
- schedules: The schedule on which the pipeline should run.
- cron: The cron syntax defining the schedule.
- displayName: The display name for the schedule.
- branches: Include only the master branch so that only the master branch is built and deployed.
- always: true: Always run the pipeline.
- trigger: Conditions for running the pipeline for when a push is made.
- branches: Exclude all branches so that the build is not triggered for commits on any branch including master.
- pr: none: Disable triggering the pipeline for when a PR is opened or changes pushed to an open PR. master.
- variables: We set a template file to get the environment variables.
- jobs: The jobs to run.
- template: Use the job template created in azure-pipelines.common.yml.
- parameters: The parameters to be passed to the job template
- netlifySiteId: The site ID from environment variables.
- netlifyToken: The token from environment variables.
- message: The message is hardcoded to Daily Build.
- The cron syntax is mm HH DD MM DW. You can find more details about the supported syntax here
- The timezone for schedules is UTC so the build times need to be adjusted accordingly.
- Don’t forget to set
always: true. If you have it set to false, a build might not be triggered if you are scheduling posts a few days in advance.
With this setup, I can write posts and schedule them to go live in the future.