StepSecurity Maintained changed-files Action
StepSecurity Maintained Actions are usually exclusive to StepSecurity enterprise customers. However, in an effort to help the community with the tj-actions/changed-files security incident, we are making this StepSecurity Maintained Action freely available to everyone.
Overview
Effortlessly track all changed files and directories relative to a target branch, the current branch (preceding commit or the last remote commit), multiple branches, or custom commits returning relative paths from the project root using this GitHub action.
Table of contents
Features ๐
- Fast execution, averaging 0-10 seconds.
- Leverages either Github's REST API or Git's native diff command to determine changed files.
- Facilitates easy debugging.
- Scales to handle large/mono repositories.
- Supports Git submodules.
- Supports merge queues for pull requests.
- Generates escaped JSON output for running matrix jobs based on changed files.
- Lists changed directories.
- Limits 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 fetching a fixed number of commits, which improves performance.
- 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:
- Provides boolean output indicating changes in specific files.
- Uses Glob pattern matching.
- Supports Globstar.
- Supports brace expansion.
- Supports negation.
- Uses YAML syntax for specifying patterns.
And many more...
Usage ๐ป
[!IMPORTANT]
- Push Events: When configuring
actions/checkout, make sure to set fetch-depth to either 0 or 2, depending on your use case.
- Mono Repositories: To avoid pulling the entire branch history, you can utilize the default
actions/checkout's fetch-depth of 1 for pull_request events.
- Quoting Multiline Inputs: Avoid using single or double quotes for multiline inputs. The value is already a string separated by a newline character. Refer to the Examples section for more information.
- Credentials Persistence: If
fetch-depth is not set to 0, make sure to set persist-credentials to true when configuring actions/checkout.
- Matching Files and Folders: To match all files and folders under a directory, this requires a globstar pattern e.g.
dir_name/** which matches any number of subdirectories and files.
Visit the discussions for more information or create a new discussion for usage-related questions.
Detect changes to all files in a Pull request relative to the target branch or since the last pushed commit.
Using local .git directory ๐
name: CI
on:
pull_request:
branches:
- main
jobs:
# ------------------------------------------------------------------------------------------------------------------------------------------------
# Event `pull_request`: Compare the last commit of the main branch or last remote commit of the PR branch -> to the current commit of a PR branch.
# ------------------------------------------------------------------------------------------------------------------------------------------------
changed_files:
runs-on: ubuntu-latest # windows-latest || macos-latest
name: Test changed-files
steps:
- uses: actions/checkout@v6
# -----------------------------------------------------------------------------------------------------------
# Example 1
# -----------------------------------------------------------------------------------------------------------
- name: Get changed files
id: changed-files
uses: step-security/changed-files@v47
# 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
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
for file in ${ALL_CHANGED_FILES}; do
echo "$file was changed"
done
# -----------------------------------------------------------------------------------------------------------
# Example 2
# -----------------------------------------------------------------------------------------------------------
- name: Get all changed markdown files
id: changed-markdown-files
uses: step-security/changed-files@v47
with:
# Avoid using single or double quotes for multiline patterns
files: |
**.md
docs/**.md
- name: List all changed files markdown files
if: steps.changed-markdown-files.outputs.any_changed == 'true'
env:
ALL_CHANGED_FILES: ${{ steps.changed-markdown-files.outputs.all_changed_files }}
run: |
for file in ${ALL_CHANGED_FILES}; do
echo "$file was changed"
done
# -----------------------------------------------------------------------------------------------------------
# Example 3
# -----------------------------------------------------------------------------------------------------------
- name: Get all test, doc and src files that have changed
id: changed-files-yaml
uses: step-security/changed-files@v47
with:
files_yaml: |
doc:
- '**.md'
- docs/**
test:
- test/**
- '!test/**.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
# NOTE: Ensure all outputs are prefixed by the same key used above e.g. `test_(...)` | `doc_(...)` | `src_(...)` when trying to access the `any_changed` output.
if: steps.changed-files-yaml.outputs.test_any_changed == 'true'
env:
TEST_ALL_CHANGED_FILES: ${{ steps.changed-files-yaml.outputs.test_all_changed_files }}
run: |
echo "One or more test file(s) has changed."
echo "List all the files that have changed: $TEST_ALL_CHANGED_FILES"
- name: Run step if doc file(s) change
if: steps.changed-files-yaml.outputs.doc_any_changed == 'true'
env:
DOC_ALL_CHANGED_FILES: ${{ steps.changed-files-yaml.outputs.doc_all_changed_files }}
run: |
echo "One or more doc file(s) has changed."
echo "List all the files that have changed: $DOC_ALL_CHANGED_FILES"
# -----------------------------------------------------------------------------------------------------------
# Example 4
# -----------------------------------------------------------------------------------------------------------
- name: Get changed files in the docs folder
id: changed-files-specific
uses: step-security/changed-files@v47
with:
files: docs/*.{js,html} # Alternatively using: `docs/**`
files_ignore: docs/static.js
- name: Run step if any file(s) in the docs folder change
if: steps.changed-files-specific.outputs.any_changed == 'true'
env:
ALL_CHANGED_FILES: ${{ steps.changed-files-specific.outputs.all_changed_files }}
run: |
echo "One or more files in the docs folder has changed."
echo "List all the files that have changed: $ALL_CHANGED_FILES"
Using Github's API :octocat:
name: CI
on:
pull_request:
branches:
- main
jobs:
# -------------------------------------------------------------
# Event `pull_request`: Returns all changed pull request files.
# --------------------------------------------------------------
changed_files:
# NOTE:
# - This is limited to pull_request* events and would raise an error for other events.
# - A maximum of 3000 files can be returned.
# - For more flexibility and no limitations see "Using local .git directory" above.
runs-on: ubuntu-latest # windows-latest || macos-latest
name: Test changed-files
permissions:
pull-requests: read
steps:
- name: Get changed files
id: changed-files
uses: step-security/changed-files@v47
- name: List all changed files
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
for file in ${ALL_CHANGED_FILES}; do
echo "$file was changed"
done
On push โฌ๏ธ
Detect changes to files made since the last pushed commit.