Never lose track of your Shopify theme changes: automated Shopify theme backups with GitHub Actions

Shopify allows you to edit the theme code and download the theme package again. It also let’s you restore older versions of individual theme files. But over time it’s not clear which changes were made, especially if you are not the only one who is editing the theme. Therefore I thought it would be nice to version the theme in a Git repository – but that doesn’t seem to be possible out-of-the-box.

But then there’s scheduled GitHub Actions (similar to cron jobs) and a Shopify CLI to access the shop from a terminal. So with some experimentation I could figure out a script that will pull the remote Shopify theme and push it into a the GitHub branch which has this action. You might find this action useful:

# Pulls Shopify theme and pushes it to `main` branch. To setup please proceed as described:
#
# 1. Create private Shopify app
# 2. Allow read permission for theme
# 3. Provide the secrets in GitHub under `Settings / Secrets / Actions` (password is `Admin-API / Password` from the Shopify app
#
# For some reason the password from the `Theme Kit Access` app by Shopify doesn't work, but would be a better approach.

name: Pull Shopify Theme

on:
  schedule:
    - cron: "0 3 * * *"

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  pull:
    name: Backup
    runs-on: ubuntu-latest
    env:
      # Run Shopify CLI in CI mode, see https://github.com/Shopify/shopify-cli/issues/1582#issuecomment-973322889
      CI: 1
      SHOPIFY_SHOP: ${{ secrets.SHOPIFY_SHOP }} # storename w/o `.myshopify.com`
      SHOPIFY_PASSWORD: ${{ secrets.SHOPIFY_APP_PASSWORD }}
    steps:
      - name: Install
        run: sudo gem install shopify-cli

      - name: Checkout
        uses: actions/checkout@v2

      - name: Pull theme
        run: |
          shopify login
          shopify theme pull --live
      - name: Check for changes and apply
        run: |
          git config --global user.name 'YOUR BOT NAME'
          git config --global user.email 'SOME E-MAIL'
          {
            git add --all && git commit -m "feat: remote from `date`" && git push && printf "\n" && echo "::notice::? Updated with latest from remote Shopify theme"
          } || {
            printf "\n" && echo "::notice::? There are no updates in the remote Shopify theme"
          }

This is a GitHub action that has to be added to a repository that also holds the theme code. You can create a repository with a fresh theme code base and the run this action. Please note that you have to set the GitHub Secrets as described in the comments / code and update the git user name and email placeholders. The script will run once a day at 3 am.


Photo by Kevin Ku from PexPhoto by Markus Spiske from Pexels