question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to list only folders with changes

See original GitHub issue

I’m working on a repo where this GitHub Action would be very useful. The folder structure is:

.
└── .github
    └── workflows
        └── deploy.yml
├── README.md
└── Services
    ├── serviceone
    │   └── bunch_of_files
    ├── servicetwo
    │   └── bunch_of_files
    └── servicethree
        └── bunch_of_files

The deploy.yml file currently looks like

name: 'Deploy'

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Check which services were modified
        uses: dorny/paths-filter@v2
        id: filter
        with:
          list-files: shell
          filters: |
            services:
              - 'Services/**'
      - name: Deploy changed services
        if: ${{ steps.filter.outputs.services == 'true' }}
        run: |
          echo "All changes:"
          for i in ${{ steps.filter.outputs.services_files }}; do echo $i | awk ; done

Instead of outputting the individual files changes in Services/** is there a way to just output the folders containing changes such as

Services/serviceone
Services/servicethree

Thanks!

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
ndpetecommented, Jul 22, 2022

So my filters are just slightly different than the example. The key on the filter is a directory name and the value is the directory. After the matrix is setup I just set a default working-directory to the output from the matrix which is the key and therefore the directory I need to run from. Below is the example modified similiar to what I have.

jobs:
  # JOB to run change detection
  changes:
    runs-on: ubuntu-latest
    outputs:
      # Expose matched filters as job 'packages' output variable
      packages: ${{ steps.filter.outputs.changes }}
    steps:
    # For pull requests it's not necessary to checkout the code
    - uses: dorny/paths-filter@v2
      id: filter
      with:
        filters: |
          package1: src/package1
          package2: src/package2

  # JOB to build and test each of modified packages
  build:
    needs: changes
    strategy:
      matrix:
        # Parse JSON array containing names of all filters matching any of changed files
        # e.g. ['package1', 'package2'] if both package folders contains changes
        package: ${{ fromJSON(needs.changes.outputs.packages) }}
    runs-on: ubuntu-latest
    defaults:
       run:
         working-directory: src/${{ matrix.package }}
    steps:
      - uses: actions/checkout@v2
      - ...

So the two key points are key in the filter is the directory name and then the part:

defaults:
      run:
        working-directory: src/${{ matrix.package }}

This sets the working directory for the matrix jobs to src/{whatever the current matrix job is}

Does that make any sense? I could mock up a repo that shows the process if needed.

0reactions
Shakedcommented, Nov 22, 2022

@ahrenstein @vincentgna

I ended up with this hacky solution:

jobs:
  changes:
    runs-on: ubuntu-latest
    # Required permissions
    # permissions:
    #   pull-requests: read
    outputs:
      # Expose matched filters as job 'packages' output variable
      packages: ${{ steps.filter.outputs.changes }}
      directories: ${{ steps.transform.outputs.directories }}
    steps:
    # For pull requests it's not necessary to checkout the code
    - name: Checkout
      uses: actions/checkout@v3
    - uses: dorny/paths-filter@v2
      id: filter
      with:
        base: ${{ github.ref }}
        list-files: shell
        filters: |
          azure:
            - azure/**
    - name: transform to directories
      id: transform
      continue-on-error: false
      run: |
        folders=()
        for f in ${{ steps.filter.outputs.azure_files }}; \
          do \
            echo "Adding $(dirname $f) to folders"; \
            folders+=($(dirname $f)); \
        done
        unique_folders=($(printf "%s\n" "${folders[@]}" | sort -u | tr '\n' ' '))
        echo "directories=$(jq --compact-output --null-input '$ARGS.positional' --args -- ${unique_folders[@]})" >> $GITHUB_OUTPUT

terraform:
    needs: changes
    name: "Terraform"
    runs-on: ubuntu-latest
    strategy:
      matrix:
        directory: ${{ fromJSON(needs.changes.outputs.directories) }}
    steps:
      - name: test
         run: echo ${{ matrix.directory }}

It would be great to add an option that simulates it, something like list-directories: shell. I don’t mind adding a PR if it’s accepted by the owners.

Read more comments on GitHub >

github_iconTop Results From Across the Web

git diff - only show which directories changed - Stack Overflow
yes, as I said, you get only directories with that command. The cut is doing that.
Read more >
List Your Folder Structure in Windows - UW Finance
Open File Explorer in Windows. Navigate to the directory containing the folders you wish to appear in your list. Click in the address...
Read more >
Linux / UNIX List Just Directories Or Directory Names - nixCraft
You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command...
Read more >
List of recently changed files for a directory and all ...
Try dir /a-d /o-d /tw /s (show files only, order by date descending, use last write time for sorting, recurse into subdirs). However...
Read more >
Is there any way to list all folders ONLY in the level directly ...
Is there any way to list all folders ONLY in the... Learn more about path, files, directory, genpath, dotdot, dot, dot directory names....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found