如何使用 GitHub 操作 SCP 存储库文件?

Sam*_*rko 1 git continuous-integration scp github github-actions

我正在尝试实施一个 GitHub 操作,该操作将我的 repo 文件在推送到 master 分支时将其 SCP 到服务器。我在 Bitbucket Pipelines 上有一个类似的设置,但现在我正在学习使用 GitHub 操作来做到这一点,我没有任何运气。

我的项目是一个简单的Node.js 应用程序,我想简单地将所有文件 scp 到服务器,然后我将运行 post-scp 脚本,以便npm i将新文件复制到服务器。只是想在我学习的时候保持简单。

我正在使用scp-files GitHub Action。这是我的文件:

name: Deploy to production

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
jobs:
  deploy:
    name: SCP files to server
    runs-on: ubuntu-latest
    steps:
      - name: SCP files via ssh key
        uses: appleboy/scp-action@master
        env:
          USERNAME: ${{ secrets.USERNAME }}
          HOST: ${{ secrets.HOST }}
          KEY: ${{ secrets.SSH_DEPLOYMENT_KEY }}
        with:
          source: './*'
          target: '/home/ubuntu/flatbread/'
Run Code Online (Sandbox Code Playgroud)

这个动作能够完成Set up jobBuild appleboy/scp-action@master。但是运行的时候会报错appleboy/scp-action@master。这是我收到的错误:

tar: empty archive
exit status 1
tar all files into /tmp/320558105/i2yG360Zje.tar
##[error]Docker run failed with exit code 1
Run Code Online (Sandbox Code Playgroud)

我不太确定我做错了什么。即使我将 更改source: './*'为示例文件夹(即source: app),它仍然给我同样的错误。


更新

如果我将 更改source: './*'source: '.',就不再出现 GitHub 操作错误而言,这似乎可以解决问题:

tar all files into /tmp/719605837/1uYygkf4Vn.tar
scp file to server.
create folder /home/***/flatbread/
untar file 1uYygkf4Vn.tar
remove file 1uYygkf4Vn.tar
================================================
Successfully executed transfer data to all host.
================================================
Run Code Online (Sandbox Code Playgroud)

不幸的是,在验证服务器上的文件时,没有对其进行任何更改。任何想法为什么会这样?

小智 7

希望这可以帮助!

  • 首先创建一个文件夹以外的回购
  • 然后您将所有回购内容复制到其中
  • 然后你tar它
  • 上传到服务器
name: CI

# 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
    - uses: actions/checkout@v2

    # Runs a set of commands using the runners shell
    - name: Run a multi-line script
      run: |
        mkdir ../build
        cp -TR . ../build
        tar -cvf deploy.tar ../build/

    - name: copy file via ssh password
      uses: appleboy/scp-action@master
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        key: ${{ secrets.KEY }}
        port: ${{ secrets.PORT }}
        source: "deploy.tar"
        target: "destination/folder"
Run Code Online (Sandbox Code Playgroud)