【问题标题】:How do I SCP repo files using GitHub Actions?如何使用 GitHub Actions SCP 存储库文件?
【发布时间】:2020-05-31 19:59:42
【问题描述】:

我正在尝试实现一个 GitHub 操作,它将我的 repo 文件通过 SCP 发送到服务器并推送到主分支。我在 Bitbucket Pipelines 上有一个类似的设置,但现在我正在学习使用 GitHub 操作来执行此操作,我没有任何运气。

我的项目是一个简单的Node.js app,我想简单地将所有文件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/'

这个动作可以完成Set up job和Build 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

我不太确定我做错了什么。即使我将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.
================================================

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

【问题讨论】:

  • 根据您的更新,appleboy/scp-action@master 操作看起来更像是一个问题。通过在该存储库中提交问题,您可能会获得更多关注。
  • 嗯,你可能是对的。虽然我已经尝试了来自不同提供商的一堆类似的 scp 操作,但它们都具有相同的效果。我会尝试联系。

标签: git github continuous-integration scp github-actions


【解决方案1】:

希望这会有所帮助!

  • 首先您在您的存储库外部创建一个文件夹
  • 然后你将所有的 repo 内容复制到其中
  • 然后你 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"

【讨论】:

  • 太棒了!这正是我想要的谢谢!
  • 请说明您是如何运行部署后脚本的。
  • @ManiKumarReddyKancharla 你只是添加了额外的步骤,但我总是在运行下一步之前大约 15 秒放一点延迟步骤。
  • 我如何通过 GitHub 操作中的代理主机进行 SCP?
猜你喜欢
  • 2021-09-16
  • 2022-11-20
  • 2020-02-09
  • 2023-03-25
  • 2022-08-08
  • 2021-01-29
  • 2019-12-21
  • 2020-06-02
  • 2020-02-01
相关资源
最近更新 更多