Gitlab CI - Deploy to Google Cloud Run


Replace your-gcp-project-id, your-image-name, your-service-name, and your-region with appropriate values. If you want to require authentication to access the service, remove --allow-unauthenticated.

In your GitLab project settings, add the following environment variables:

  • GCP_SERVICE_ACCOUNT_KEY: A JSON key for your GCP service account with appropriate permissions to deploy to Google Cloud Run. To create one, follow the instructions here.

Example Dockerfile for a NextJS app

# Use the official Node.js image as the base image
FROM node:16-alpine AS builder

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package.json package-lock.json ./

# Install dependencies
RUN npm ci

# Copy the rest of the application code
COPY . .

# Build the application
RUN npm run build

# Use the official Node.js runtime image as the base image for the production environment
FROM node:16-alpine

# Set the working directory
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package.json package-lock.json ./

# Install only production dependencies
RUN npm ci --only=production

# Copy the build output from the builder stage
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public

# Set the environment variable for Next.js
ENV NODE_ENV=production

# Expose the default Next.js port
EXPOSE 3000

# Start the application
CMD ["npm", "start"]


.gitlab-ci.yml

stages:
  - build
  - deploy

variables:
  GOOGLE_PROJECT_ID: your-gcp-project-id
  IMAGE_NAME: your-image-name
  SERVICE_NAME: your-service-name
  GCP_REGION: your-region

build:
  stage: build
  image: docker:20.10.9
  services:
    - docker:20.10.9-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  script:
    - docker login -u _json_key --password-stdin https://gcr.io <<< "${GCP_SERVICE_ACCOUNT_KEY}"
    - docker build -t gcr.io/$GOOGLE_PROJECT_ID/$IMAGE_NAME:$CI_COMMIT_SHA .
    - docker push gcr.io/$GOOGLE_PROJECT_ID/$IMAGE_NAME:$CI_COMMIT_SHA
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

deploy:
  stage: deploy
  image: google/cloud-sdk:slim
  script:
    - echo "$GCP_SERVICE_ACCOUNT_KEY" > gcp-service-account-key.json
    - gcloud auth activate-service-account --key-file gcp-service-account-key.json
    - gcloud config set project $GOOGLE_PROJECT_ID
    - gcloud run deploy $SERVICE_NAME --image gcr.io/$GOOGLE_PROJECT_ID/$IMAGE_NAME:$CI_COMMIT_SHA --platform managed --region $GCP_REGION --allow-unauthenticated
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'


After creating the .gitlab-ci.yml file, GitLab will automatically build and deploy your application to Google Cloud Run whenever you push changes to the main branch. You can adjust the rules in the build and deploy stages to match your desired workflow.

Subscribe to dadonk

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe