okaryo.log

Fetching All Commit History in a Repository with GitHub Actions | okaryo.log

Fetching All Commit History in a Repository with GitHub Actions

    #GitHubActions#GitHub#CI/CD

Introduction

In a GitHub Actions workflow, there was a need to retrieve the total number of commits in a repository using the following command:

git rev-list HEAD --count

However, no matter what, it only outputted 1. Upon debugging, it appeared that only the most recent commit was present.

Cause and Solution

When using the actions/checkout@v3 action to check out the repository in GitHub Actions, it turns out that by default, only a single commit is fetched.

According to the official documentation:

Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags. Refer here to learn which commit $GITHUB_SHA points to for different events.

As mentioned above, to retrieve the entire commit history, fetch-depth: 0 needed to be specified.

Here is an example GitHub Actions configuration:

jobs:
  test:
    name: Run test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0 # Specify this
      - run: git rev-list HEAD --count # Now it outputs the total number of commit history!

With this, the complete commit history can be retrieved.

Conclusion

The actions/checkout@v3 action, which was only used with - uses: actions/checkout@v3 before, has various additional options available. It’s always recommended to glance through the README of any tool before using it.


Related Posts
Related Posts
Promotion

This site uses Google Analytics.