Software Engineering/Git, SVN

Github action nothing to commit, working tree clean 오류 해결

컴슈터 2024. 3. 30. 12:25

문제 발생

Git Action을 이용해서 티스토리와 Github를 서로 연동하다가 build failed이 발생했다. 


내 경우 git add .를 수행하다가 발생했는데, 다른 명령어를 수행하다가 동일한 에러가 발생할 수 있다.

Github action nothing to commit, working tree clean
Error: Process completed with exit code 1.

 

main.yml 파일에서 아래 명령어를 순차적으로 실행하다가 ✅ 표시된 부분에서 오류가 발생한 것이다. 빌드 시점에 새롭게 추가된 내용이 없는데  git add .를 수행하려고 하니 오류가 발생한 것이다.

jobs:
  # This workflow contains a single job called "build"
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16
          
      - name: Install dependencies
        run: | 
          npm ci
          npm install rss-parser
          
      - name: Update README
        run: npm start
        
      - name: Commit README
        run: |
          git add .     // ✅ 이 부분에서 에러 발생
          git config --local user.email "본인의 Github 이메일"
          git config --local user.name "본인의 Github 이름"
          git commit -m "Update README.md"
          git push origin main

해결 방법

그렇다면 빌드를 수행할 시점에 추가된 내용이 존재하는지 체크한 다음, 내용이 없다면 커밋하거나 푸시하지 않으면 된다. Git 명령어 diff를 이용해서 소스 변경 여부를 체크한다. 소스에서 추가되거나 변경된 내용이 없으면 문제가 발생한 부분을 아예 실행하지 않는다. 

jobs:
  # This workflow contains a single job called "build"
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 20

      - name: Install dependencies
        run: | 
          npm ci
          npm install rss-parser

      - name: Update README
        run: npm start

      - name: Check if there are any changes ✅ 변경점 있는지 체크
        id: verify_diff
        run: |
          git diff --quiet . || echo "changed=true" >> $GITHUB_OUTPUT

      - name: Commit README
        if: steps.verify_diff.outputs.changed == 'true' ✅ 변경점 있을 경우에만 실행
        run: |
          git config --local user.email "본인의 Github 이메일"
          git config --local user.name "본인의 Github 이름"
          git add .
          git commit -m "Update README.md"
          git push origin main


최종적으로 build success된 것을 확인했다.