Replace a string on commit using a github action

2 min read

Using a github action we can make it search for a given string an replace it for another when we commit the data. It can be useful to fix common mistakes

The GitHub action would look like this:

name: find&replace strings

on:
  push:
    branches:
      - master

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

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@master
        with:
          find: "Comunitat Autònoma de Catalunya"
          replace: "República de Catalunya"

      - name: Push changes
        uses: EndBug/add-and-commit@v7
        with:
          message: 'fix str'

It is configured to trigger for each commit we push to the master branch:

on:
  push:
    branches:
      - master

It will checkout the repo, replace the strings and commit the changes back to the repo:

    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Find and Replace
        uses: jacobtomlinson/gha-find-replace@master
        with:
          find: "Comunitat Autònoma de Catalunya"
          replace: "República de Catalunya"

      - name: Push changes
        uses: EndBug/add-and-commit@v7
        with:
          message: 'fix str'

Bear in mind that we shouldn't use this approach for secrets, since it will create a new commit with the change.


Posted on 01/10/2021