Daniel
Daniel

Reputation: 1134

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.

Upvotes: 2

Views: 11872

Answers (2)

JohnMayorga18
JohnMayorga18

Reputation: 9

Hello!!



Fortunately I know what is the problem and how to solve it :)  

First you know what do you want with the "my_file json"??
  • To get copied to "GuestMachine" and execute them on that machine??.
  • To get copied automatically to your "GitHubReporsitory" for every any change??
So, I have the solution for both questions. Let´s Go.
First If do you want to copy "my_file json" on your "ActionGustLinuxMachine" you don´t need to use the "chmod" command, simple use the "sudo cp" command.... yes!! with sudo on your .yml action file and use the "permissions: contents: read" on the .yml file and that´s all I have projects with the "sudo cp my_file json folder" command and works propertly and please use EXACTLY that command NO!! "cp my_file json folder/my_file json" [INCORECT!!] (as I have seen before) and you can use the Linux "dir" command to see the results.

Now if do you want to reach the second answer, That is possible, so before to explain you the solution I must to explain you that the "GuestMachine" regardless the operating system (Linux, Windows, "Nginx") it´s only a machine that once the action is triggered, copy (automatically) the code from the repo to the "WorkMachine" and then execute the .yml (instructions) and finally if the execution for the .yml code is successful or failed (regardless) ALL THE INFORMATION FOR THAT MACHINE WILL BE ERASED because that machine will need to use for another action in another part of the world, that "ZoombieMachine" is another instance (VM) linked for a pool (BareMetal).

You have 2 Methods to do this, and the First is more complicated and faster, the second is easy and slower.

First use the .yml commands to install GitHub on the "GuestMachine" and use a "GitHubActionSecret" to autenticate via the GustMachineShell and clone your Reporsitory (sound redundant but is necessary), then use the "sudo cp command" that I have mentioned, and use it the make a copy from your "my_file json" then create a commit and push them :D .

The second solution requires more knowledge and you Must to use the GitHubCLI_API on the .yml and grand permissions in the @checkout see [How to Grant It][1] then use it to create the commit and pull [SeeTheDocumentationHere][2].

I hope I've helped you, please UpVote if helped you ;).

Upvotes: 0

Daniel
Daniel

Reputation: 1134

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.

Upvotes: 1

Related Questions