Deploy NextJS on GCP
Create Docker Image
# Install dependencies only when needed
FROM node:alpine
ENV NODE_ENV production
WORKDIR /app
COPY . .
RUN npm install && npm run build
EXPOSE 3000
CMD ["npm", "start"]
#.dockerignore
node_modules
.next
npm-debug.log
.husky
.github
Setup GCP environment
Start New Project and push docker image to gcp container registry
gcloud auth login
gcloud auth configure-docker
docker push asia.gcr.io/${PROJECT_ID}/${IMAGE_NAME}
Create New Service for run the docker image
Setup Pipeline
Set pipeline by using github action
# This is a basic workflow to help you get started with Actions
name: My Website CI
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
env:
IMAGE_NAME: asia.gcr.io/${{ secrets.GCP_PROJECT_ID }}/my-website
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout
uses: actions/checkout@v2
# Install NodeJS
- name: Setup Node.js environment
uses: actions/setup-node@v2.1.5
# Install node_modules
- name: Install Node_modules
run: npm install
# Fix Style
- name: Fix Style
run: npm run lint && npm run lint:style
# GCP Login
- name: GCP Login
uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
with:
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_email: ${{ secrets.GCP_EMAIL }}
service_account_key: ${{ secrets.GCP_CREDENTIALS }}
- name: GCP Configure Docker
run: gcloud auth configure-docker --quiet
# Build Docker Image
- name: Build Docker image
run: docker build . -t $IMAGE_NAME
# Push Docker Image
- name: Push Docker image
run: docker push $IMAGE_NAME
# Deploy to Cloud Run
- name: Deploy to Cloud Run
run: gcloud run deploy my-website --image $IMAGE_NAME --region asia-east1 --platform managed
References
Last updated
Was this helpful?