如何在github操作中获取存储库的提交计数?

Sar*_*une 0 git github-actions github-actions-self-hosted-runners

对于我们的应用程序构建号,我们使用分支构建中存储库迄今为止的提交总数。

这是之前使用的实现的

git log --oneline | wc -l
Run Code Online (Sandbox Code Playgroud)

之前我们使用jenkins,现在我们正在更改为github actions。

当尝试使用类似的工作流程步骤来计算提交计数时,每次只给出 1。

我的工作流程。

# 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
Run Code Online (Sandbox Code Playgroud)

Krz*_*iak 8

如果您查看actions/checkout存储库,您会发现默认情况下它仅获取单个提交。您可以使用以下fetch-depth参数更改此设置:

 - uses: actions/checkout@v3
   with:
     fetch-depth: 0
Run Code Online (Sandbox Code Playgroud)

从结帐的自述文件:

0 表示所有分支和标签的所有历史记录。