Skip to content

Instantly share code, notes, and snippets.

@philschmid
Last active December 11, 2019 09:55
Show Gist options
  • Select an option

  • Save philschmid/e1059164f00422a11572040b929fd501 to your computer and use it in GitHub Desktop.

Select an option

Save philschmid/e1059164f00422a11572040b929fd501 to your computer and use it in GitHub Desktop.
cloudbuild.yaml for cloud run on gcp

Cloudbuild Example for private github-repository + Docker build and push + run Terraform script

HOW TO

run tf apply var=_GIT_USERNAME=test var=_GIT_Password=abc .... lokal. This will create a cloudbuild-trigger on the defined branch
of the repository. Afterwards you can push to the Branch an the Cloudbuild will be triggered and run the steps definden in the cloudbuild.yam

# gcloud builds submit --config=cloudbuild.yaml .
# 1. step clone git repository
# 2. build docker with all params
# 3. push docker to gcr
# 4. run tf init & apply to deploy cloud run
substitutions:
_GIT_USERNAME: talosinsights # default value
_GIT_PASSWORD: abcd # default value
_GIT_REPOSITORY: lvl-1-flair # default value
_IMAGE_NAME: lvl-1-flair # default value
_STAGE: qa
#TODO: check if pw has special characters https://support.brightcove.com/special-characters-usernames-and-passwords
#FIXME: if git clone with username und pw not working https://cloud.google.com/cloud-build/docs/access-private-github-repos
steps:
# Connect to the repository
- name: "gcr.io/cloud-builders/git"
id: Clone
args:
- clone
- ${_GIT_USERNAME}:${_GIT_PASSWORD}@github.com:${_GIT_USERNAME}/${_GIT_REPOSITORY}
# This step runs the unit tests on the app
# - name: 'python:3.7-slim'
# id: Test
# entrypoint: /bin/sh
# args:
# - -c
# - 'pip install flask && python test_app.py -v'
# This step builds the container image.
- name: "gcr.io/cloud-builders/docker"
id: Build
args:
- build
- -t
- gcr.io/$PROJECT_ID/${_IMAGE_NAME}:$SHORT_SHA
- .
# This step pushes the image to Container Registry
# The PROJECT_ID and SHORT_SHA variables are automatically
# replaced by Cloud Build.
- name: "gcr.io/cloud-builders/docker"
id: Push
args:
- push
- gcr.io/$PROJECT_ID/${_IMAGE_NAME}:$SHORT_SHA
# This step initalizes the cloud run service on gcp with terraform init
- name: "hashicorp/terraform"
id: terraform-init
args:
- init
# This step initalizes the cloud run service on gcp with terraform apply
- name: "hashicorp/terraform"
id: terraform-apply
args:
- apply
- var=PROJECT_ID=$PROJECT_ID
- var=docker_image=gcr.io/$PROJECT_ID/${_IMAGE_NAME}:$SHORT_SHA
- -auto-approve
# Trigger for Github repo
# https://cloud.google.com/cloud-build/docs/create-github-app-triggers
resource "google_cloudbuild_trigger" "filename-trigger" {
trigger_template {
branch_name = "gcp-deploy"
repo_name = var.repo
}
substitutions = {
_GIT_USERNAME: var._GIT_USERNAME
_GIT_PASSWORD: var._GIT_PASSWORD
_GIT_REPOSITORY: var._GIT_REPOSITORY
_IMAGE_NAME: var._IMAGE_NAME
_STAGE: var._STAGE
# aws zeug
}
filename = "cloudbuild.yaml"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment