3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2024-12-16 09:27:57 +00:00
changed-files/README.md
tj-actions[bot] d6d7cb291e
Upgraded to v37 (#1298)
Co-authored-by: jackton1 <jackton1@users.noreply.github.com>
2023-06-23 19:55:39 +00:00

62 KiB

Ubuntu Mac OS Windows Public workflows that use this action.

Codacy Badge CI Update release version.

All Contributors

Screen Shot 2021-11-19 at 4 59 21 PM

changed-files

Effortlessly track all changed files and directories relative to a target branch, preceding commit or the last remote commit returning relative paths from the project root.

Table of contents

Features

  • Provides fast execution, averaging 0-10 seconds.
  • Leverages either Github's REST API or Git's native diff to determine changed files.
  • Facilitates easy debugging.
  • Scales to handle large repositories.
  • Supports Git submodules.
  • Generates escaped JSON output for running matrix jobs based on changed files.
  • Lists changed directories.
    • Allows limiting the matching changed directories to a specified maximum depth.
    • Optionally excludes the current directory.
  • Writes outputs to a designated .txt or .json file for further processing.
  • Restores deleted files to their previous location or a newly specified location.
  • Supports Monorepos by fetching a fixed number of commits.
  • Compatible with all platforms (Linux, MacOS, Windows).
  • Supports GitHub-hosted runners.
  • Supports GitHub Enterprise Server.
  • Supports self-hosted runners.
  • Lists all files and directories that have changed:
    • Between the current pull request branch and the last commit on the target branch.
    • Between the last commit and the current pushed change.
    • Between the last remote branch commit and the current HEAD.
  • Restricts change detection to a subset of files and directories:

And many more.

Usage

Warning

:

  • IMPORTANT: For push events when configuring actions/checkout the fetch-depth should be set to either fetch-depth: 0 OR fetch-depth: 2 depending on your use case.
  • For monorepos where pulling all the branch history might not be desired, you can omit actions/checkout - fetch-depth for pull_request events.
  • All multiline inputs should not use double or single quotes since the value is already a string separated by a newline character. See Examples for more information.
  • Ensure that persist-credentials is set to true when configuring actions/checkout if fetch-depth isn't set to 0.
  • For repositories that have PRs generated from forks when configuring actions/checkout set the repository to ${{ github.event.pull_request.head.repo.full_name }}. See: Example
name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  # -------------------------------------------------------------
  # Event `pull_request`: Returns all changed pull request files.
  # --------------------------------------------------------------
  job1:  # Example 1 - Using GitHub's API
    # NOTE: 
    # - This is limited to pull_request* events and would raise an error for other events.
    # - A maximum of 3000 files can be returned.
    runs-on: ubuntu-latest  # windows-latest | macos-latest
    name: Test changed-files
    if: github.event_name != 'push'
    permissions:
      pull-requests: read
    steps:
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v37

      - name: List all changed files
        run: |
          for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
            echo "$file was changed"
          done          

  # ------------------------------------------------------------------------------------------------------------
  # Event `push`: Compare the preceding commit -> to the current commit of the main branch.
  # Event `pull_request`: Compare the last commit of main -> to the current commit of a Pull Request branch.
  # ------------------------------------------------------------------------------------------------------------
  job2:  # Example 2 - Using local .git directory
    runs-on: ubuntu-latest  # windows-latest | macos-latest
    name: Test changed-files
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # OR "2" -> To retrieve the preceding commit.

      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v37
        
        # To compare changes between the current commit and the last pushed remote commit set `since_last_remote_commit: true`. e.g
        # with:
        #   since_last_remote_commit: true 

      - name: List all changed files
        run: |
          for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
            echo "$file was changed"
          done          

  job3:  # Example 3 - Using local .git directory
    runs-on: ubuntu-latest  # windows-latest | macos-latest
    name: Test changed-files
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # OR "2" -> To retrieve the preceding commit.

      - name: Get changed files in the docs folder
        id: changed-files-specific
        uses: tj-actions/changed-files@v37
        with:
          files: docs/*.{js,html}  # Alternatively using: `docs/**` or `docs`

      - name: Run step if any file(s) in the docs folder change
        if: steps.changed-files-specific.outputs.any_changed == 'true'
        run: |
          echo "One or more files in the docs folder has changed."
          echo "List all the files that have changed: ${{ steps.changed-files-specific.outputs.all_changed_files }}"          

  job4:  # Example 4 - Using local .git directory
    runs-on: ubuntu-latest  # windows-latest | macos-latest
    name: Test changed-files
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # OR "2" -> To retrieve the preceding commit.
  
      - name: Get all changed .js file(s) or any file in the static folder excluding the docs folder
        id: changed-files-excluded
        uses: tj-actions/changed-files@v37
        with:
          files: |
            **.js
            static            
          files_ignore: docs

      - name: Run step if any .js file(s) or any file in the static folder change
        if: steps.changed-files-excluded.outputs.any_changed == 'true'
        run: |
          echo "One or more .js file(s) or any file in the static folder but not in the doc folder has changed."
          echo "List all the files that have changed: ${{ steps.changed-files-excluded.outputs.all_changed_files }}"          

  job5:  # Example 5 - Using local .git directory
    runs-on: ubuntu-latest  # windows-latest | macos-latest
    name: Test changed-files
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # OR "2" -> To retrieve the preceding commit.

      - name: Get all test, doc and src files that have changed
        id: changed-files-yaml
        uses: tj-actions/changed-files@v37
        with:
          files_yaml: |
            doc:
              - *.md
              - docs/**
              - !docs/README.md
            test:
              - test/**
              - !test/README.md
            src:
              - src/**            
          # Optionally set `files_yaml_from_source_file` to read the YAML from a file. e.g `files_yaml_from_source_file: .github/changed-files.yml`

      - name: Run step if test file(s) change
        if: steps.changed-files-yaml.outputs.test_any_changed == 'true'
        run: |
          echo "One or more test file(s) has changed."
          echo "List all the files that have changed: ${{ steps.changed-files-yaml.outputs.test_all_changed_files }}"          
      
      - name: Run step if doc file(s) change
        if: steps.changed-files-yaml.outputs.doc_any_changed == 'true'
        run: |
          echo "One or more doc file(s) has changed."
          echo "List all the files that have changed: ${{ steps.changed-files-yaml.outputs.doc_all_changed_files }}"          

To access more examples, navigate to the Examples section.

If you feel generous and want to show some extra appreciation:

Support this project with a

Buy me a coffee

Useful Acronyms

Acronym Meaning
A Added
C Copied
M Modified
D Deleted
R Renamed
T Type changed
U Unmerged
X Unknown

Outputs

OUTPUT TYPE DESCRIPTION
added_files string Returns only files that are
Added (A).
added_files_count string Returns the number of added_files
all_changed_and_modified_files string Returns all changed and modified
files i.e. a combination of (ACMRDTUX)
all_changed_and_modified_files_count string Returns the number of all_changed_and_modified_files
all_changed_files string Returns all changed files i.e.
a combination of all added, copied, modified and renamed files (ACMR)
all_changed_files_count string Returns the number of all_changed_files
all_modified_files string Returns all changed files i.e.
a combination of all added, copied, modified, renamed and deleted files (ACMRD).
all_modified_files_count string Returns the number of all_modified_files
all_old_new_renamed_files string Returns only files that are
Renamed and lists their old
and new names. NOTE: This
requires setting include_all_old_new_renamed_files to true.
Also, keep in mind that
this output is global and
wouldn't be nested in outputs
generated when the *_yaml_* input
is used. (R)
all_old_new_renamed_files_count string Returns the number of all_old_new_renamed_files
any_changed string Returns true when any of
the filenames provided using the
files* or files_ignore* inputs has changed. i.e.
using a combination of all added, copied, modified and renamed files (ACMR).
any_deleted string Returns true when any of
the filenames provided using the
files* or files_ignore* inputs has been deleted.
(D)
any_modified string Returns true when any of
the filenames provided using the
files* or files_ignore* inputs has been modified.
i.e. using a combination of all added, copied, modified, renamed, and deleted files (ACMRD).
copied_files string Returns only files that are
Copied (C).
copied_files_count string Returns the number of copied_files
deleted_files string Returns only files that are
Deleted (D).
deleted_files_count string Returns the number of deleted_files
modified_files string Returns only files that are
Modified (M).
modified_files_count string Returns the number of modified_files
only_changed string Returns true when only files
provided using the files* or files_ignore* inputs
has changed. i.e. using a combination of all added, copied, modified and renamed files (ACMR).
only_deleted string Returns true when only files
provided using the files* or files_ignore* inputs
has been deleted. (D)
only_modified string Returns true when only files
provided using the files* or files_ignore* inputs
has been modified. (ACMRD).
other_changed_files string Returns all other changed files
not listed in the files
input i.e. using a combination of all added, copied, modified and renamed files (ACMR).
other_changed_files_count string Returns the number of other_changed_files
other_deleted_files string Returns all other deleted files
not listed in the files
input i.e. a combination of all deleted files (D)
other_deleted_files_count string Returns the number of other_deleted_files
other_modified_files string Returns all other modified files
not listed in the files
input i.e. a combination of all added, copied, modified, and deleted files (ACMRD)
other_modified_files_count string Returns the number of other_modified_files
renamed_files string Returns only files that are
Renamed (R).
renamed_files_count string Returns the number of renamed_files
type_changed_files string Returns only files that have
their file type changed (T).
type_changed_files_count string Returns the number of type_changed_files
unknown_files string Returns only files that are
Unknown (X).
unknown_files_count string Returns the number of unknown_files
unmerged_files string Returns only files that are
Unmerged (U).
unmerged_files_count string Returns the number of unmerged_files

Inputs

INPUT TYPE REQUIRED DEFAULT DESCRIPTION
api_url string false "${{ github.api_url }}" Github API URL.
base_sha string false Specify a different base commit
SHA used for comparing changes
diff_relative string false "true" Exclude changes outside the current
directory and show path names
relative to it. NOTE: This
requires you to specify the
top level directory via the
path input.
dir_names string false "false" Output unique changed directories instead
of filenames. NOTE: This returns
. for changed files located
in the current working directory
which defaults to $GITHUB_WORKSPACE.
dir_names_exclude_current_dir string false "false" Exclude the current directory represented
by . from the output
when dir_names is set to
true.
dir_names_max_depth string false Limit the directory output to
a maximum depth e.g test/test1/test2
with max depth of 2
returns test/test1.
escape_json string false "true" Escape JSON output.
fetch_depth string false "50" Depth of additional branch history
fetched. NOTE: This can be
adjusted to resolve errors with
insufficient history.
files string false File and directory patterns used
to detect changes (Defaults to the entire repo if unset) NOTE:
Multiline file/directory patterns should not
include quotes.
files_from_source_file string false Source file(s) used to populate
the files input.
files_from_source_file_separator string false "\n" Separator used to split the
files_from_source_file input
files_ignore string false Ignore changes to these file(s)
NOTE: Multiline file/directory patterns should
not include quotes.
files_ignore_from_source_file string false Source file(s) used to populate
the files_ignore input
files_ignore_from_source_file_separator string false "\n" Separator used to split the
files_ignore_from_source_file input
files_ignore_separator string false "\n" Separator used to split the
files_ignore input
files_ignore_yaml string false YAML used to define a
set of file patterns to
ignore changes
files_ignore_yaml_from_source_file string false Source file(s) used to populate
the files_ignore_yaml input. Example
files_ignore_yaml_from_source_file_separator string false "\n" Separator used to split the
files_ignore_yaml_from_source_file input
files_separator string false "\n" Separator used to split the
files input
files_yaml string false YAML used to define a
set of file patterns to
detect changes
files_yaml_from_source_file string false Source file(s) used to populate
the files_yaml input. Example
files_yaml_from_source_file_separator string false "\n" Separator used to split the
files_yaml_from_source_file input
include_all_old_new_renamed_files string false "false" Include all_old_new_renamed_files output. Note this
can generate a large output
See: #501.
json string false "false" Output list of changed files
in a JSON formatted string
which can be used for
matrix jobs.
old_new_files_separator string false " " Split character for old and
new renamed filename pairs.
old_new_separator string false "," Split character for old and
new filename pairs.
output_dir string false ".github/outputs" Directory to store output files.
output_renamed_files_as_deleted_and_added string false "false" Output renamed files as deleted
and added files.
path string false "." Specify a relative path under
$GITHUB_WORKSPACE to locate the repository.
quotepath string false "true" Use non-ascii characters to match
files and output the filenames
completely verbatim by setting this
to false
recover_deleted_files string false "false" Recover deleted files.
recover_deleted_files_to_destination string false Recover deleted files to a
new destination directory, defaults to
the original location.
separator string false " " Split character for output strings
sha string false Specify a different commit SHA
used for comparing changes
since string false Get changed files for commits
whose timestamp is older than
the given time.
since_last_remote_commit string false "false" Use the last commit on
the remote branch as the
base_sha. Defaults to the last
non-merge commit on the target
branch for pull request events
and the previous remote commit
of the current branch for
push events.
token string false "${{ github.token }}" Github token used to fetch
changed files from Github's API.
until string false Get changed files for commits
whose timestamp is earlier than
the given time.
write_output_files string false "false" Write outputs to the output_dir
defaults to .github/outputs folder. NOTE:
This creates a .txt file
by default and a .json
file if json is set
to true.

Versioning

This GitHub Action follows the principles of Semantic Versioning for versioning releases.

The format of the version string is as follows:

  • major: indicates significant changes or new features that may not be backward compatible.

  • minor: indicates minor changes or new features that are backward compatible.

  • patch: indicates bug fixes or other small changes that are backward compatible.

Examples

Get all changed files in the current branch
...
    - name: Get changed files
      id: changed-files
      uses: tj-actions/changed-files@v37
...
Get all changed files and using a comma separator
...
    - name: Get all changed files and use a comma separator in the output
      id: changed-files
      uses: tj-actions/changed-files@v37
      with:
        separator: ","
...

See inputs for more information.

Get all changed files and list all added files
...
    - name: Get changed files
      id: changed-files
      uses: tj-actions/changed-files@v37

    - name: List all added files
      run: |
        for file in ${{ steps.changed-files.outputs.added_files }}; do
          echo "$file was added"
        done        
...

See outputs for a list of all available outputs.

Get all changed files and optionally run a step if a file was modified
...
    - name: Get changed files
      id: changed-files
      uses: tj-actions/changed-files@v37

    - name: Run a step if my-file.txt was modified
      if: contains(steps.changed-files.outputs.modified_files, 'my-file.txt')
      run: |
        echo "my-file.txt file has been modified."        
...

See outputs for a list of all available outputs.

Get all changed files and write the outputs to a txt file
...

   - name: Get changed files and write the outputs to a txt file
     id: changed-files-write-output-files-txt
     uses: ./
     with:
       write_output_files: true

   - name: Verify the contents of the .github/outputs/added_files.txt file
     run: |
       cat .github/outputs/added_files.txt       
...
Get all changed files and write the outputs to a json file
...
   - name: Get changed files and write the outputs to a json file
     id: changed-files-write-output-files-json
     uses: ./
     with:
       json: true
       write_output_files: true

   - name: Verify the contents of the .github/outputs/added_files.json file
     run: |
       cat .github/outputs/added_files.json       
...
Get all changed files using a list of files
...
    - name: Get changed files
      id: changed-files
      uses: tj-actions/changed-files@v37
      with:
        files: |
          my-file.txt
          *.sh
          *.png
          !*.md
          test_directory
          **/*.sql          
...

See inputs for more information.

Get all changed files using a list of files and take action based on the changes
...
    - name: Get changed files
      id: changed-files-specific
      uses: tj-actions/changed-files@v37
      with:
        files: |
          my-file.txt
          *.sh
          *.png
          !*.md
          test_directory
          **/*.sql          

    - name: Run step if any of the listed files above change
      if: steps.changed-files-specific.outputs.any_changed == 'true'
      run: |
        echo "One or more files listed above has changed."        

    - name: Run step if only the files listed above change
      if: steps.changed-files-specific.outputs.only_changed == 'true'
      run: |
        echo "Only files listed above have changed."        

    - name: Run step if any of the listed files above is deleted
      if: steps.changed-files-specific.outputs.any_deleted == 'true'
      run: |
        for file in ${{ steps.changed-files-specific.outputs.deleted_files }}; do
          echo "$file was deleted"
        done        

    - name: Run step if all listed files above have been deleted
      if: steps.changed-files-specific.outputs.only_deleted == 'true'
      run: |
        for file in ${{ steps.changed-files-specific.outputs.deleted_files }}; do
          echo "$file was deleted"
        done        
...

See outputs for a list of all available outputs.

Get all changed files using a source file or list of file(s) to populate to files input
...
    - name: Get changed files using a source file or list of file(s) to populate to files input.
      id: changed-files-specific-source-file
      uses: tj-actions/changed-files@v37
      with:
        files_from_source_file: test/changed-files-list.txt
...

See inputs for more information.

Get changed files using a source file or list of file(s) to populate to files input and optionally specify more files
...
    - name: Get changed files using a source file or list of file(s) to populate to files input and optionally specify more files.
      id: changed-files-specific-source-file-and-specify-files
      uses: tj-actions/changed-files@v37
      with:
        files_from_source_file: |
          test/changed-files-list.txt          
        files: |
          test.txt          
...

See inputs for more information.

Get all changed files using a different SHA
...
    - name: Get changed files using a different SHA
      id: changed-files
      uses: tj-actions/changed-files@v37
      with:
        sha: ${{ github.event.pull_request.head.sha }}
...

See inputs for more information.

Get all changed files using a different base SHA
...
    - name: Get changed files using a different base SHA
      id: changed-files
      uses: tj-actions/changed-files@v37
      with:
        base_sha: ${{ github.event.pull_request.base.sha }}
...

See inputs for more information.

Get all changed files between the previous tag and the current tag
...
on:
  push:
    tags:
      - 'v*'

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v37

      - name: Get changed files in the .github folder
        id: changed-files-specific
        uses: tj-actions/changed-files@v37
        with:
          base_sha: ${{ steps.get-base-sha.outputs.base_sha }}
          files: .github/**

      - name: Run step if any file(s) in the .github folder change
        if: steps.changed-files-specific.outputs.any_changed == 'true'
        run: |
          echo "One or more files in the .github folder has changed."
          echo "List all the files that have changed: ${{ steps.changed-files-specific.outputs.all_changed_files }}"          
...

See inputs for more information.

Get all changed files for a repository located in a different path
...
    - name: Checkout into dir1
      uses: actions/checkout@v3
      with:
        fetch-depth: 0
        path: dir1

    - name: Run changed-files with defaults in dir1
      id: changed-files-for-dir1
      uses: tj-actions/changed-files@v37
      with:
        path: dir1

    - name: List all added files in dir1
      run: |
        for file in ${{ steps.changed-files-for-dir1.outputs.added_files }}; do
          echo "$file was added"
        done        
...

See inputs for more information.

Get all changed files with non äšćįí characters i.e (Filename in other languages)
...
    - name: Run changed-files with quotepath disabled
      id: changed-files-quotepath
      uses: tj-actions/changed-files@v37
      with:
        quotepath: "false"

    - name: Run changed-files with quotepath disabled for a specified list of file(s)
      id: changed-files-quotepath-specific
      uses: ./
      with:
        files: test/test-è.txt
        quotepath: "false"
...

See inputs for more information.

Get all changed files using the last successful commit of the base branch
  • Push event
    ...
          - name: Get branch name
            id: branch-name
            uses: tj-actions/branch-names@v6
    
          - uses: nrwl/nx-set-shas@v3
            id: last_successful_commit_push
            with:
              main-branch-name: ${{ steps.branch-name.outputs.current_branch }} # Get the last successful commit for the current branch.
              workflow-id: 'test.yml'
    
          - name: Run changed-files with the commit of the last successful test workflow run
            id: changed-files-base-sha-push
            uses: tj-actions/changed-files@v37
            with:
              base_sha: ${{ steps.last_successful_commit_push.outputs.base }}
    ...
    
  • Pull request events
    ...
          - name: Get branch name
            id: branch-name
            uses: tj-actions/branch-names@v5
    
          - uses: nrwl/nx-set-shas@v3
            id: last_successful_commit_pull_request
            with:
              main-branch-name: ${{ steps.branch-name.outputs.base_ref_branch }} # Get the last successful commit on master or main branch
              workflow_id: 'test.yml'
    
          - name: Run changed-files with the commit of the last successful test workflow run on main
            id: changed-files-base-sha-pull-request
            uses: tj-actions/changed-files@v37
            with:
              base_sha: ${{ steps.last_successful_commit_pull_request.outputs.base }}
    ...
    

Warning

: This setting overrides the commit sha used by setting since_last_remote_commit to true. It is recommended to use either solution that works for your use case.

See inputs for more information.

Get all changed files but only return the directory names
...
    - name: Run changed-files with dir_names
      id: changed-files-dir-names
      uses: tj-actions/changed-files@v37
      with:
        dir_names: "true"
...

See inputs for more information.

Get all changed files and return JSON formatted outputs
...
    - name: Run changed-files with json output
      id: changed-files-json
      uses: tj-actions/changed-files@v37
      with:
        json: "true"
...

See inputs for more information.

Get all changed files by commits pushed in the past
...
    - name: Get changed-files since 2022-08-19
      id: changed-files-since
      uses: tj-actions/changed-files@v37
      with:
        since: "2022-08-19"

    - name: Get changed-files until 2022-08-20
      id: changed-files-until
      uses: tj-actions/changed-files@v37
      with:
        until: "2022-08-20"
...

See inputs for more information.

Real world usage

image

And many more...

Known Limitation

Warning

:

  • Spaces in file names can introduce bugs when using bash loops. See: #216 However, this action will handle spaces in file names, with a recommendation of using a separator to prevent hidden issues.

    Screen Shot 2021-10-23 at 9 37 34 AM

Migration guide

With the switch from using grep's Extended regex to match files to the natively supported workflow glob pattern matching syntax introduced in v13 you'll need to modify patterns used to match files.

...
      - name: Get specific changed files
        id: changed-files-specific
        uses: tj-actions/changed-files@v24
        with:
          files: |
-            \.sh$
-            .(sql|py)$
-            ^(dir1|dir2)
+            *.{sh,sql,py}
+            dir1
+            dir2

Credits

This package was created with cookiecutter-action.

Report Bugs

Report bugs at https://github.com/tj-actions/changed-files/issues.

If you are reporting a bug, please include:

  • Your operating system name and version.
  • Any details about your workflow that might be helpful in troubleshooting. (NOTE: Ensure that you include full log outputs with debugging enabled)
  • Detailed steps to reproduce the bug.

Contributors

Thanks goes to these wonderful people (emoji key):

Josh Soref
Josh Soref

📖
Nick Landers
Nick Landers

💻
Krasimir Nikolov
Krasimir Nikolov

💻 📖
Ivan Pizhenko
Ivan Pizhenko

💻 📖
talva-tr
talva-tr

💻
Ikko Ashimine
Ikko Ashimine

📖
James
James

📖
James Cheng
James Cheng

📖
Masaya Suzuki
Masaya Suzuki

💻
fagai
fagai

📖
Constantine Peresypkin
Constantine Peresypkin

💻
Mathieu Dupuy
Mathieu Dupuy

📖
Joe Moggridge
Joe Moggridge

📖
Charles Santos
Charles Santos

💻
Kostiantyn Korniienko
Kostiantyn Korniienko

📖
Logan Pulley
Logan Pulley

💻
Kenji Miyake
Kenji Miyake

💻
adonisgarciac
adonisgarciac

💻 📖
Chiel Fernhout
Chiel Fernhout

📖
Alberto Perdomo
Alberto Perdomo

📖

This project follows the all-contributors specification. Contributions of any kind welcome!