dorny / paths-filter

Conditionally run actions based on files modified by PR, feature branch or pushed commits
MIT License
2.12k stars 238 forks source link

Export lists of changed files via action output parameter #20

Closed dorny closed 4 years ago

dorny commented 4 years ago

For some use-cases it might be useful to provide access to lists of changed (or added/deleted/modified) files via output parameters.

For this use-cases there is already Changed Files Exporter action. Benefit to implement it here is that we could make it work also for push events and provide different encoding option (JSON array, new-line delimited).

robdodson commented 4 years ago

Would this make it so I could get a list of files that changed that matched a specific filter?

My use case is I'd like to pass a path separated list of files to textlint only if those files have changed. Something like:

- name: Get changed files
  uses: dorny/paths-filter@v2.2.0
  id: filter
  with:
  filters: |
    markdown:
      - '*.md'
- name: Lint Markdown
    if: ${{ steps.filter.outputs.markdown == 'true' }}
    run: npx textlint ${{ steps.filter.outputs.all_files }}  # where all_files would equal a space separated list of markdown file paths
dorny commented 4 years ago

Unfortunately right now this wouldn't work What's implemented on the develop branch returns JSON object with lists for all added, modified and deleted files. Provided path filters are not considered for this output, what I see now, is not very useful.

I will change it so it could support your use case. Maybe I will get to it tomorrow. If not, then it will take some more time as I'm going for a vacation afterwards.

Anyway, to avoid problems with files with white-space characters, the output will be definitely in the JSON format. You will have to use jq tool to transform it to space-delimited list.

robdodson commented 4 years ago

Sounds good, thank you :)

I'm not familiar with the jq tool, do you happen to have a link with an explainer?

dorny commented 4 years ago

Here's the proposal. I already implemented in a PR #32 Given your example, it would work like this:

- uses: dorny/paths-filter@export-files
  id: filter
  with:
    list-files: shell
    filters: |
      markdown:
        - added|modified: '*.md'
- name: Lint Markdown
  if: ${{ steps.filter.outputs.markdown == 'true' }}
  run: npx textlint ${{ steps.filter.outputs.markdown_files }}

Input parameter list-files set to shell enables listing of matching files for each rule. Output is provides as ${RULE_NAME}_files output variable, in this case it's markdown_files. The shell format wraps each path in single quotes so the output can be directly used in shell scrips. Therefore the jq tool I mention earlier won't be needed.

Another part of the solution is the added|modified specifier in the rule. Normally deleted files are also considered as "changed". For example if this action it's used to conditionally run tests, deletion of matching file should trigger it.

For linting it obviously doesn't make sense to consider removed files and linting tools would probably error on non-existent files.

Would this work for you? I still have to update the documentation but then I could release it as v2.3.0

robdodson commented 4 years ago

This looks great! Yeah we would totally use this :)

dorny commented 4 years ago

Implemented in release v2.3.0