Skip to content

Instantly share code, notes, and snippets.

@matthewcotton
Last active June 18, 2024 15:44
Show Gist options
  • Save matthewcotton/470dbb20bd1f92ed8a842d936f94811d to your computer and use it in GitHub Desktop.
Save matthewcotton/470dbb20bd1f92ed8a842d936f94811d to your computer and use it in GitHub Desktop.
GitLab CI/CD Shopify Theme Deployment Files
FROM node:18.16.0-alpine3.18
RUN apk add --no-cache ruby ruby-dev make gcc g++ linux-headers && \
gem install bundler && \
npm install -g @shopify/cli @shopify/theme
VOLUME ["/project"]
WORKDIR /project
stages:
- review
- staging
- uat
- prep_deploy
- deploy
- finalise_deploy
- notify
- cleanup
default:
image: shopify-cli:latest
cache: &global_cache
key: "$CI_COMMIT_REF_SLUG"
paths:
- shopify-deployment
policy: pull-push
review:
stage: review
script:
- echo "Contents of root directory before actions:"
- ls -la
- echo "Contents of shopify-deployment before actions:"
- ls -la shopify-deployment || true
- shopify version
- mkdir -p ./shopify-deployment
- chmod +x ./shopify-deployment-script.sh; ./shopify-deployment-script.sh
- echo "Contents of shopify-deployment after actions:"
- cat ./shopify-deployment/deployment-data
- DYNAMIC_ENVIRONMENT_URL=$(grep -o '"preview_url":"[^}]*' ./shopify-deployment/deployment-data | grep -Eo '(http|https)://[^"]+')
- echo "DYNAMIC_ENVIRONMENT_URL=$DYNAMIC_ENVIRONMENT_URL" >> deploy.env
artifacts:
reports:
dotenv: deploy.env
environment:
name: review/$CI_COMMIT_REF_NAME
url: $DYNAMIC_ENVIRONMENT_URL
on_stop: stop_review
cache:
<<: *global_cache
only:
- branches
except:
- staging
- uat
- main
staging:
stage: staging
script:
- echo "Contents of root directory before actions:"
- ls -la
- echo "Contents of shopify-deployment before actions:"
- ls -la shopify-deployment || true
- shopify version
- mkdir -p ./shopify-deployment
- echo "Deploying updates to Shopify Staging theme:"
- shopify theme push --theme $STAGING_THEME_ID --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN --json > ./shopify-deployment/deployment-data
- echo "Contents of shopify-deployment after actions:"
- cat ./shopify-deployment/deployment-data
- DYNAMIC_ENVIRONMENT_URL=$(grep -o '"preview_url":"[^}]*' ./shopify-deployment/deployment-data | grep -Eo '(http|https)://[^"]+')
- echo "DYNAMIC_ENVIRONMENT_URL=$DYNAMIC_ENVIRONMENT_URL" >> deploy.env
artifacts:
reports:
dotenv: deploy.env
environment:
name: review/$CI_COMMIT_REF_NAME
url: $DYNAMIC_ENVIRONMENT_URL
cache:
<<: *global_cache
only:
- staging
uat:
stage: uat
script:
- echo "Contents of root directory before actions:"
- ls -la
- echo "Contents of shopify-deployment before actions:"
- ls -la shopify-deployment || true
- shopify version
- mkdir -p ./shopify-deployment
- echo "Deploying updates to Shopify UAT theme:"
- shopify theme push --theme $UAT_THEME_ID --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN --json > ./shopify-deployment/deployment-data
- echo "Contents of shopify-deployment after actions:"
- cat ./shopify-deployment/deployment-data
- DYNAMIC_ENVIRONMENT_URL=$(grep -o '"preview_url":"[^}]*' ./shopify-deployment/deployment-data | grep -Eo '(http|https)://[^"]+')
- echo "DYNAMIC_ENVIRONMENT_URL=$DYNAMIC_ENVIRONMENT_URL" >> deploy.env
artifacts:
reports:
dotenv: deploy.env
environment:
name: review/$CI_COMMIT_REF_NAME
url: $DYNAMIC_ENVIRONMENT_URL
cache:
<<: *global_cache
only:
- uat
generate_version:
stage: prep_deploy
image: alpine:latest
rules:
- if: $CI_COMMIT_TAG
when: never
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- echo "RELEASE_TAG = $(date '+%Y%m%d.%H%M')" >> variables.env
artifacts:
reports:
dotenv: variables.env
expire_in: 1 week
deploy:
stage: deploy
script:
- echo "Contents of root directory before actions:"
- ls -la
- echo "Contents of shopify-deployment before actions:"
- ls -la shopify-deployment || true
- shopify version
- mkdir -p ./shopify-deployment
- shopify theme push --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN --live --allow-live --json > ./shopify-deployment/deployment-data
- echo "Contents of shopify-deployment after actions:"
- cat ./shopify-deployment/deployment-data
- ID=$(grep -o '"id":[^"]*' ./shopify-deployment/deployment-data | grep -o '[^:]*$' | grep -o '^[^,]*')
- PREVIEW_URL=$(grep -o '"preview_url":"[^}]*' ./shopify-deployment/deployment-data | grep -Eo '(http|https)://[^"]+')
- DYNAMIC_ENVIRONMENT_URL=${PREVIEW_URL}?preview_theme_id=${ID}
- echo "DYNAMIC_ENVIRONMENT_URL=$DYNAMIC_ENVIRONMENT_URL" >> deploy.env
artifacts:
reports:
dotenv: deploy.env
environment:
name: live
url: $DYNAMIC_ENVIRONMENT_URL
cache:
<<: *global_cache
only:
- main
release:
stage: finalise_deploy
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
- if: $CI_COMMIT_TAG
when: never
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- echo "Running release $RELEASE_TAG..."
release:
name: 'Release $RELEASE_TAG'
tag_name: '$RELEASE_TAG'
description: '$RELEASE_TAG'
ref: '$CI_COMMIT_SHA'
artifacts:
expire_in: 1 week
stop_review:
stage: cleanup
script:
- ./teardown-environment
- dep ci:destroy cd --revision=$CI_COMMIT_SHA
when: manual
environment:
name: review/$CI_COMMIT_REF_NAME
action: stop
only:
- branches
except:
- main
- uat
- staging
# IMPORTANT: Deployment results files (e.g. deployment-data) cannot use the file type '.json' as Shopify will interpret these as being theme files and the theme deployment will fail
# When the deployment-data file already exisits (loaded from the cache) then try to redeploy to this theme
if [ -f ./shopify-deployment/deployment-data ]
then
# Get the theme id from the deployment-data json file
ID="$(grep -o '"id":[^"]*' ./shopify-deployment/deployment-data | grep -o '[^:]*$' | grep -o '^[^,]*')"
echo "Deploying updates to Shopify development theme with id: $ID"
echo "shopify theme push --theme $ID --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN > ./shopify-deployment/result"
shopify theme push --theme $ID --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN > ./shopify-deployment/result
# If the redeployment output includes the word 'Error' then deploy a development new theme
ERROR="$(grep -w "Error" ./shopify-deployment/result)"
if [ ! -z "$ERROR" ]
then
echo "WARNING: There was an error with the deployemnt. This is probably due to this Shopify development theme being inactive for more than 7 days. The theme will now be redeployed. This will result in the Shopify development theme having a new id and url."
echo "shopify theme push --development --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN --json > ./shopify-deployment/deployment-data"
shopify theme push --development --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN --json > ./shopify-deployment/deployment-data
fi
rm -f ./shopify-deployment/result
# When the deployment-data file doesn't exisit deploy a new development theme
else
echo "Deploying a new Shopify development theme:"
echo "shopify theme push --development --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN --json > ./shopify-deployment/deployment-data"
shopify theme push --development --store $SHOPIFY_FLAG_STORE --password $SHOPIFY_CLI_THEME_TOKEN --json > ./shopify-deployment/deployment-data
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment