Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 222
quadronom Avatar asked Jun 17 '20 15:06

quadronom


People also ask

How do I rename a file in GitHub?

In your repository, browse to the file you want to rename. In the upper right corner of the file view, click to open the file editor. In the filename field, change the name of the file to the new filename you want. You can also update the contents of your file at the same time.

How do I change a file name in git bash?

Step 1: Open GitHub. Step 2: Open the repository to rename any file in that repository. Step 3: Open the file which we want to rename. Step 4: Click the edit button and rename the file.

Can you CD in GitHub actions?

GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

How do I rename a file on GitHub?

Many files can be renamed directly on GitHub, but some files, such as images, require that you rename them from the command line. This procedure assumes you've already: Git Bash. Change the current working directory to your local repository.

How do I record a rename operation in Git?

Perform the following to record the rename operation in the staging area. Stage the deleted file− “file1.txt” and Stage the untracked file− “file1.java” Use the git add command to achieve this. Use the git status command to verify the status. The output of the command suggests that the rename operation has been recorded.

How do I move files from one GitHub repository to another?

Many files can be moved directly on GitHub, but some files, such as images, require that you move them from the command line. On your computer, move the file to a new location within the directory that was created locally on your computer when you cloned the repository. Open TerminalTerminalGit Bash.

How to rename a file in Linux Using mv command?

The syntax for using the Linux mv command is − Use the Linux command mv to rename the file to “file1.java”. Execute the git status command to verify the file’s status in Git. The output in the screenshot suggests that the file has been renamed in two steps −


1 Answers

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.

like image 185
quadronom Avatar answered Nov 15 '22 08:11

quadronom