quadronom
quadronom

Reputation: 732

GitHub Action to copy and a rename a file

Is there a smart way to copy and rename files via a GitHub Actions? I want to have some READMEs to be copied to the /docs folder (:= the same repo, not a remote one!), where they will be renamed according to their frontmatter title.

Goal is to have some kind of auto-updating doc system where everytime I push the Jekyll gets populated automatically.

Upvotes: 17

Views: 30984

Answers (1)

quadronom
quadronom

Reputation: 732

In the end this is what I did:

name: docs
on:
  push:
    branches: [ master ]
    paths: 'myfolder/*/README.md'

jobs:
  docit:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@master
    - name: Copy the Readmes
      run: |
        find ./myfolder/ -type f -name "README.md" | while read fname; do
          dirname=`dirname "$fname"`
          foldername=`basename "$dirname"`
          filename=`basename "$fname"`
          newname=`echo "$dirname" | sed -e "s/ /_/g"`
          cp "${dirname}/$filename" "./docs/_myfolder/${foldername}.md"
        done
    - name: Commit files
      run: |
        git config --local user.email "[email protected]"
        git config --local user.name "GitHub Action"
        git commit -m "Add changes" -a
    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

I figured out that I didn't need to look into the frontmatter, since the README was inside a folder with the name I need anyway.
Beware: cp will nag if you run it the first time and the CI will fail, because you can't have empty folders tracked in git. So I just created an empty dummy file to make sure the "_myfolder" is always there.

Upvotes: 22

Related Questions