skip to Main Content

In a github workflow YAML, I’ve been trying several ways to run a command to remove all the content of a directory excluding a directory, and I don’t find the way to make it work.

The command is:
rm -rf !(dist)

It throws an error since brackets are special yaml characters, so I have to quote them rm -rf "!(dist)". Then Github workflow doesn’t throw any error, but the command isn’t working.

Some idea how can I make it work?

To give more context, I want to publish just the dist content of a NPM project, instead publishing the root of it including the dist folder.

This is the YAML:

name: Node.js Package

on:
  release:
    types: [created]

jobs:
  publish-gpr:
    runs-on: ubuntu-latest
    permissions: write-all
  steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-node@v3
    with:
      node-version: 16
    - run: npm ci
    - run: npm run build
    - run: |
        rm -rf "!(dist)"
        cd dist
        cp -R ./ ../
        cd ..
        rm -rf ./dist
    - run: npm publish

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    As @Azeem indicated in the comments, I had to add shopt -s extglob to the beggining of the script.

    This is the working YAML with brackets:

    name: Node.js Package
    
    on:
      release:
        types: [created]
    
    jobs:
      publish-gpr:
        runs-on: ubuntu-latest
        permissions: write-all
        steps:
          - uses: actions/checkout@v3
          - uses: actions/setup-node@v3
            with:
              node-version: 16
          - run: npm ci
          - run: npm run build
          - run: |
              shopt -s extglob
              rm -rf "!(dist)"
              cd dist
              cp -R ./ ../
              cd ..
              rm -rf ./dist
          - run: npm publish
    

  2. One way you can get around this is use find instead of rm -rf. I believe this is the syntax or close to it:

    find . -mindepth 1 -name dist -prune -o -exec rm -rf {} ;
    

    if that does not work try this:

    find -maxdepth 1 ! -name dist ! -name . -exec rm -rf {} ;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search