Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save TheRealFlyingCoder/773bf60f433ccbdbad8c296a99fb3738 to your computer and use it in GitHub Desktop.
Save TheRealFlyingCoder/773bf60f433ccbdbad8c296a99fb3738 to your computer and use it in GitHub Desktop.
Github Actions: Remix + Cloud Run + Docker
So Github workflow for deploying a docker image to GCR, and subsequently pushing that to a cloud run instance is pretty easy
First just add your github tokens:
GCP_SERVICE_ID
GCP_PROJECT_ID
GCP_REGION <-- your services deploy region
GCP_SA_KEY <-- Follow the method in here (http://acaird.github.io/computers/2020/02/11/github-google-container-cloud-run)
Take note that my Dockerfile is specifically for an Express.js remix app, where Remix builds to `/server/build`.
And my express file is at `server/index.js`
name: 🚀 Deploy
on:
push:
branches:
- main
jobs:
build:
name: 🐳 Build
if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
runs-on: ubuntu-latest
# only build/deploy main branch on pushes
steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.6.0
with:
access_token: ${{ secrets.GITHUB_TOKEN }}
- name: ⬇️ Checkout repo
uses: actions/checkout@v2
- name: Setup gcloud
uses: google-github-actions/setup-gcloud@master
with:
service_account_key: ${{ secrets.GCP_SA_KEY }}
project_id: ${{ secrets.GCP_PROJECT_ID }}
export_default_credentials: true
- name: Configure docker for GCP
run: gcloud auth configure-docker
- name: 🐳 Set up Docker Buildx
uses: docker/setup-buildx-action@v1
# Setup cache
- name: ⚡️ Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: 🐳 Docker build
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_SERVICE_ID }}:${{ github.sha }}
build-args: |
COMMIT_SHA=${{ github.sha }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,mode=max,dest=/tmp/.buildx-cache-new
# This ugly bit is necessary if you don't want your cache to grow forever
# till it hits GitHub's limit of 5GB.
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
deploy:
name: 🚀 Deploy
runs-on: ubuntu-latest
needs: [build]
# only build/deploy main branch on pushes
if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
steps:
- name: 🛑 Cancel Previous Runs
uses: styfle/cancel-workflow-action@0.9.1
with:
access_token: ${{ github.token }}
- name: ⬇️ Checkout repo
uses: actions/checkout@v2
- name: Deploy to Cloud Run
uses: google-github-actions/deploy-cloudrun@main
with:
service: ${{ secrets.GCP_SERVICE_ID }}
image: gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.GCP_SERVICE_ID }}:${{ github.sha }}
credentials: ${{ secrets.GCP_SA_KEY }}
region: ${{ secrets.GCP_REGION }}
# base node image
FROM node:16-bullseye-slim as base
# Install all node_modules, including dev dependencies
FROM base as deps
RUN mkdir /app
WORKDIR /app
ADD package.json package-lock.json ./
RUN npm install --production=false
# Setup production node_modules
FROM base as production-deps
RUN mkdir /app
WORKDIR /app
COPY --from=deps /app/node_modules /app/node_modules
ADD package.json package-lock.json ./
RUN npm prune --production
# Build the app
FROM base as build
RUN mkdir /app
WORKDIR /app
COPY --from=deps /app/node_modules /app/node_modules
ADD . .
RUN npm run build
# Finally, build the production image with minimal footprint
FROM base
ENV NODE_ENV=production
RUN mkdir /app
WORKDIR /app
COPY --from=production-deps /app/node_modules /app/node_modules
#My build goes to /app/server/build and i'm running /server/index.js express
COPY --from=build /app/server /app/server
COPY --from=build /app/public /app/public
ADD . .
CMD ["npm", "run", "start"]
@shanewwarren
Copy link

This looks great thanks for sharing this! Sorry if this is a dumb question, but where does GCP_SERVICE_ID come from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment