Search code examples
bashyamlcopygithub-actions

Copy files with github-action


I am trying to write a GitHub action that makes a copy of a file in my repo into a different subdir in the repo. This is my dir structure (before the file is copied)

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

This is what I want my file structure to be after the file is copied

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

So basically, my GitHub action should make a copy of my_file.json named copy_of_my_file.json and place it inside the tests sub dir. I've built a bash script with the following

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

Locally, I run chmod u+x copyfile.sh and then bash copyfile.sh and the file is indeed copied. My copy_file.yml workflow is as follows:

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"

The action runs with no errors, but the file doesn't get copied. I tried other actions from the GitHub Marketplace with no luck. I also tried changing the action's run to simply cp my_file.json tests/copy_of_my_file.json but it doesn't work either.


Solution

  • The problem was that the file needed to be committed to the repo. One could write its own commit action or pick from the GitHub Actions Marketplace.

    In the background, what happens is that the action is run on a GitHub server. The checkout action fetches the files from the repo. Anything that is done afterward happens in that server. Therefore, anything that needs to be "saved", must be pushed to the repo or generate an action artifact.