-
-
Save pydevops/2d50bfd766a83b2607fda464f8b69506 to your computer and use it in GitHub Desktop.
Google Cloud Build PR deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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