Deploying a Shopify App to Google Cloud Run: A Step-by-Step Guide

Dale Nguyen - Mar 3 - - Dev Community

Deploy Shopify App to Cloud Run

While Heroku and Fly are popular choices for Shopify app deployment, Google Cloud Run offers a compelling alternative. Cloud Run provides a serverless platform that scales automatically, eliminates server management headaches, and integrates seamlessly with other Google Cloud services.

Here’s a guide to deploying your Shopify App to Cloud Run:

Prerequisites

  • A Google Cloud Project with billing enabled.
  • A Shopify App created and configured. -Basic understanding of Docker and containerization.

Steps:

1. Create a Dockerfile:

  • Define the environment for your app, including dependencies and Shopify API credentials (stored securely using secrets management).

  • Specify the command to start your app.

If you are using a template from Shopify, the Dockerfile is already created for you. For example:



FROM node:18-alpine

EXPOSE 3000

WORKDIR /app
COPY . .

ENV NODE_ENV=production

RUN npm install --omit=dev
# Remove CLI packages since we don't need them in production by default.
# Remove this line if you want to run CLI commands in your container.
RUN npm remove @shopify/app @shopify/cli
RUN npm run build

# You'll probably want to remove this in production, it's here to make it easier to test things!
RUN rm -f prisma/dev.sqlite

CMD ["npm", "run", "docker-start"]


Enter fullscreen mode Exit fullscreen mode

2. Build and Push Docker Image:

  • Use docker build to create an image based on your Dockerfile.

  • Tag and push the image to a container registry like Google Container Registry (GCR).



# if you using M1 chip: --platform linux/amd64 is needed 
# when building the docker image
docker buildx build --platform linux/amd64 -t amazing-app .

# tag the image - remember to replace REGION, PROJECT_ID, ARTIFACT_FOLDER
# to match with your project
docker tag amazing-app REGION-docker.pkg.dev/PROJECT_ID/ARTIFACT_FOLDER/amazing-app:1.3

docker push REGION-docker.pkg.dev/PROJECT_ID/ARTIFACT_FOLDER/amazing-app:1.3


Enter fullscreen mode Exit fullscreen mode

3.1 Set Up Cloud Run Service:

  • In the Google Cloud Console, navigate to Cloud Run services.

  • Click “Create Service” and choose a region for deployment.

  • Select “Container image” as the source and provide the image URI from the Container Registry or Artifact Registry.

  • Define scaling and resource allocation based on your app’s needs.

Create a new Cloud Run service

  • Configure environment variables for your app, including Shopify API keys and URLs.


# To retrieve the environment value, you can run the following command

npm run shopify app env show


Enter fullscreen mode Exit fullscreen mode
  • After retrieving the env, you can add it to Cloud Run settings

Cloud Run environment variables

3.2. Deploying using Cloud Code

Cloud Code is an IDE extension where you can deploy the current project with just a few clicks

Cloud Code extension

You don’t even have to build and push the docker image locally. Cloud Code will handle everything for you.

4. Update Shopify App Configuration:

  • In your Shopify Partner Dashboard, navigate to your app’s settings.

  • Update the “App webhook URLs” with the Cloud Run service URL.

Update Shopify App URL

5. Test and Deploy:

  • Test your app locally to ensure it functions correctly with Cloud Run’s environment variables.
  • Deploy the Cloud Run service and monitor logs for any errors.
  • Utilize the development App store to check if it works correctly or not

Amazing app served from Cloud Run

Additional Considerations:

  • Security: Implement proper authentication and authorization mechanisms for your app.
  • Monitoring: Set up monitoring and alerting to track app performance and identify issues.
  • Scaling: Cloud Run automatically scales based on traffic, but you can define custom scaling rules for better control.

Benefits of Deploying to Cloud Run:

  • Serverless: No need to manage servers, allowing you to focus on app development.
  • Scalability: Cloud Run automatically scales to meet traffic demands.
  • Cost-effective: You only pay for the resources your app uses.
  • Integration: Integrates seamlessly with other Google Cloud services.

Resources:

By following these steps and leveraging Cloud Run’s features, you can deploy your Shopify app efficiently and benefit from a scalable, cost-effective, and secure hosting solution.

. . . . . . . . . . . . . . . .