Deploying a Shopify App to Google Cloud Run: A Step-by-Step Guide
Tags: shopify, gcp, programming, webdev
Published: 2024-03-03 19:28:35

![Deploy Shopify App to Cloud Run](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubzj5m55cedb9xtcr7c4.png)

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"]
```

### 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
```

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmt6ahq08boxalffqanu.png)

- 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
```

- After retrieving the env, you can add it to Cloud Run settings


![Cloud Run environment variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obpzt1up2r28yirnui6o.png)

### 3.2. Deploying using Cloud Code

[Cloud Code](https://cloud.google.com/code/docs) is an IDE extension where you can deploy the current project with just a few clicks

![Cloud Code extension](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d2h7chimtt89ud8v85j5.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xihixprvv5per56fm5g.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cgxvu9h394ld191rf0kz.png)

## 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:

- Google Cloud Run Documentation: https://cloud.google.com/run/docs
- Deploying a Shopify Node App Docker Image to GCP: https://shopify.dev/docs/apps/deployment/web

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.