Adding Total Commit Days badge at repository

·

2 min read

Summary

  1. Add badge at README.md in advance which will be updated later on.

  2. Make a Github Actions workflow that do things as follows:

    1. count total commit days of current repository

    2. find and replace your badge at README.md

    3. commit the updated README.md and push

Result: https://github.com/TIL-challenge/gwonhong

Adding badge

![Total Commit Days](https://img.shields.io/badge/Total%20Commit%20Days-0-brightgreen)

Copy & Paste the above text anywhere you want to put inside README.md. It will add the badge with count 0.

Write Github Actions workflow

Add config file to the repository

Make a .yml file at following path: .github/workflows/commit-counter.yml (The file name don’t have to be the same as mine!)

Write the workflow

Read carefully, I wrote all the time related things to run correctly on Korea. Adjust them as you want!

name: Update Commit Counters

on:
  schedule:
    - cron: '0 15 * * *'  # Runs at 3:00 PM UTC, which is 12:00 AM KST
  workflow_dispatch: # Enables manual run

jobs:
  update_commit_stats:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # to get all the commit logs

      - name: Get Commit Days (Excluding github-actions)
        id: commit_days
        run: |
          # I live in Korea, so set the timezone to Korea Standard Time (KST).
          export TZ="Asia/Seoul"

          # Get total commit days excluding commits made by "github-actions"
          total_days=$(git log --pretty="%cd %cn" --date=short | grep -v "github-actions" | cut -d ' ' -f1 | sort | uniq | wc -l)
          echo "total_days=$total_days" >> "$GITHUB_ENV" # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/store-information-in-variables

      - name: Update README with Shields.io badges
        run: |
          # Construct the shields.io badge URLs
          total_commit_badge="![Total Commit Days](https://img.shields.io/badge/Total%20Commit%20Days-$total_days-brightgreen)"

          # Replace the existing badges in README.md
          sed -i 's|!\[Total Commit Days\](https://img.shields.io/badge/Total%20Commit%20Days-[0-9]*-brightgreen)|'"$total_commit_badge"'|g' README.md

          # Push the changes back to the repository
          git config --global user.name "github-actions"
          git config --global user.email "github-actions@github.com"
          git add README.md
          git commit -m "Update commit days badges"
          git push

This will update the badge at README.md showing total commit days of your repository every 00:00 KST.

How to customize Badge

  1. update README.md first.

  2. carefully update the sed command at workflow(the .yml file) to match your updated badge.

How to run manually

1. Go to your GitHub repository.

2. Click on the “Actions” tab.

3. Select the workflow you want to run (it will be listed by the name you defined in the name field of the YAML file).

4. Click the “Run workflow” button, which should appear on the right side if the workflow has the workflow_dispatch trigger enabled.

5. If there are any inputs defined, you will be able to specify them, otherwise just hit Run workflow to execute it immediately.