Search code examples
gitgithub-actionsgithub-actions-self-hosted-runners

How to get commit count of the repository in github actions?


For our application build number, we are using the total number of commits till date of the repository in the branch building.

This one was achieved earlier using

git log --oneline | wc -l

Earlier we used jenkins and now we are changing to github actions.

When tried the similar workflow step to calculate the count of commits, this is giving only 1 every time.

My workflow.

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ r12.1_githubactions ]
  pull_request:
    branches: [ r12.1_githubactions ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: DevBuild1

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v3
      
      # Runs a single command using the runners shell
      - name: Run a one-line script
        run: |
          echo "checking count"
          $count = git log --oneline | wc -l
          echo $count

Solution

  • If you have a look at the actions/checkout repo, you will notice that by default it fetches only a single commit. You can change this using the fetch-depth parameter:

     - uses: actions/checkout@v3
       with:
         fetch-depth: 0
    

    From the checkout's readme:

    0 indicates all history for all branches and tags.