Skip to content

Instantly share code, notes, and snippets.

@mikesparr
Last active March 20, 2021 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikesparr/e2d0e00c3a0b051fa9168fbc918f9b93 to your computer and use it in GitHub Desktop.
Save mikesparr/e2d0e00c3a0b051fa9168fbc918f9b93 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 git@github.com: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 msparr@gmail.com
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