Search code examples
gitlogginggithub-actionsreadme

README has failed to update using Github Action


I want to update a section of the README file in a github repo using github actions. The section of the README is clearly marked with a comment:

<!-- start-quote -->
<!-- end-quote -->

I have written this gihub worflow to update the section.

name: readme-quote

on:
  push:
  schedule:
    - cron: '0 */12 * * *'

jobs:
  add_quote:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Get an inspirational quote
      run: |
        QUOTE="This quote is fetched from a remote api using CURL"
        echo "$QUOTE"

    - name: Update quote in readme
      run: |-
        # Set a default quote if the 
        echo "${QUOTE:=>Silence is Golden}"
        # Disable interpreting exclamation mark! as a command
        histchars=
        # Set delimiters to variables
        start_delimiter="<!-- start-quote -->"
        end_delimiter="<!-- end-quote -->"
        # Remove previous quote with its delimter
        sed -i "/$start_delimiter/,/$end_delimiter/d" README.md
        # Add the new quote to the README file
        printf "$start_delimiter\n$QUOTE\n$end_delimiter\n" >> README.md

    - name: Commit and push changes if README changed
      run: |-
        git diff
        git config --local user.email "[email protected]"
        git config --local user.name "GitHub Actions"
        git add README.md
        git diff --quiet || (git add README.md && git commit -m "Added new quote to README")
        git push origin main

All steps are successful on the local machine and the workflow succeeds ✔ in github actions but the README file is not updated! Surprisingly, part of the logs show that the files has been updated.

Run git diff
  git diff
  git config --local user.email "[email protected]"
  git config --local user.name "GitHub Actions"
  git add README.md
  git diff --quiet || (git add README.md && git commit -m "Added new quote to README")
  git push origin main
  shell: /usr/bin/bash -e {0}
diff --git a/README.md b/README.md
index 94b5482..ce225aa 100644
--- a/README.md
+++ b/README.md
@@ -27,4 +27,5 @@

 <!-- start-quote -->
-<!-- end-quote -->
\ No newline at end of file
+> Silence is Golden
+<!-- end-quote -->
Everything up-to-date

Solution

  • First you run git add README.md. After that git diff --quiet exits with code 0 so the next || (git add README.md && git commit doesn't run. And without commit there is nothing to push.

    Remove the first git add README.md.