Can github actions edit parts of README.md?

Viewed 31

I want to update ONLY the first header of a readme so it always has the repo's name. I know how to get the repo name in github actions by doing something like:

name: CI

on:
 push:

jobs:
 build:
 runs-on: ubuntu-latest

 steps:
      - uses: actions/checkout@v3

      - name: Run a one-line script
 run: echo "REPO_NAME=${{ github.event.repository.name }}" >> $GITHUB_ENV

However, I want to access my readme.md and add the 'github.event.repository.name' to the header. So I would make a Readme with something like:

Introduction for: {{ github.event.repository.name }}

hoping I can get something like this with gitactions:

Introduction for: RepoName

I tried using some marketplace github actions but the one I tried seems to not do variables and it seems to update the whole readme.md not just the header: https://github.com/marketplace/actions/dynamic-readme

Here is the failed example for this marketplace plugin:

name: Dynamic Template

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  update_templates:
    name: "Update Templates"
    runs-on: ubuntu-latest
    steps:
      - name: "  Fetching Repository Contents"
        uses: actions/checkout@main

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo "REPO_NAME=${{ github.event.repository.name }}" >> $GITHUB_ENV


      # Runs a set of commands using the runners shell
      - name: Update GitHub Profile README
        uses: theboi/github-update-readme@v1.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPO_NAME: ${{ github.event.repository.name }}
        with:
          header: $REPO_NAME

Is there any way to make the readme.md file have the repo name dynamically with a variable in github actions?

EDIT: I think I am very close but I don't know how to commit code in github actions. I figured, I can do this manually by using the sed command in bash in github actions. I think it worked but I think I have to commit the code to make it save. Here is the code I have so far:

name: Dynamic Template

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  update_templates:
    name: "Update Templates"
    runs-on: ubuntu-latest
    steps:
      - name: "  Fetching Repository Contents"
        uses: actions/checkout@main

      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: echo "REPO_NAME=${{ github.event.repository.name }}" >> $GITHUB_ENV


      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          ls
          sed -i 's/<reponame>/$REPO_NAME/' README.md
          cat README.md
          echo $REPO_NAME
1 Answers

I figured it out. You can it manually by using the sed command in bash within a runner in github actions. Set your README.md with a variable that you want to replace like <reponame> then use github actions to find that string and replace it with something you want (for me the repo name).

name: Dynamic Template

on:
  create:

jobs:
  update_templates:
    name: "Update Templates"
    runs-on: ubuntu-latest
    steps:
      - name: "  Fetching Repository Contents"
        uses: actions/checkout@main

      # 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

The email I used is a dependabot mentioned from here: https://github.com/orgs/community/discussions/26560#discussioncomment-3531273

Related