DEV Community

Peter Mekhaeil
Peter Mekhaeil

Posted on • Edited on

9 3

Using GitHub Actions to push changes

We can use GitHub Actions to push a new commit each time there is a new change detected.

I've recently had to do this to automate updating README.md with a listing of the repository files each time a new push has been detected. The GitHub workflow is found here.

Here are the learnings that may come useful to others:

Using bash to clear file content

cat /dev/null > README.md
Enter fullscreen mode Exit fullscreen mode

/dev/null is a pseudo file in Linux that has no output so we can override a file with this empty content.

Using bash to echo a string with new line

echo -e '# Today I Learned\n' > README.md
Enter fullscreen mode Exit fullscreen mode

-e is required to escape backslashes. This allows us to print new line (\n) when outputting to a file.

Using bash to read the first line of a file

head -n 1 $filename
Enter fullscreen mode Exit fullscreen mode

Using bash to remove characters from a string

echo '# Title' | sed 's/# //'
Enter fullscreen mode Exit fullscreen mode

sed is short for Stream EDitor and one of its common uses is pattern replacement. The above will remove # from the string (by replacing it with an empty string).

Using bash to append string to file

echo 'My string' >> README.md
Enter fullscreen mode Exit fullscreen mode

The >> operator is used to append to a file (or create the file if it does not exist).

Using GitHub Actions to push changes to a repository

actions/checkout is an offical GitHub Action that can checkout a repository. We can also use this to push changes back.

on: push
jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: |
          # Clear README.md
          cat /dev/null > README.md

          # Add Title
          echo -e '# Today I Learned\n' > README.md

          # Loop through all TILs and add to README.md
          dir=./learnings
          for filename in "$dir"/*
          do
            title=$(head -n 1 $filename | sed 's/# //')
            echo "- [$title](https://github.com/petermekhaeil/til/blob/master/$filename)" >> README.md
          done

          # Push changes
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add README.md
          git commit -m "Update README.md"
          git push
Enter fullscreen mode Exit fullscreen mode

Today I Learned
I share what I learn on Today I Learned.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
lukeecart profile image
Luke Cartwright

Great Article! Thank you 🤘👏

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay