DEV Community

Nasrul Hazim Bin Mohamad
Nasrul Hazim Bin Mohamad

Posted on

Automating Netlify Deployments Every 24 Hours with GitHub Actions

In today's fast-paced development environment, automation is key to improving efficiency and reliability. If you're using Netlify to host your static sites, you may want to trigger deployments periodically or on-demand to ensure your site stays up to date. In this blog post, I'll show you how to automate Netlify deployments using GitHub Actions, triggering them every 24 hours and clearing the cache before each build.

What You’ll Need

Before we start, make sure you have the following:

  1. Netlify Site ID – You can find this in your Netlify dashboard under Site Settings > General > Site information > API ID.
  2. Netlify Personal Access Token – This token will allow GitHub Actions to trigger a deployment via the Netlify API. You can generate one under User Settings > Applications > Personal access tokens in Netlify.
  3. A GitHub repository connected to your Netlify site.

Why Automate Netlify Deployments?

Automating deployments brings several advantages:

  • Regular Updates: If your site fetches external content or depends on API data, automating deployments ensures the latest content is always reflected.
  • Cache Clearing: Netlify caches builds by default. Regularly clearing the cache can help prevent stale content or bugs caused by outdated builds.
  • Time-Saving: With GitHub Actions, you can easily automate tedious tasks and focus on more important aspects of your development.

Setting Up GitHub Actions for Netlify Deployments

Here's how to set up a GitHub Actions workflow that triggers a deployment to Netlify every 24 hours while clearing the cache.

Step 1: Add Secrets to GitHub

In your GitHub repository, you’ll need to store your Netlify credentials as secrets:

  1. Go to your repository on GitHub.
  2. Navigate to Settings > Secrets and variables > Actions.
  3. Add two new secrets:
    • NETLIFY_SITE_ID: The API ID for your Netlify site.
    • NETLIFY_ACCESS_TOKEN: Your Netlify personal access token.

Step 2: Create the GitHub Actions Workflow

Next, create a new GitHub Actions workflow in your repository. You can do this by adding a file to .github/workflows/netlify-deploy.yml:

name: Netlify Scheduled Deployment

on:
  schedule:
    - cron: '0 0 * * *' # This runs every 24 hours at midnight UTC
  workflow_dispatch: # Allows manual triggering of the action from GitHub Actions

jobs:
  deploy:
    if: github.ref == 'refs/heads/main' # Specify the branch 'main' to trigger on
    runs-on: ubuntu-latest

    steps:
      - name: Trigger Netlify Build with Cache Clearing
        run: |
          # Call the Netlify API to trigger a new build and clear the cache
          curl -X POST \
          -H "Content-Type: application/json" \
          -H "Authorization: Bearer ${{ secrets.NETLIFY_ACCESS_TOKEN }}" \
          -d '{"clear_cache": true}' \
          https://api.netlify.com/api/v1/sites/${{ secrets.NETLIFY_SITE_ID }}/builds
Enter fullscreen mode Exit fullscreen mode

Step 3: How It Works

  • Scheduled Trigger: The workflow is triggered automatically every 24 hours using the cron syntax '0 0 * * *'. This schedule corresponds to midnight UTC each day, but you can adjust it based on your needs.

  • Manual Trigger: The workflow_dispatch event allows you to manually trigger the deployment from the GitHub Actions tab whenever you need to deploy instantly.

  • Cache Clearing: The clear_cache: true flag ensures that the build cache is cleared before each deployment, avoiding any potential issues caused by outdated cache files.

Step 4: Test and Monitor Your Deployment

Once the workflow is set up, head over to the Actions tab in your GitHub repository to verify that the deployment works. You can manually run the workflow by clicking the Run workflow button if you want to test it right away.

Conclusion

With this simple automation, you can now rest assured that your Netlify site will be automatically deployed every 24 hours, with cache clearing included to prevent stale builds. GitHub Actions offers powerful automation tools, and with this setup, you can take advantage of it to streamline your deployment process.

Feel free to customize this workflow to suit your project’s needs and make deployment headaches a thing of the past!

Top comments (0)