Ray Lee | 李宗叡
Learn or Die
Published in
2 min readJun 21, 2019

--

點擊這裡參閱中文版

My Blog

Introduction

This article is about:

  1. Open a GCP instance with gcloud
  2. Import ssh key into an instance with gcloud
  3. Running your project in the background with Daemon
  4. Deploy your project on GCP virtual machine via gitlab pusher

Environment

Create a GCP virtual machine

Here is how I would do, you could have your own way.

  • Ray use Mac, so I install Google Cloud SDK locally. As to the installation, you could refer to official document
  • Create a VM
  1. Create a VM called example-instance-1
  2. The boot drive storage is 10GB
  3. Pull image we need from ubuntu-os-cloud
  4. We use ubuntu-1804-lts as the image-family, so the latest version of this family will be used automatically.
  5. The type of boot-drive is pd-stand, you could check the types with command gcloud compute disk-types list
  6. The machine type is f1-micro, you could check the types wit command gcloud compute machine-types list
  7. We identify each instance with tags, and we will it when we want to create a firewall-rules.
  8. We specify the zone of the instance. Some resources are limited in certain zone and region

As follows:

After creating, let’s produce ssh-key first

Assume that the key is named example

Put root before the key in instanceSSHList file, whose format is as follows:

Get the name of the instance

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)

Installation

Here we mainly install nvm, node with version v12.1.0, and npm. You could get more detail via the official document

Daemon

Now, let’s config Daemon. We will run our service with Daemon so it will be run automatically when it's disconnected.

  • If it shows that the service is not found, we need to reload the daemon
  • Note that the authority has to been revised, making Daemon executable
  • Set up auto-restart, when VM reboots, the service will auto start
  • In this example, the name of Daemon will be equal to the name of project

CI/CD

Gitlab variables setting

  • We are going to do CI/CD with Gitlab pusher, so we have to create a pair of ssh key , and set the private key as $SSH_PRIVATE_KEY in variable setting

Gitlab yaml config file

We will set up the yaml file of Gitlab pusher In our project

Conclusion

Until now, when we use git push to specified branch, we should be able to trigger gitlab pusher to achieve automatic deployment

Write Medium in Markdown? Try Markdium!

--

--

Ray Lee | 李宗叡
Learn or Die

It's Ray. I do both backend and frontend, but more focus on backend. I like coding, and would like to see the whole picture of a product.