Introduction
This article is about:
- Open a GCP
instancewithgcloud - Import
ssh keyinto aninstancewithgcloud - Running your project in the background with
Daemon - Deploy your project on
GCP virtual machineviagitlab pusher
Environment
Create a GCP virtual machine
Here is how I would do, you could have your own way.
- Ray use
Mac, so I installGoogle Cloud SDKlocally. As to the installation, you could refer to official document - Create a VM
- Create a VM called
example-instance-1 - The boot drive storage is 10GB
- Pull
imagewe need fromubuntu-os-cloud - We use
ubuntu-1804-ltsas theimage-family, so the latest version of this family will be used automatically. - The type of boot-drive is
pd-stand, you could check the types with commandgcloud compute disk-types list - The machine type is
f1-micro, you could check the types wit commandgcloud compute machine-types list - We identify each instance with
tags, and we will it when we want to create a firewall-rules. - We specify the
zoneof the instance. Some resources are limited in certainzoneandregion
- Create a VM called
As follows:
gcloud compute instances create example-instance-1 \ |
After creating, let’s produce ssh-key first
ssh-keygen -t rsa -b 4096 -C "root@example" |
Assume that the key is named example
cat example.pub > instanceSSHConfig && vim instanceSSHList |
Put root before the key in instanceSSHList file, whose format is as follows:
[USERNAME]:ssh-rsa [KEY] [USERNAME] |
Get the name of the instance
gcloud compute instances list |
Add the public key into the instance
(Be careful! This command will replace all of your SSH keys on this instance, that said, any keys without appearing on this file will be gone)
gcloud compute instances add-metadata instanceName --metadata-from-file ssh-keys=instanceSSHList |
Installation
Here we mainly install nvm, node with version v12.1.0, and npm. You could get more detail via the official document
apt-get update -y && apt-get install curl -y && curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash && export NVM_DIR="$HOME/.nvm" && [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" && nvm install v12.1.0 && apt-get install npm -y |
Daemon
Now, let’s config Daemon. We will run our service with Daemon so it will be run automatically when it’s disconnected.
sudo vim /etc/init.d/serviceName |
#!/bin/sh |
If it shows that the service is not found, we need to reload the
daemonsudo systemctl daemon-reload
Note that the authority has to been revised, making
Daemonexecutablesudo chmod 755 serviceNameSet up auto-restart, when VM reboots, the service will auto start
sudo systemctl enable serviceNameIn this example, the name of
Daemonwill be equal to the name of project
CI/CD
Gitlab variables setting

- We are going to do
CI/CDwithGitlab pusher, so we have to create a pair ofssh key, and set theprivatekey as$SSH_PRIVATE_KEYinvariable settingssh-keyscan to-be-conneted-instance-ip
Gitlab yaml config file
We will set up the yaml file of Gitlab pusher
In our project
vim .gitlab-ci.yml |
# This file is a template, and might need editing before it works on your project. |
Conclusion
Until now, when we use git push to specified branch, we should be able to trigger gitlab pusher to achieve automatic deployment
Comments