Using Azure Pipelines to deploy a gatsby site on a schedule

This entry was posted on
  • devops
  • tutorial
  • gatsby
  • azure pipelines

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.

  1. By using an AWS Lambda or Azure Functions. This is what I had initially and you can see the code here.
  2. 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'
  1. name: The name for this pipeline.
  2. schedules: The schedule on which the pipeline should run.
    1. cron: The cron syntax defining the schedule.
    2. displayName: The display name for the schedule.
    3. branches: Include only the master branch so that only the master branch is built and deployed.
    4. always: true: Always run the pipeline.
  3. trigger: Conditions for running the pipeline for when a push is made.
    1. branches: Exclude all branches so that the build is not triggered for commits on any branch including master.
  4. pr: none: Disable triggering the pipeline for when a PR is opened or changes pushed to an open PR. master.
  5. variables: We set a template file to get the environment variables.
  6. jobs: The jobs to run.
    1. template: Use the job template created in azure-pipelines.common.yml.
  7. parameters: The parameters to be passed to the job template
    1. netlifySiteId: The site ID from environment variables.
    2. netlifyToken: The token from environment variables.
    3. message: The message is hardcoded to Daily Build.

Gotchas

  1. The cron syntax is mm HH DD MM DW. You can find more details about the supported syntax here
  2. The timezone for schedules is UTC so the build times need to be adjusted accordingly.
  3. 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.

Conclusion

With this setup, I can write posts and schedule them to go live in the future.

More like this