Search code examples
githubgithub-actions

How can I run a linux command with brackes in Github workflow YAML?


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.


Solution

  • 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