Niloo Aghayan
Niloo Aghayan

Reputation: 103

Github action to copy specific folders from one branch to another in the same repo

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

Upvotes: 10

Views: 6992

Answers (2)

Rocklan
Rocklan

Reputation: 8130

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.

Upvotes: 1

Bertrand Martel
Bertrand Martel

Reputation: 45503

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 '[email protected]'
          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

Upvotes: 6

Related Questions