使用Github Action部署Node.js程序至腾讯云服务器

·  阅读 1989

核心思想是master分支push后触发Action去ssh云服务器执行shell来部署

1.云服务器生成SSH公私钥

ssh-keygen -t rsa -b 4096 -C your_email@example.com
默认会在~/.ssh/路径下生成两个文件(私钥和公钥):id_rsa, id_rsa.pub
把公钥内容复制进~/.ssh/authorized_keys文件内

2.云服务器创建shell脚本用于部署

示例采用了pm2部署Node.js程序,可以根据自己的情况编写shell

cd /your_project_path
git pull origin master
npm install
pm2 restart 0
复制代码

3.设置Github Secrets变量

项目页面Settings->Secrets->New secret
创建以下3个变量:
REMOTE_HOST: 云服务器ip
REMOTE_USER: SSH登录user
SERVER_SSH_KEY: 步骤1中id_rsa的内容(私钥)

4. 新建一个Action

项目页面点击Action新建,Workflow file内容如下:

# This is a basic workflow to help you get started with Actions

name: Deploy

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - name: Run a command remotely
      uses: docker://evaneos/ssh-action:0.1.0
      with:
        hosts: ${{ secrets.REMOTE_HOST }}
        user: ${{ secrets.REMOTE_USER }}
        private_key: ${{ secrets.SERVER_SSH_KEY }}
        commands: sh /your_path/your_shell.sh

复制代码

至此完成设置,当master分支push后,可以点击Action查看workflows

分类:
阅读
标签:
分类:
阅读
标签:
收藏成功!
已添加到「」, 点击更改