current position:Home>Alicloud server series (VI) cicd JSON server on alicloud using GitHub action

Alicloud server series (VI) cicd JSON server on alicloud using GitHub action

2022-01-27 04:33:47 helloBearBaby

install json-server

json-server need node Environment to run , Therefore, you need to check whether the local installation node, Use node -v Check whether the local environment is installed , If installed, you will be prompted for version , If it is not installed, an error will be reported .

npm install -g json-server

 Insert picture description here
see json-server Is the installation successful :

json-server --help

 Insert picture description here
Installation succeeded
You can try to write a json file :db.json

  "data": [
      "title": " The museum ",
      "address": " Chengdu ",
      "date": "2021 year 9 month 8 Japan "
      "title": " Mt. ",
      "address": " Chengdu ",
      "date": "2021 year 9 month 9 Japan "

Then execute... On the same path

json-server --watch db.json --port 5000

 Insert picture description here
So we have a mock The source of the data

install pm2

PM2 Is a load balancing function Node Application process manager .

 npm install pm2 -g

 Insert picture description here
Installation successful

Create a json file

vim process.json

The content is :

  "apps": [
      "name": "mock-api",
      "script": "json-server",
      "args": "--watch /usr/share/nginx/html/data/db.json --port 5000"

among script Indicates the command we want to execute ,args Refer to specific parameters , Note that the path here needs to use an absolute path , Follow us just now json-server --watch db.json --port 5000 The same part .

Execute on the same path pm2:

pm2 start process.json

 Insert picture description here
You can see pm2 Success is up

To configure Nginx

Antecedents feed : Alibaba cloud server series ( Four ) To configure Nginx

We hope pm2 mock The data will be proxy , edit Nginx The configuration file :

vim /usr/local/nginx/conf/nginx.conf
location /data {
        rewrite /data/(.*) /$1 break;
        proxy_pass http://localhost:5000;

So that we pm2 the 5000 The port will be proxied to "/data"

To configure Github action

Antecedents feed : Alibaba cloud server series ( 5、 ... and ) Use Github Action Publish the project to Alibaba cloud
stay ci.yaml Add... To the file :

      # deploy data to aliyun
      - name: deploy mock data to aliyun server
        uses: easingthemes/[email protected]
          SSH_PRIVATE_KEY: ${
          ARGS: "-avzr --delete"
          SOURCE: "db.json"
          REMOTE_HOST: ${
    { secrets.ALIYUN_SERVER_HOST }}
          REMOTE_USER: ${
    { secrets.ALIYUN_REMOTE_USER }}
          TARGET: ${
    { secrets.ALIYUN_DATA_TARGET }}
      # start json-server
      - name: restart json-server
        uses: appleboy/[email protected]
         host: ${
    { secrets.ALIYUN_SERVER_HOST }}
         username: ${
    { secrets.ALIYUN_REMOTE_USER }}
         key: ${
         script: |
          cd ${
    { secrets.ALIYUN_DATA_TARGET }}
          pm2 restart process.json

The first operation is to put db.json Push the file to the directory specified by the server
The second operation is to enter the target path , And then execute pm2
This is restart pm2 The operation of :pm2 restart process.json

End of the flower

copyright notice
author[helloBearBaby],Please bring the original link to reprint, thank you.

Random recommended