Skip to content

Instantly share code, notes, and snippets.

@claudiobastos
Created September 10, 2018 16:14
Show Gist options
  • Select an option

  • Save claudiobastos/5c7f6958a96ab46f6070fdfe2fb3f555 to your computer and use it in GitHub Desktop.

Select an option

Save claudiobastos/5c7f6958a96ab46f6070fdfe2fb3f555 to your computer and use it in GitHub Desktop.
From Push to Kubernetes : using Google Cloud Build
# build containers
steps:
# $PROJECT_ID : the project ID of the build.
# $BUILD_ID : the autogenerated ID of the build.
# $REPO_NAME : the source repository name specified by RepoSource.
# $BRANCH_NAME : the branch name specified by RepoSource.
# $TAG_NAME : the tag name specified by RepoSource.
# $REVISION_ID
# $COMMIT_SHA : the commit SHA specified by RepoSource or resolved from the specified branch or tag.
# $SHORT_SHA : first 7 characters of $REVISION_ID or $COMMIT_SHA.
# Fetch vendor from bucket
- name: 'gcr.io/cloud-builders/gsutil'
args: ['cp', '-r', 'gs://my_project_bucket/vendor.tgz', './vendor.tgz']
waitFor: ['-'] # The '-' indicates that this step begins immediately.
id: 'fetch-resources'
# Decompress vendor
- name: 'debian'
dir: 'src'
args: ['tar', '-xzf', '../vendor.tgz']
id: 'decompress-vendor'
waitFor: ['fetch-resources']
# Remove vendor
- name: 'debian'
args: ['rm', './vendor.tgz']
id: 'remove-vendorzip'
waitFor: ['decompress-vendor']
# Prepara o autoload bootstrap
- name: 'composer'
args: ["composer", "dump-autoload"]
dir: 'src'
id: 'dump-autoload'
waitFor: ['decompress-vendor']
# Unit Tests
- name: 'phpunit/phpunit:latest'
args: [ '--debug', '--testsuit', '"Unit Tests"' ]
dir: 'src'
id: 'unit-tests'
waitFor: ['dump-autoload']
# Build app image
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-q', '-t', 'gcr.io/$PROJECT_ID/web_myapp', '.' ]
id: 'myapp'
waitFor: ['unit-tests']
# Push app image
- name: 'gcr.io/cloud-builders/docker'
args: ["push", "gcr.io/$PROJECT_ID/web_myapp"]
id: 'push-myapp'
waitFor: ['myapp']
# Build php image
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-q', '-t', 'gcr.io/$PROJECT_ID/web_php_fpm', '.', '--build-arg', 'COMMIT_SHA=$SHORT_SHA', '--build-arg', 'PROJECT_ID=$PROJECT_ID', '--build-arg', 'BUILD_ID=$BUILD_ID']
dir: 'php-fpm'
id: 'php-fpm'
waitFor: ['unit-tests']
# Push php-fpm
- name: 'gcr.io/cloud-builders/docker'
args: ["push", "gcr.io/$PROJECT_ID/web_php_fpm"]
dir: 'php-fpm'
id: 'push-php-fpm'
waitFor: ['php-fpm']
# Build nginx image
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build', '-q', '-t', 'gcr.io/$PROJECT_ID/web_nginx', '.' ]
dir: 'nginx'
id: 'nginx'
waitFor: ['unit-tests']
# Push nginx
- name: 'gcr.io/cloud-builders/docker'
args: ["push", "gcr.io/$PROJECT_ID/web_nginx"]
dir: 'nginx'
id: 'push-nginx'
waitFor: ['nginx']
# Prepare new PHP deploy yaml
- name: 'debian'
dir: 'k8s'
args: ['sed', '-i', "s/hash: placeholder/hash: $COMMIT_SHA/g", 'php-fpm-deployment.yaml']
id: 'fix-deploy-php'
waitFor: ['php-fpm']
# Prepare new NGINX deploy yaml
- name: 'debian'
dir: 'k8s'
args: ['sed', '-i', "s/hash: placeholder/hash: $COMMIT_SHA/g", 'nginx-deployment.yaml']
id: 'fix-deploy-nginx'
waitFor: ['nginx']
# Prepare new PHP service yaml
- name: 'debian'
dir: 'k8s'
args: ['sed', '-i', "s/hash: placeholder/hash: $COMMIT_SHA/g", 'php-fpm-service.yaml']
id: 'fix-service-php'
waitFor: ['php-fpm']
# Prepare new NGINX service yaml
- name: 'debian'
dir: 'k8s'
args: ['sed', '-i', "s/hash: placeholder/hash: $COMMIT_SHA/g", 'nginx-service.yaml']
id: 'fix-service-nginx'
waitFor: ['nginx']
# Apply changes php: deploy
- name: 'gcr.io/cloud-builders/kubectl'
args: ['apply', '-f', 'k8s/php-fpm-deployment.yaml']
waitFor: ['push-php-fpm','push-myapp','fix-deploy-php']
id: 'apply-deploy-php'
env:
- 'CLOUDSDK_COMPUTE_ZONE=$_COMPUTE_ZONE_'
- 'CLOUDSDK_CONTAINER_CLUSTER=$_CONTAINER_CLUSTER_NAME_'
# Apply changes nginx: deploy
- name: 'gcr.io/cloud-builders/kubectl'
args: ['apply', '-f', 'k8s/nginx-deployment.yaml']
waitFor: ['push-nginx', 'push-myapp', 'fix-deploy-nginx', 'apply-deploy-php']
id: 'apply-deploy-nginx'
env:
- 'CLOUDSDK_COMPUTE_ZONE=$_COMPUTE_ZONE_'
- 'CLOUDSDK_CONTAINER_CLUSTER=$_CONTAINER_CLUSTER_NAME_'
# check if deploy succeeded and (if it did) update php-fpm service
- name: 'gcr.io/cloud-builders/kubectl'
args: ['rollout', 'status', 'deploy', 'pnw-admin-php-fpm']
waitFor: ['apply-deploy-php']
id: 'wait-php-deploy'
env:
- 'CLOUDSDK_COMPUTE_ZONE=$_COMPUTE_ZONE_'
- 'CLOUDSDK_CONTAINER_CLUSTER=$_CONTAINER_CLUSTER_NAME_'
# check if deploy succeeded and (if it did) update php-fpm service
- name: 'gcr.io/cloud-builders/kubectl'
dir: 'k8s'
args: ['apply', '-f', 'php-fpm-service.yaml']
waitFor: ['wait-php-deploy']
id: 'apply-service-php'
env:
- 'CLOUDSDK_COMPUTE_ZONE=$_COMPUTE_ZONE_'
- 'CLOUDSDK_CONTAINER_CLUSTER=$_CONTAINER_CLUSTER_NAME_'
# check if deploy succeeded and (if it did) update nginx service
- name: 'gcr.io/cloud-builders/kubectl'
args: ['rollout', 'status', 'deploy', 'pnw-admin-nginx']
waitFor: ['apply-deploy-nginx', 'apply-service-php']
id: 'wait-nginx-deploy'
env:
- 'CLOUDSDK_COMPUTE_ZONE=$_COMPUTE_ZONE_'
- 'CLOUDSDK_CONTAINER_CLUSTER=$_CONTAINER_CLUSTER_NAME_'
# check if deploy succeeded and (if it did) update nginx service
- name: 'gcr.io/cloud-builders/kubectl'
dir: 'k8s'
args: ['apply', '-f', 'nginx-service.yaml']
waitFor: ['wait-nginx-deploy']
id: 'apply-service-nginx'
env:
- 'CLOUDSDK_COMPUTE_ZONE=$_COMPUTE_ZONE_'
- 'CLOUDSDK_CONTAINER_CLUSTER=$_CONTAINER_CLUSTER_NAME_'
# my-image is pushed to Container Registry
images:
- 'gcr.io/$PROJECT_ID/web_myapp'
- 'gcr.io/$PROJECT_ID/web_php_fpm'
- 'gcr.io/$PROJECT_ID/web_nginx'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment