How to Use GitHub Actions to Automatically Alert Google When Your Sitemap Changes

3/7/2021

How To

This “How To” assumes you already have a repository (repo) on GitHub, and have access to GitHub Actions. It is also primarily designed with Jekyll sites hosted on GitHub in mind, however these same steps should work for any site where your actual published site content is tightly coupled with the code in your GitHub repo.

1. GitHub Actions Folder Setup

If you already have any GitHub Actions “workflows” setup you can skip this step.

If not:

  1. Create a .github folder at the root (top level) of your repo
  2. Within the .github folder you just created, create a workflows folder

2. Create GitHub Actions YAML

GitHub Actions (or probably more accurately “workflows”) are controlled by YAML files (.yml or .yaml). Within .github/workflows/ create a new YAML file for the workflow. You can title it whatever you like. (Mine is titled: ping_google_about_sitemap.yml.)

Paste the following code into your newly created YAML file:

# YAML

# You can name this whatever you want:
name: Ping Google About Updated Sitemap

# Controls when the action will run:
on:
  # Triggers the workflow on push events, only on the master branch, and only
  # if sitemap.xml was updated:
  push:
    # If your primary ("production") branch is "main" (or anything else) instead
    # of "master" you will need to update this line:
    branches: [ master ]
    # TODO: Update the filepath below to be the filepath to the live /
    # production / rendered sitemap.xml in your GitHub repo:
    paths:
      - PATH/TO/YOUR/sitemap.xml

  # This allows you to run this workflow manually from the Actions tab in your
  # GitHub repo:
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in
# parallel:
jobs:
  # This workflow contains a single job called "ping" (the ID for this job):
  ping:
    # Again this can be whatever name you like:
    name: Ping Google
    # The type of runner that the job will run on:
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the
    # job:
    steps:
      # Runs a single command using the runners shell:
      - name: Send HTTP GET request
        # TODO: Update the part of the below URL AFTER `?sitemap=` to be the
        # URL of the published sitemap.xml on your site (NOT in your repo).
        # For example, if your sitemap is at https://www.example.com/sitemap.xml
        # the final command run would be:
        # `curl http://www.google.com/ping?sitemap=https://www.example.com/sitemap.xml`
        run: curl http://www.google.com/ping?sitemap=FULL_URL_OF_YOUR/sitemap.xml

The above code is heavily commented to explain what it’s doing. You can ignore most of this if you want, but importantly you MUST at a minimum update the two parts of the code marked by the # TODO: comments. These are the places in the code where you need to specify the path and URL to your sitemap respectively.

Finally, save the file.

3. Commit YAML File to GitHub

Commit your newly created and edited workflow YAML file to the primary (e.g. master) branch of your repo.

Once this is done, GitHub should automatically find the file and set up your GitHub Action.

4. Test Your Action

Go to the “Actions” tab on your GitHub repo and verify that you see the action you just committed. You should see it in the “Workflows” list toward the left of the screen:

GitHub Actions Workflows

If you used the setup above, you’ll be able to manually trigger the workflow / action to run:

Manually Run Workflow

Give that a try now. If everything was setup correctly, it should run for a little bit and then give you a nice, happy, green check-mark indicating that it ran successfully:

Successful Run Example

Finally, you can test it by actually making a change to your sitemap.xml and commiting that. Again, you can go the “Actions” tab and see the result of the workflow / action (which, if you’ve made it this far, should be another happy, green check-mark).

Tested On

Sources

  1. https://docs.github.com/en/actions/quickstart
  2. https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions

Like This?

for More in the Future

with Comments / Questions / Suggestions