7

Is there a github action which allows me to copy specific folders (.eg. dist and static) from one branch to another in the same private repo. I appreciate any help. here is what was trying to get it to work using Copycat action.


name: Copying static files

on:
  push:
    branches:
      - src # Set a branch name to trigger deployment


jobs:
  copy:
    runs-on: ubuntu-latest
    steps:
    - name: Copycat
      uses: andstor/copycat-action@v3
      with:
        personal_token: ${{ secrets.PERSONAL_TOKEN }}
        src_path: static.
        dst_path: /static/
        dst_owner: CompanyX
        dst_repo_name: MyRepo
        dst_branch: dest
        src_branch: src
        src_wiki: false
        dst_wiki: false
    - name: Copycat2
      uses: andstor/copycat-action@v3
      with:
        personal_token: ${{ secrets.PERSONAL_TOKEN  }}
        src_path: dist.
        dst_path: /dist/
        dst_owner: CompanyX
        dst_repo_name: MyRepo
        dst_branch: des
        src_branch: src
        src_wiki: false
        dst_wiki: false

but I'm getting this error even though I have personal token setup in my profile.

fatal: could not read Password for 'https://github.com': No such device or address

Niloo Aghayan
  • 73
  • 1
  • 4

2 Answers2

5

If you want to commit to the same repository, you don't have to specify a personal token, just use actions/checkout@v2 (see this)

One solution to copy files between branch is to use git checkout [branch] -- $files, see this post

The following workflow copy files from directory named static on source branch to branch named dest:

name: Copy folder to other branch

on: [push]

jobs:
  copy:
    name: Copy my folder
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: copy
        env:
          SRC_FOLDER_PATH: 'static'
          TARGET_BRANCH: 'dest'
        run: |
          files=$(find $SRC_FOLDER_PATH -type f) # get the file list
          git config --global user.name 'GitHub Action'
          git config --global user.email 'action@github.com'
          git fetch                         # fetch branches
          git checkout $TARGET_BRANCH       # checkout to your branch
          git checkout ${GITHUB_REF##*/} -- $files # copy files from the source branch
          git add -A
          git diff-index --quiet HEAD ||  git commit -am "deploy files"  # commit to the repository (ignore if no modification)
          git push origin $TARGET_BRANCH # push to remote branch
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • I did not test the script, I just wanted to know can I use -S to sign the commit – GrayGalaxy Jul 23 '22 at 06:56
  • The `git checkout ${GITHUB_REF##*/} -- $files` does not work, as it says ```fatal: invalid reference: source-branch Error: Process completed with exit code 128.```. I even checkout-ed from `source-branch` earlier, so yes, the branch does exist. – DavidNyan10 Apr 17 '23 at 15:51
0

If you just want to check out a file from another branch, another option is to do the following:

- uses: actions/checkout@v3

- name: Fetch file from another branch
  run: |
    git fetch origin otherbranch:otherbranch --force
    git show otherbranch:filetoget.txt > filetoget.txt
    git diff 

The git diff step is optional but quite useful if you want to see what's changed.

Rocklan
  • 7,888
  • 3
  • 34
  • 49