user3310334
user3310334

Reputation:

Run GitHub workflow only when a commit creates a new file

I would like to make a GitHub workflow that runs some find-and-replace substitutions on new files that are committed to the repository.

I can't find where to start because I can't find a workflow trigger that seems to be able to detect when a new file is committed.

What set up could I use to run the substitutions?

Upvotes: 1

Views: 345

Answers (1)

Matteo
Matteo

Reputation: 39430

I would suggest you use the dorny/paths-filter GitHub Actions to discover if a file is added in your PR, as an example:

- uses: dorny/paths-filter@v2
  id: filter
  with:
    # Enable listing of files matching each filter.
    # Paths to files will be available in `${FILTER_NAME}_files` output variable.
    # Paths will be escaped and space-delimited.
    # Output is usable as command-line argument list in Linux shell
    list-files: shell

    # Changed file can be 'added', 'modified', or 'deleted'.
    # By default, the type of change is not considered.
    # Optionally, it's possible to specify it using nested
    # dictionary, where the type of change composes the key.
    # Multiple change types can be specified using `|` as the delimiter.
    filters: |
      added:
        - added: '**'

Then run step only in case of adding:

- name: Do some stuff on added files
  if: steps.filter.outputs.added == 'true'
  run: |
    for file in ${{ steps.filter.outputs.added_files }}; do
      echo "${file} added"
    done

See the outputs section for further details.

Upvotes: 2

Related Questions