使用Github Action部署 前后端 ,pm2启动node.js

791 阅读2分钟

github actions 官方传送门

GitHub Actions 是一个持续集成和持续交付 (CI/CD) 平台,可让您自动化构建、测试和部署管道。您可以创建工作流来构建和测试存储库的每个拉取请求,或将合并的拉取请求部署到生产中。

GitHub Actions 不仅仅是 DevOps,还允许您在存储库中发生其他事件时运行工作流。例如,您可以运行工作流以在有人在您的存储库中创建新问题时自动添加适当的标签。

GitHub 提供 Linux、Windows 和 macOS 虚拟机来运行您的工作流程,您也可以在自己的数据中心或云基础架构中托管自己的自托管运行器。

废话不多说,直接上最全的代码


# 触发脚本的条件,develop分支push代码的时候
name: node // 流程名称
on:
  push:
    branches: [ node ]   //当node分支提交成功开始执行
  pull_request:
    branches: [ node ]

# 要执行的任务
jobs:
  # runs-on 指定job任务运行所需要的虚拟机环境(必填)
  build:
    runs-on: ubuntu-latest

    # 获取源码
    steps:
      # 使用action库  actions/checkout获取源码
      - name: action
        uses: actions/checkout@v1

      - name: node v14.17.0
        uses: actions/setup-node@v1
        with:
          # 选择要使用的 node 版本
          node-version: "v14.17.0"

      - name: npm install    // 找到vue项目安装依赖开始打包
        run: |
          cd nodeblog/vue3
          npm i
          npm run build

      - name: 前端vue文件打包📦
        run: |
          cd nodeblog/vue3/dist 
          sudo apt-get install unzip // 先压缩文件
          zip -r dist.zip ./* -r     // 压缩dist目录
          ls

      # 上传打包文件到远程服务器 腾讯
      - name: Deploy——TX 🚀
        uses: cross-the-world/ssh-scp-ssh-pipelines@latest # 因为构建之后,需要把代码上传到服务器上,所以需要连接到ssh,并且做一个安全拷贝(security copy)操作
        env:
          WELCOME: "ssh scp ssh pipelines"
          LASTSSH: "Doing something after copying"
        with:
          host: ${{ secrets.TX_HOST }}
          user: ${{ secrets.TX_USER }}
          pass: ${{ secrets.TX_PASS }}
          port: ${{ secrets.TX_PORT }}
          connect_timeout: 10s
          first_ssh: |
            rm -rf /usr/share/nginx/html
            mkdir -p /usr/share/nginx/html
          scp: |
            'node-koa-pm2/pmaoblog/dist/dist.zip' => /usr/share/nginx/html
          last_ssh: |
            cd /usr/share/nginx/html
            unzip dist.zip
            rm -rf dist.zip
            
  node:
    runs-on: ubuntu-latest

    # 获取源码
    steps:
      # 使用action库  actions/checkout获取源码
      - name: action
        uses: actions/checkout@v1

      - name: node v14.17.0
        uses: actions/setup-node@v1
        with:
          # 选择要使用的 node 版本
          node-version: "v14.17.0"

      - name: zip 📦
        run: |
          sudo apt-get install unzip
          zip -r nodeblog.zip nodeblog/

      # 启动node后台服务
      - name: node部署 🚀🚀
        uses: cross-the-world/ssh-scp-ssh-pipelines@latest # 因为构建之后,需要把代码上传到服务器上,所以需要连接到ssh,并且做一个安全拷贝(security copy)操作
        env:
          WELCOME: "ssh scp ssh pipelines"
          LASTSSH: "Doing something after copying"
        with:
          host: ${{ secrets.TX_HOST }}
          user: ${{ secrets.TX_USER }}
          pass: ${{ secrets.TX_PASS }}
          port: ${{ secrets.TX_PORT }}
          connect_timeout: 20s
          first_ssh: |
            rm -rf /usr/nodeblog
            mkdir -p /usr/nodeblog
          scp: |
            'nodeblog.zip' => /usr/nodeblog
          last_ssh: |
            cd /usr/nodeblog
            unzip nodeblog.zip
            cd /usr/nodeblog
            npm i
#            pm2 start ./src/main.js
            pm2 restart ./src/main.js