Skip to content

Instantly share code, notes, and snippets.

@chonz0
Last active December 11, 2019 18:52
Show Gist options
  • Save chonz0/6c153ab18201ef661035e8bc8dab0414 to your computer and use it in GitHub Desktop.
Save chonz0/6c153ab18201ef661035e8bc8dab0414 to your computer and use it in GitHub Desktop.

Google Cloud Platform

Ir a Google Cloud Console y crear un nuevo proyecto

Luego es necesario habilitar Cloud Run API y Cloud Build API desde "APIs & Services > Enable APIs & Services" (este paso va a solicitar habilitar el billing para el proyecto).

Una vez configurado nuestro proyecto, vamos a automatizar el proceso de build & deploy.

Cada vez que pusheemos código a la rama master de nuestro repositorio en GitHub, Cloud Build va a buildear la imagen de Docker, subir el nuevo código a Cloud Container Registry y deployarlo a Cloud Run

Roles & Permissions

  • Abrir IAM & admin > IAM

  • Editar (✏️) la cuenta [PROJECT_NUMBER]@cloudbuild.gserviceaccount.com

  • Click en Add another role

  • Seleccionar Cloud Run > Cloud Run Admin

  • Guardar

  • Seleccionar [PROJECT_NUMBER]@developer.gserviceaccount.com

  • Agregar el rol Service Account User y Cloud Build Service Agent

Cloud Build Trigger

  • Abrir Cloud Build > Triggers
  • Connect Repository
  • Elegir GitHub (Cloud Build GitHub App)
  • Marcar el checkbox del repositorio deseado y aceptar TOS
  • Create Push Trigger y editarlo para que sólo afecte la rama master

Correr localmente

Para correr en local, ejecutar lo siguiente:

docker build --tag project-cloud:latest .
docker run -p 8080:8080 --name project_cloud project-cloud:latest

Se sugiere instalar Captain

1

Use cloud-build-local

It is possible to run the exact same build process which runs in Cloud Build on your local machine. Please keep in mind Docker is required.

cloud-build-local 
  --config=cloud-build/cloudbuild.yaml 
  --dryrun=false 
  --push .

Deployar a App Engine

Prerequisitos:

$ gcloud builds submit --tag gcr.io/{PROJECT_ID}/{IMAGE_NAME}:latest .
$ gcloud config set project myProject
$ gcloud app deploy app.yaml
$ gcloud app deploy ./app.flexible.yaml --image-url gcr.io/{PROJECT_ID}/{IMAGE_NAME}

Referencias: 1 2 3 4

Clean up

gcloud app describe # overview de una app
gcloud app services list # obtener listado de servicios
gcloud app services describe default # describir un servicio

The gcloud app versions list command gives the percent traffic split and serving status.

gcloud app versions list
SERVICE VERSION TRAFFIC_SPLIT LAST_DEPLOYED SERVING_STATUS
default 20181119t152033 0.00 2018–11–19T15:22:04–08:00 SERVING
default 20181119t154106 1.00 2018–11–19T15:42:05–08:00 SERVING

The gcloud app versions describe command can be used to identify the application runtime:

gcloud app versions describe --service default 20181010t161719
…
env: standard
…
servingStatus: SERVING
…

Checking Traffic You can check if your app is receiving traffic by reading the logs with the command

gcloud app logs read

Cleaning Up To delete the earlier version of the app not serving any traffic execute

VERSION=20181119t152033
gcloud app versions delete $VERSION

Check that the version has been deleted

gcloud app versions list
SERVICE VERSION TRAFFIC_SPLIT LAST_DEPLOYED SERVING_STATUS
default 20181119t154106 1.00 2018–11–19T15:42:05–08:00 SERVING

Al deployar nuevas versiones, las anteriores no se eliminan automáticamente, lo cual puede generar gastos no deseados y poco visibles.

Referencias: 1 2

steps:
# build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/project-cloud:${SHORT_SHA}', '.']
# push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/project-cloud']
# deploy container image to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
args: ['beta', 'run', 'deploy', 'project-cloud', '--image', 'gcr.io/$PROJECT_ID/project-cloud:${SHORT_SHA}', '--region', 'us-central1', '--allow-unauthenticated', '--platform', 'managed']
env:
- 'PORT=8080'
images:
- gcr.io/$PROJECT_ID/project-cloud
FROM node:10-alpine
WORKDIR /usr/src/app
ENV PORT 8080
ENV HOST 0.0.0.0
COPY package*.json ./
RUN npm install --only=production
# Copy the local code to the container
COPY . .
RUN npm run build
EXPOSE 8080
# Start the service
CMD npm run start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment