Skip to content

Update Views Per Month Badge #46

Update Views Per Month Badge

Update Views Per Month Badge #46

Workflow file for this run

name: Update Views Per Month Badge
on:
push:
branches:
- main # Этот workflow запускается при пуше в основную ветку
schedule:
- cron: '0 * * * *' # Работает каждый час
jobs:
fetch_traffic:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Fetch traffic data from GitHub API
id: fetch_traffic
run: |
# Получаем данные о трафике с GitHub API, используя переменную ${{ github.repository }}
response=$(curl -H "Authorization: token ${{ secrets.PUBLIC_REPO_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/traffic/views)
# Извлекаем общее количество просмотров и уникальных просмотров
total_views=$(echo "$response" | jq '.count')
unique_views=$(echo "$response" | jq '.uniques')
# Формируем URL для картинки с уникальными просмотрами
badge_total_url="https://img.shields.io/badge/Total%20V%20Per%20Month-$total_views-blue"
badge_unique_url="https://img.shields.io/badge/Unique%20V%20Per%20Month-$unique_views-green"
# Сохраняем URL в Environment File
echo "BADGE_TOTAL_URL=$badge_total_url" >> $GITHUB_ENV
echo "BADGE_UNIQUE_URL=$badge_unique_url" >> $GITHUB_ENV
- name: Update badge in README.md
run: |
# Проверяем, что BADGE_TOTAL_URL не пустой
if [ -z "$BADGE_TOTAL_URL" ]; then
echo "Error: Badge URL is empty."
exit 1
fi
# Проверяем, что BADGE_UNIQUE_URL не пустой
if [ -z "$BADGE_UNIQUE_URL" ]; then
echo "Error: Badge URL is empty."
exit 1
fi
# Обновляем файл с новым значением
sed -i \
-e "s|!\[Total Views Month Badge](.*)|![Total Views Month Badge](${BADGE_TOTAL_URL})|" \
-e "s|!\[Unique Views Month Badge](.*)|![Unique Views Month Badge](${BADGE_UNIQUE_URL})|" \
README.md
- name: Set git configuration
run: |
git config --global user.name "cortez24rus"
git config --global user.email "cortez24rus@gmail.com"
- name: Pull latest changes
run: |
git pull origin main || echo "No changes to pull"
- name: Commit and push changes to README.md
run: |
git add README.md
git commit -m "Update unique views badge in README.md" || echo "No changes to commit"
git push origin main