이번 주는 TIL 몇 번 했는지, 총 TIL 몇 번 했는지

지니🧸·2024년 9월 19일
2

Git & GitHub

목록 보기
5/6

1. TIL을 할 레포지토리를 설정한다

1-1. GitHub Action 봇에 해당 레포지토리 쓰기 작업 권한을 부여한다

레포지토리의 Settings > Actions > General > Workflow Permissions > Read and Write permissions 선택

1-2. Personal Access Token (PAT)를 생성한다

GitHub > Settings > Developer Settings > Personal access Tokens > Generate new token

  • 이 토큰에 쓰기 권한을 허용해야 한다
  • 토큰을 복사한다

1-3. PAT를 레포지토리 시크릿에 추가한다

TIL할 레포지토리의 Settings > Secrets and Variables > Actions 에서 GH_PAT라는 이름의 시크릿을 생성하고, 값에 복사해둔 토큰을 붙여넣는다

2. GitHub Workflow를 추가한다

.github/workflows/update_commit.yml을 생성하고 다음을 복사한 후, github url을 적절하게 넣는다

커밋을 세고 반영하는 커밋은 반영되지 않도록 필터 로직을 추가했다

name: Update Commit Activity Badges

on:
  push:
    branches:
      - main
  schedule:
    - cron: '59 23 * * *'
  workflow_dispatch:

jobs:
  update-commit-badges:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repo
        uses: actions/checkout@v2
        with:
          ref: main
          persist-credentials: true
          fetch-depth: 0

      - name: Setup Git Config
        run: |
          git config --global user.email "github-actions[bot]@github.com"
          git config --global user.name "github-actions[bot]"

      - name: Count Total Commit Days (Exclude Badge Update Commits)
        id: total_commit_days
        run: |
          total_days=$(git log --format='%cd' --date=format:'%Y-%m-%d' --grep='badge' --invert-grep | sort -u | wc -l)
          echo "total_commit_days=$total_days" >> $GITHUB_ENV

      - name: Count Weekly Commit Days (Exclude Badge Update Commits)
        id: weekly_commit_days
        run: |
          weekly_days=$(git log --since='7 days ago' --format='%cd' --date=format:'%Y-%m-%d' --grep='badge' --invert-grep | sort -u | wc -l)
          echo "weekly_commit_days=$weekly_days" >> $GITHUB_ENV


      - name: Create Badges
        run: |
          total_badge_url="https://img.shields.io/badge/total_commit_days-${{ env.total_commit_days }}-blue?cache=$(date +%s)"
          weekly_badge_url="https://img.shields.io/badge/weekly_commit_days-${{ env.weekly_commit_days }}-green?cache=$(date +%s)"
          echo "![Total Commit Days]($total_badge_url)" > total_commit_badge.md
          echo "![Weekly Commit Days]($weekly_badge_url)" > weekly_commit_badge.md

      - name: Update README with Badges at Top
        run: |
          # Safely remove existing badge lines
          sed -i '/Total Commit Days/d' README.md || echo "No Total Commit Days badge found to remove"
          sed -i '/Weekly Commit Days/d' README.md || echo "No Weekly Commit Days badge found to remove"

          # Combine badges and the rest of the README
          cat total_commit_badge.md weekly_commit_badge.md README.md > temp_readme.md
          mv temp_readme.md README.md

      - name: Stash Changes (if any) before Pull
        run: |
          git add .
          git stash push -m "temp-stash-for-pull" || echo "Nothing to stash"

      - name: Pull Latest Changes
        run: |
          git pull origin main --rebase || echo "Rebase failed, attempting to continue"

      - name: Apply Stash (if any)
        run: |
          git stash pop || echo "No stash to apply"

      - name: Commit Changes with Identifier
        run: |
          git add README.md
          git commit -m "Update commit days badges [badge-update]" || echo "No changes to commit"

      - name: Fetch Latest Changes Before Pushing
        run: |
          git fetch origin main
          git rebase origin/main || echo "Rebase failed, attempting to resolve automatically"
          git rebase --continue || echo "No rebase to continue"

      - name: Push Changes
        env:
          GH_PAT: ${{ secrets.GH_PAT }}
        run: |
          git push https://x-access-token:${GH_PAT}@github.com/TIL-challenge/becooq81.git || echo "Push failed, trying to resolve"
profile
우당탕탕

2개의 댓글

comment-user-thumbnail
2024년 9월 21일

퍼가요 *^^*

1개의 답글

관련 채용 정보