Skip to content

jacobtomlinson/gha-find-replace

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Find and Replace Action

GitHub Marketplace Build Integration Tests Unit Tests

This action will find and replace strings in your project files.

Usage

Example workflow

This example replaces hello with world in all of your project files.

name: My Workflow
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "hello"
          replace: "world"
          regex: false

Inputs

Input Description
find A string to find and replace in your project files. (Defaults to a regular expression.)
replace The string to replace it with.
include (optional) A glob of files to include. Defaults to **.
exclude (optional) A glob of files to exclude. Defaults to .git/** .
regex (optional) Whether to match with.find as a regular expression instead of a fixed string. Defaults to true.

Outputs

Output Description
modifiedFiles The number of files that have been modified

Examples

Including a subdirectory

You can limit your find and replace to a directory.

name: My Workflow
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "hello"
          replace: "world"
          include: "justthisdirectory/**"
          regex: true

Filter by file name

You can limit your find and replace to just files with a specific name.

name: My Workflow
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "hello"
          replace: "world"
          include: "**README.md" # Will match all README.md files in any nested directory

Exclude by file type

You can set your find and replace to ignore certain file types.

name: My Workflow
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "hello"
          replace: "world"
          exclude: "**/*.py" # Do not modify Python files

If you are getting an error message that reads, "refusing to allow a GitHub App to create or update workflow ..." it means that your GitHub action may be trying to edit your new workflow file. Omit this file from the find-and-replace search with exclude: ..

name: My Workflow
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "hello"
          replace: "world"
          exclude: . # Do not modify this file

Pushing changes back

Any modifications during a GitHub Actions workflow are only made to the working copy checked out by the actions/checkout step. If you want those changes to be pushed back to the repository you'll need to add a final step that does this. You will have to give your workflow write permissions.

name: My Workflow
on: [push, pull_request]
jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "hello"
          replace: "world"
          regex: false

      - name: Push changes
        uses: stefanzweifel/git-auto-commit-action@v4

If you need the push event to trigger other workflows, use a repo scoped Personal Access Token.

      - name: Push changes
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ secrets.MY_PAT }}
          branch: ${{ github.ref }}

Contributing

Install go. This project currently uses Go 1.23. We recommend gvm to switch between multiple go versions.

$ go version
go version go1.23.2 darwin/amd64

Build and test.

go test
go build -v .

Make your code changes, add tests, and make a pull request!

Publishing

To publish a new version of this Action we need to update the Docker image tag in action.yml and also create a new release on GitHub.

  • Work out the next tag version number.
  • Update the Docker image in action.yml.
  • Create a new release on GitHub with the same tag.

About

A GitHub Action to find and replace strings in project files.

Resources

License

Stars

Watchers

Forks

Packages