首页 > 解决方案 > 使用 github-action 复制文件

问题描述

我正在尝试编写一个 GitHub 操作,将我的存储库中的文件副本复制到存储库中的不同子目录中。这是我的目录结构(在文件被复制之前)

my_project
  |_ .github
  |     |_ workflows
  |          |_ run_tests.yml
  |          |_ copy_file.yml
  |_ tests
  |    |_ testthat
  |         |_ test1.R
  |         |_ test2.R
  |_ my_file.json
  |_ copyfile.sh

这就是我希望我的文件结构在文件被复制后的样子

my_project
  |_ .github
  |     |_ workflows
  |          |_ run_tests.yml
  |          |_ copy_file.yml
  |_ tests
  |    |_ testthat
  |    |    |_ test1.R
  |    |    |_ test2.R
  |    |_ copy_of_my_file.json
  |_ my_file.json
  |_ copyfile.sh

所以基本上,我的 GitHub 操作应该复制my_file.json命名copy_of_my_file.json并将其放在tests子目录中。我已经bash用以下内容构建了一个脚本

#!/bin/bash
cp my_file.json tests/copy_of_my_file.json

在本地,我运行chmod u+x copyfile.sh然后bash copyfile.sh确实复制了文件。我的copy_file.yml工作流程如下:

name: Copy my_file.json to tests subdirectory
on: [push, pull_request, workflow_dispatch]
jobs:
  run:
    runs-on: [ubuntu-latest]
    steps:
    - name: Checkout ️
      uses: actions/checkout@v2

    - name: Make copy of my_file.json 
      run: |
        chmod u+x "${GITHUB_WORKSPACE}/copyfile.sh"
        bash "${GITHUB_WORKSPACE}/copyfile.sh"

该操作运行没有错误,但文件没有被复制。我尝试了 GitHub Marketplace 中的其他操作,但没有成功。我也尝试将动作更改run为简单cp my_file.json tests/copy_of_my_file.json,但它也不起作用。

标签: bashyamlcopygithub-actions

解决方案


推荐阅读