6

I'm trying to copy contents of a folder into another one during a github workflow. I know the workflow can create new folders and files because calling build on a react project creates the build that isn't present in the project, but it throws an error in a subsequent run command that uses mkdir.

Error: mkdir: cannot create directory ‘myNewFolder’: No such file or directory

My question is how to achieve either

> mkdir myNewFolder && cp -R myOldFolder myNewFolder

OR

> cp -R myOldFolder myNewFolder

to work when myNewFolder doesn't exist in the repo/workflow working directory?

EDIT (requested workflow file)

name: Test Server Build and Deploy (CD)

on:
  push:
    branches:
        - cd_branch

jobs:
  deploy:
    runs-on: ubuntu-latest
    env: 
        MY_APP_ENV_VARIABLE:  ${{ secrets.ENV_VARIABLE}}

steps:
        - uses: actions/checkout@v2
        - uses: actions/setup-node@v2
          with:
              node-version: '14.15.4'
        - run: npm cache clean --force
        - run: npm run copy-script

Where my copy-script is:

mkdir existingFolder/newFolder1/newFolder2 && \
    cp -R oldfolder/sub existingFolder/newFolder1/newFolder2
hoijui
  • 3,615
  • 2
  • 33
  • 41
Mr.Drew
  • 939
  • 2
  • 9
  • 30
  • That error is not related to git. you may have not enough permissions to create a new folder. what is the read/write/execute permission status of your current folder? – Mahdi Aryayi Jun 16 '21 at 05:17
  • If you are referring to the GitHub actions settings, I have selected "Workflows have read and write permissions in the repository for all scopes." for the particular repo in question. – Mr.Drew Jun 16 '21 at 05:21
  • Show a minimal version of your workflow file. `mkdir` and `cp` should work normally, just as they would on your local machine, in GitHub Actions. – DannyB Jun 16 '21 at 07:36

1 Answers1

7

When creating a new folder nested within another, add -p (parants) option after mkdir to tell Linux to make all directories listed in the path.

I tried with this and it works for me:

name: SO-023 Create folder

on:
  push:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Create folder
        run: |
          mkdir -p myNewFolder/myNewSubFolder && cp -R dist myNewFolder/myNewSubFolder
          ls myNewFolder/MyNewSubFolder
          
Mr.Drew
  • 939
  • 2
  • 9
  • 30
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • 1
    I think the pipe after `run` is a YAML syntax thing. It means the indented lines below it are a string, @Mr.Drew. It's only used when the string will be on a separate line from the key. The values next to `uses:` and such are also strings, but they're on the same line right next to `:` so you don't use a pipe there. – Mixchange Jul 17 '21 at 00:02
  • Can you put multiple pipes for line separating long commands or only the one after the `:` ? – Mr.Drew Jul 18 '21 at 07:38