Search code examples
linuxgithubgithub-actionsbuilding-github-actions

How to create a folder in github workflow?


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

Solution

  • 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