레포지토리의 Settings
> Actions
> General
> Workflow Permissions
> Read and Write permissions
선택
GitHub
> Settings
> Developer Settings
> Personal access Tokens
> Generate new token
TIL할 레포지토리의 Settings
> Secrets and Variables
> Actions
에서 GH_PAT
라는 이름의 시크릿을 생성하고, 값에 복사해둔 토큰을 붙여넣는다
.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"
퍼가요 *^^*