[GitHub Actions] fetch-depth를 통해 한 개 이상의 commit을 가져오기


환경

  • GitHub Actions


배경

  • GitHub Actions를 사용할 때, Job에서 과거 commit 이력들을 불러와야 하는 상황이 있어서 찾아본 내용을 정리한다.


GitHub Actions Checkout fetch-depth

사용법

  • 기본값이 1이라 하나의 commit만 가져오며 더 가져오고 싶으면 아래처럼 사용하면 된다.
- uses: actions/checkout@v4
  with:
    # Number of commits to fetch. 0 indicates all history for all branches and tags.
    # Default: 1
    fetch-depth: 0


예시

workflow 파일 예시

  • fetch-depth에 5을 설정해 5개를 가져온다.
name: Test Git Log Workflow

on:
  workflow_dispatch:
    inputs:
      CHECKOUT_REF:
        required: false
        type: string
        default: main

jobs:
  print_git_commit_log_without_fetch_depth:
    name: job name 1
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Repository
      uses: actions/checkout@v4
    - name: Print Commit Hash
      run: echo $GITHUB_SHA
    - name: Print Git Log
      run: git log
  print_git_commit_log_with_fetch_depth:
    name: job name 2
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Repository
      with:
        fetch-depth: 5 # fetch recent five commits
      uses: actions/checkout@v4
    - name: Print Commit Hash
      run: echo $GITHUB_SHA
    - name: Print Git Log
      run: git log

실행 결과

  • 아래 결과를 보면 commit이 1개가 아닌 5개를 가져왔다.
  git log
  shell: /usr/bin/bash -e {0}
commit 5d9dfa3701670b5ac034829d2e421202b9eebc11
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:57:08 2025 +0900

    Update test-git-log.yml
  git log
  shell: /usr/bin/bash -e {0}
commit 5d9dfa3701670b5ac034829d2e421202b9eebc11
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:57:08 2025 +0900

    Update test-git-log.yml

commit 78bbeaf925e2f4ec0d17c32d84d7cebcfcce0fcf
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:55:07 2025 +0900

    Update and rename test-git-log to test-git-log.yml

commit 2de68ba14ebe93aa444b701350ae4a44f81d9c5b
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:54:02 2025 +0900

    Update test-git-log

commit 17bc45af78267dfc634b30d24358c2c878b660f8
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Thu May 1 13:52:48 2025 +0900

    Create test-git-log

commit 9c9a2a3e7c58bfa02c4c024186757a721d40e9ee
Author: Taewoo Lee <TWpower@users.noreply.github.com>
Date:   Fri Nov 15 18:22:07 2024 +0900

    Merge pull request #55 from TWpower/workflow-complete-event-release-7cef35c
    
    release(devops): Update Workflow Complete Event Workflows


의견

  • CI/CD 작성 시, 과거 커밋들을 가져오는 코드가 있었는데 계속 오류가 나서 찾아보게 되었다. 생각해보면 전체를 가져오는 것은 낭비일 수 있으니, 이런 옵션을 제공하는 것이 맞는 것 같다. 페이지네이션(Pagination) 개념처럼, 모든 데이터를 한 번에 로드하지 않는 경우가 꽤 많다.


참고자료