Skip to content

Instantly share code, notes, and snippets.

@pydevops
Forked from mikesparr/cloudbuild-pr.yaml
Created March 20, 2021 16:45
Show Gist options
  • Save pydevops/2d50bfd766a83b2607fda464f8b69506 to your computer and use it in GitHub Desktop.
Save pydevops/2d50bfd766a83b2607fda464f8b69506 to your computer and use it in GitHub Desktop.
Google Cloud Build PR deploy
steps:
# Build image and push to GCR
- name: 'gcr.io/cloud-builders/docker'
id: Build
args:
- 'build'
- '-t'
- 'gcr.io/$PROJECT_ID/$_SERVICE_NAME:v$_PR_NUMBER'
- '.'
# TODO: look into [images]
- name: 'gcr.io/cloud-builders/docker'
id: Push
args:
- 'push'
- 'gcr.io/$PROJECT_ID/$_SERVICE_NAME:v$_PR_NUMBER'
# Access the id_github file from Secret Manager
- name: gcr.io/cloud-builders/gcloud
entrypoint: 'bash'
args: [ '-c', 'gcloud secrets versions access latest --secret=github-ssh-key > /root/.ssh/id_github' ]
volumes:
- name: 'ssh'
path: /root/.ssh
# Set up git with key and domain
- name: 'gcr.io/cloud-builders/git'
entrypoint: 'bash'
args:
- '-c'
- |
chmod 600 /root/.ssh/id_github
cat <<EOF >/root/.ssh/config
Hostname github.com
IdentityFile /root/.ssh/id_github
EOF
ssh-keyscan -t rsa github.com > /root/.ssh/known_hosts
volumes:
- name: 'ssh'
path: /root/.ssh
# Edit Kustomize layer in config repo and push changes
- name: gcr.io/cloud-builders/git
entrypoint: 'bash'
args:
- '-c'
- |
echo "Cloning repo ..."
git clone --depth 1 [email protected]:mikesparr/gcp-cicd-demo-config
echo "Updating image tag version ..."
cd gcp-cicd-demo-config
ls
sed -i "s|newTag: .*|newTag: v$_PR_NUMBER|" demo-app/overlays/review/kustomization.yaml
echo "Creating review app ..."
cp templates/demo-app-review.yaml apps/
echo "Pushing changes to git config repo ..."
git config --global user.name mikesparr
git config --global user.email [email protected]
git add -A
git commit -m "Updated with build ${BUILD_ID} from ${REPO_NAME} commit ${COMMIT_SHA}"
git push origin master
volumes:
- name: 'ssh'
path: /root/.ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment