Using Azure Pipelines to deploy a gatsby site on a schedule
Tags: gatsby, devops, azurepipelines, netlify
Published: 2020-01-09 03:26:54

---
title: Using Azure Pipelines to deploy a gatsby site on a schedule
published: true
date: 2020-01-08 00:00:00 UTC
tags: gatsby, devops, azure pipelines, netlify
canonical_url: https://www.ankursheel.com/blog/using-azure-pipelines-to-deploy-a-gatsby-site-on-a-schedule
description: Azure pipelines can be triggered on a schedule. Let's see how we can use this feature to deploy our gatsby site to netlify on a daily basis and have scheduled posts automatically go live.
cover: https://thepracticaldev.s3.amazonaws.com/i/cnb7ajpltd9gv3ap2ysa.jpg
---

In an [earlier post](https://www.ankursheel.com/blog/deploying-a-gatsby-site-to-netlify-using-azure-pipelines), 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](https://github.com/AnkurSheel/BlogFunctions).
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](https://www.ankursheel.com/blog/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_.

```yaml
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](https://docs.microsoft.com/en-us/azure/devops/pipelines/build/triggers?view=azure-devops&tabs=yaml#supported-cron-syntax)
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 schedulingposts a few days in advance.

## Conclusion

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