Google Cloud Repositories CI/CD
Summary
- Source Code Repositories Pricing
- Create Github Repository
- Create Google Cloud Source Repository
- Create Dockerfile for uploading to Firebase
- Create cloudbuil.yaml for Google Cloud Build to trigger build
- Setup Triggers
- Commit to cause a trigger to run
- Update content to change Hello World
Why do I use Google Source Repositories?
Because it is cheaper! (and I develop on a shoestring budget) The negative is that it is not as straight forward to use as git clone {{% /blockquote-success %}}
Github
Google Cloud Source Repositories
Github
Create Repository
git clone https://github.com/AJONPLLC/lesson-1-firebase-project.git cd lesson-1-firebase-project/
Remove remote references
git remote -v
origin https://github.com/AJONPLLC/lesson-1-firebase-project.git (fetch)
origin https://github.com/AJONPLLC/lesson-1-firebase-project.git (push)
git remote rm origin
Add Newly created repo
git remote add origin https://github.com/AJONPLLC/lesson-2-firebase-ci.git git push -u origin master
Google Cloud
As of right now December 3, 2018 Google has this message: This version of Cloud Source Repositories will permanently redirect to the new version of Cloud Source Repositories starting December 3rd. Try the new version today for fast code search, an improved code browser, and much more.
Google Cloud Source Repositories
Add Google Cloud Repository (standalone)
Add Code to Google Cloud Repository
git remote -v
origin https://github.com/AJONPLLC/lesson-2-firebase-ci.git (fetch)
origin https://github.com/AJONPLLC/lesson-2-firebase-ci.git (push)
git remote add google https://source.developers.google.com/p/ajonp-ajonp-com/r/ajonp-lesson-2
google https://source.developers.google.com/p/ajonp-ajonp-com/r/ajonp-lesson-2 (fetch)
google https://source.developers.google.com/p/ajonp-ajonp-com/r/ajonp-lesson-2 (push)
origin https://github.com/AJONPLLC/lesson-2-firebase-ci.git (fetch)
origin https://github.com/AJONPLLC/lesson-2-firebase-ci.git (push)
Helpful hint for multiple remotes in VSCode, you can access the git tab, then “…”, then “Push to…”. This will show you the remotes.
git remote remove origin
Create Dockerfile
dockerfiles/firebase/Dockerfile
# use latest Node FROM node # install Firebase CLI RUN npm install -g firebase-tools ENTRYPOINT ["/usr/local/bin/firebase"]
Again we are doing this on the cheap, other places will charge you a monthly fee for this. Google allows for 120 build minutes a day!!
Create Cloudbuild
steps:
# Build the firebase image
- name: 'gcr.io/cloud-builders/docker' args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/firebase', './dockerfiles/firebase' ]
# Deploy to firebase
- name: 'gcr.io/$PROJECT_ID/firebase' args: ['deploy', '--token', '${_FIREBASE_TOKEN}']
# Optionally you can keep the build images
# images: ['gcr.io/$PROJECT_ID/hugo', 'gcr.io/$PROJECT_ID/firebase']
You may have noticed an interesting line here, this allows for an argument called _FIREBASE_TOKEN to be setup in our cloud deploy, so it doesn’t leak out in our GitHub/GCP Repositories. args: [‘deploy’, ‘–token’, ‘${_FIREBASE_TOKEN}’]
firebase login:ci
1/8V_izvEco3KY8EXAMPLEONLYpnLGpGLPAvofC_0YX3qx2NE_Zxs Along with a message Example: firebase deploy --token "$FIREBASE_TOKEN"
Setup trigger
Github Trigger
Pay close attention to add the Firebase Token from the steps above.
GitHub Commit for trigger
git add . git commit -m "docker and cloudbuild added"
git push --set-upstream origin master
Google Source Repository Trigger
git push --set-upstream google master
Update Content – see CI/CD in Action
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Welcome to Firebase Hosting</title>
</head>
<body> Firebase is not building everytime I commit, just from following<a
href="https://ajonp.com/lessons/2-firebase-ci/">Google Cloud Repositories CI/CD</a> <img
src="https://res.cloudinary.com/ajonp/image/upload/q_auto/v1543793005/ajonp-ajonp-com/2-lesson-gcp-cloud-build/aj_on_firebaseCI.webp"
alt="Hero Image"> </body>
</html>
git add . git commit -m "CI/CD" git push --set-upstream google master