PythonicIT Blog

Deep dives on automation, security, architecture, and practical implementation lessons from real client work.

Automating README Updates in GitHub Repositories with GitHub Actions


In the world of software development, Continuous Integration and Continuous Delivery (CI/CD) are key practices that help in automating the software delivery process. They enable developers to integrate code changes more frequently and reliably, and automate the testing and deployment of applications. GitHub Actions plays a pivotal role in this domain, providing a powerful platform for automating workflows within your GitHub repositories.

GitHub Actions allows you to create custom workflows that automatically build, test, and deploy your code. Think of it as a way to connect GitHub's vast ecosystem with the specific needs of your project. Whether it's triggering tests on every push, deploying code directly from your repository, or automating the management of issues and pull requests, GitHub Actions offers flexibility and control in automating your software workflows.

In this blog post, we'll explore a specific use case of GitHub Actions: automating the process of updating README files in GitHub repositories. This can be particularly useful when working with template repositories, as it enables automatic customization of README.md files to include the repository's name. This not only saves time but also ensures accuracy and consistency in project documentation. Let's dive into how you can set up this automation to dynamically update the first header of your README.md file with the repository's name using GitHub Actions.

Setting up the repo

  1. Start by creating a new Repo or use an existing repo that you would want to use for this example.
  2. Create the README.md (If you haven't done so already) and add the following to the top of the file:

# Introduction for <reponame>

  • We will be using the placeholder <reponame> later.

Setting up the repo

  1. Create a .yml file in the .github/workflows directory of your repository. If you don't have that directory, you will need to create it. You can name it anything you want like readmeupdate.yml
  2. Use the following example to dynamically set the Repo name in your README.md:
name: Dynamic Template

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  update_templates:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@main
      - run: echo "REPO_NAME=${{ github.event.repository.name }}" >> $GITHUB_ENV

      # Runs a set of commands using the runners shell
      - name: Update README.md
        run: |
          sed -i 's/<reponame>/'${{ github.event.repository.name }}'/' README.md
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config user.name "github-actions[bot]"
          git commit -am "Automated report"
          git push

Lets break down the github action:

  • This github action will only run if someone pushes to the main branch.
  • actions/checkout@main: This is to make sure that the github action runner is in the correct branch.
  • The sed command is where the magic happens. It's a linux parsing command that allows you to replace strings with other data. In this case we are replacing the <reponame> with the github.event.repository.name which is your repo's current name.
  • We use a dependabot to commit and push the new information to the repo so that the README.md is updated. You don't have to use dependabot, as you can use any git user as you see fit. For more information on the dependabot you can follow this link - https://github.com/orgs/community/discussions/26560#discussioncomment-3531273

Conclusion

By using this method, you can ensure your README.md always includes the latest repository name, making your documentation more dynamic and accurate. This automation not only saves time but also helps in maintaining consistency across your projects.