Create a New Cloud Function from Git Cloud Source Repository
The objective of this step-by-step tutorial is to create and deploy a new Google Cloud Function from a local Windows 10 computer, and host the code in a Source Code Repository.
Install the Required Windows Dependencies
Install Git
Download from https://git-scm.com/downloads
If it is OK, try the following command in CMD
1 |
git --version |
Install NodeJS/NPM
Download from https://nodejs.org
To see, try the following command in CMD
1 |
npm --version |
New Git and NPM Project Initialization
Init New GIT Repository
Create a new project folder and init the GIT local repository
1 2 3 |
mkdir myproject cd myproject git init |
Init NPM Project
1 |
npm init |
Set the different parameters with your values (or the default ones). They could all be changed later.
At this point, it is possible to add the remote GIT repository, but we will do it in a next separated step.
When the process ends, your project folder should contain the package.json file.
Create NodeJS Entry Point
Create the fil in the prject folder, with you favorite text editor or EDI. With the Following example content :
1 2 3 4 5 6 7 8 9 10 |
/** * Responds to any HTTP request. * * @param {!express:Request} req HTTP request context. * @param {!express:Response} res HTTP response context. */ exports.helloWorld = (req, res) => { let message = req.query.message || req.body.message || 'Hello World!'; res.status(200).send(message); }; |
GIT Operations
Add the files to Commit
Add all files at once
1 |
git add . |
O add file by file
1 2 |
git add package.json git add index.js |
(Optional) Configure GIT User
If GIT has just been installed and this is the first use, you should configure th user name and email.
1 2 |
git config --global user.email "me@email.com" git config --global user.name "My Name" |
Commit the Code in the Local Git Repo
1 |
git commit -m "My Project Initial Commit" |
Google Cloud Source Code Repository
Create a new repository
Setup SSH Key
Check Windows 10 OpenSSH Configuration
Check that the Windows 10 OpenSSH client is installed
Test with CMD
Generate Keys
In the Command Line
1 |
ssh-keygen |
Select file location and passphrase. Your key has been generated. Default folder is .ssh in the user’s home. Copy the id_rsa.pub file’s content.
Register the SSH key with Google Cloud
Go to Manage SSH Keys
Register a new key
The key content is the content of the file id_rsa.pub
Push the Code to the Repository
Add the Remote Repository
1 |
git remote add google ssh://{user}@source.developers.google.com:2022/p/{project}/r/myRepo |
Push
1 |
git push --all google |
You can test by browsing to this code repository on Google Cloud
Push my Branch as Upstream branch
1 |
git push --set-upstream google master |
Xavier is a Freelance Innovation Solutions Architect and Technical Advisor. He has been working for 10 years in software development (Java, JEE, JavaScript, Angular, Node JS, React), and has solid experience as a Tech Lead and Technical Architect in different company sizes (Startup, SME, Large Enterprise), and in different sectors (Insurance, Risk Insurance, Transportation, Energy Management, Mobile Operator)
Recent Comments