Skip to content

Instantly share code, notes, and snippets.

@syuji-higa
Last active January 3, 2023 00:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syuji-higa/bb7ce8c598f40fd84faf3076d2eb00d6 to your computer and use it in GitHub Desktop.
Save syuji-higa/bb7ce8c598f40fd84faf3076d2eb00d6 to your computer and use it in GitHub Desktop.
Create a CI/CD environment for Next.js in Google Cloud Build & Run
gcloud alpha billing accounts list
echo "> Please select the billing account ID from the displayed and enter."
read GC_ACCOUNT_ID
echo "> Please enter a common project name for GitHub and GCP."
read PROJECT_NAME
echo "> Enter Cloud Run Location. (Recommendation is 'asia-northeast1' region)"
read GC_REVISION
GC_IMAGE_NAME=$PROJECT_NAME-image
gh repo create $PROJECT_NAME
cd $PROJECT_NAME
gcloud projects create $PROJECT_NAME --set-as-default
gcloud projects list
open -a '/Applications/Google Chrome.app' https://github.com/apps/google-cloud-build
echo "> After setting up the Google Cloud Build GitHub app, enter your Google Cloud project number."
read GC_PROJECT_NUMBER
gcloud beta billing projects link $PROJECT_NAME --billing-account=$GC_ACCOUNT_ID
gcloud services enable \
run.googleapis.com \
cloudbuild.googleapis.com \
containerregistry.googleapis.com \
cloudresourcemanager.googleapis.com
gcloud projects add-iam-policy-binding $PROJECT_NAME \
--member "serviceAccount:$GC_PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \
--role roles/run.admin
gcloud iam service-accounts add-iam-policy-binding \
$GC_PROJECT_NUMBER-compute@developer.gserviceaccount.com \
--member="serviceAccount:$GC_PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \
--role="roles/iam.serviceAccountUser"
npx create-next-app .
sed -i -e 's/"next start"/"next start -p \$PORT"/' ./package.json
cat << EOF > Dockerfile
FROM node:latest
WORKDIR /src
COPY . .
RUN yarn install \\
--prefer-offline \\
--frozen-lockfile \\
--non-interractive \\
--production=false
RUN yarn build
CMD ["yarn", "start"]
EOF
cat << EOF > cloudbuild.yaml
steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/\$PROJECT_ID/$GC_IMAGE_NAME:\$COMMIT_SHA', '.']
# Push the image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/\$PROJECT_ID/cloud-run-demo-image:\$COMMIT_SHA']
# Deploy image to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
args:
- 'run'
- 'deploy'
- '\$PROJECT_ID'
- '--image'
- 'gcr.io/\$PROJECT_ID/$GC_IMAGE_NAME:\$COMMIT_SHA'
- '--region'
- '$GC_REVISION'
- '--platform'
- 'managed'
images:
- 'gcr.io/\$PROJECT_ID/$GC_IMAGE_NAME'
EOF
cat << EOF > .gcloudignore
.git
.gitignore
node_modules/
.next/
out/
EOF
git add .
git commit -m "init"
git push origin master
open -a '/Applications/Google Chrome.app' https://console.cloud.google.com/run\?project=$PROJECT_NAME
open -a '/Applications/Google Chrome.app' https://console.cloud.google.com/cloud-build/dashboard\?project=$PROJECT_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment