如何运行 github 操作以在 Digital ocean 上部署和构建 Node 应用程序?

问题描述 投票:0回答:1

我正在尝试使用以下步骤编写 github 操作工作流:

  1. 通过 ssh 连接到数字海洋
  2. 导航到
    /saver
    文件夹
  3. main
    分支中提取更新
  4. 安装依赖和构建项目

我当前的代码是这样的

name: Deploy app

on:
  push:
    branches: [ main ]


jobs:
  build:
    runs-on: ubuntu-20.04

    steps:
      - name: Deploy to Digital Ocean
        uses: appleboy/ssh-action@master
        with: 
          host: ${{secrets.SSH_HOST}}
          key: ${{secrets.SSH_KEY}}
          username: ${{secrets.SSH_USERNAME}}
          passphrase: ${{secrets.SSH_PASSPHRASE}}
          script: |
            cd saver 
            mkdir test #just to check if it connects and creates folder
        
      - name: Checkout
        uses: actions/checkout@v3
        with:
          ref: main

      - name: Pull changes
        run: git pull
         
      - name: Install client dependencies
        run: npm run client:prodinstall
                
      - name: Build client
        run: npm run client:build

      - name: Install server dependencies
        run: npm run server:prodinstall

      - name: Install server dependencies
        run: npm run server:build
        
      

正如我在日志中看到的那样,成功登录到 Digital ocean。在服务器上我看到文件夹

test
。但是 git pull 不起作用。我明白了

Run git pull
  
Already up to date.

但是如果我自己导航到 DO 服务器并运行 git pull 我会得到新的更改。

怎么了?

更新:

当前配置

name: Deploy app

on:
  push:
    branches: [ main ]


jobs:
  build:
    runs-on: ubuntu-20.04

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          persist-credentials: false

      - name: Executing remote ssh commands using password
        uses: appleboy/[email protected]
        env:
          SSH_KEY: ${{ secrets.SSH_KEY }}
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          script: |
            install -m 600 -D /dev/null ~/.ssh/id_rsa
            echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa
            host='github.com'
            hosts="$(dig +short "$host" | grep -v '\.$' | sed -z 's|\n|,|g')$host"
            ssh-keyscan -H "$hosts" > ~/.ssh/known_hosts
            cd project_name
            git pull origin main
            pm2 restart project_name
         
      - name: Install
        run: npm run build
  • 我复制了公钥
    cat .ssh/id_rsa.pub
    并把它放在https://github.com/settings/keys
  • nano .ssh/authorized_keys
  • chmod 700 .ssh/authorized_keys
  • 使用
    cat .ssh/id_rsa
  • 从服务器复制密钥
  • 并在回购设置中将其保存为秘密SSH_KEY
  • SSH_USERNAMESSH_HOST
  • 相同

当我推送 repo 时,我得到这个错误

ssh: handshake failed: ssh: unable to authenticate, attempted methods [none], no supported methods remain
node.js git deployment github-actions digital-ocean
1个回答
1
投票

如何通过 ssh 运行它?

您需要在

git pull
运行的命令中包含您的
appleboy/ssh-action@master

这意味着该动作的

script
部分应该包括一个
cd /path/to/repository
git pull

对于 SSH URL,这意味着您需要将私钥复制到 GitHub 秘密(例如:

SSH_PRIVATE_KEY
)并将其设置为工作流程文件中的环境变量。
例如:

- name: Executing remote ssh commands using password
  uses: appleboy/[email protected]
  env:
    SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
  with:
    host: ${{ secrets.HOST }}
    username: ${{ secrets.USERNAME }}
    port: ${{ secrets.PORT }}
    script: |
      install -m 600 -D /dev/null ~/.ssh/id_rsa
      echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
      host='github.com'
      hosts="$(dig +short "$host" | grep -v '\.$' | sed -z 's|\n|,|g')$host"
      ssh-keyscan -H "$hosts" > ~/.ssh/known_hosts
      git pull origin main
© www.soinside.com 2019 - 2024. All rights reserved.