Skip to content

Update GitHub Registry badge #7

Update GitHub Registry badge

Update GitHub Registry badge #7

name: Update GitHub Registry badge
on:
schedule:
- cron: "0 12 * * *"
workflow_dispatch:
jobs:
update-ghcr-badge:
name: Update GitHub Registry badge
runs-on: windows-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
with:
ref: assets
-
name: Build badge
id: build-badge
run: |
$response = Invoke-WebRequest -Uri https://github.com/mlocati/docker-php-extension-installer/pkgs/container/php-extension-installer -ErrorAction Stop
$html = $response.Content -replace '\s+', ' '
if (-not($html -match 'Total downloads.*?> *(?<count>\d+\w*)')) {
throw 'Unable to find the total downloads count'
}
$count = $Matches.count
Write-Host -Object "Current downloads count: $count"
$response = Invoke-WebRequest -Uri https://img.shields.io/badge/GitHub_Registry_pulls-$count-066da5 -ErrorAction Stop
$newBadge = $response.Content.Trim()
Write-Host -Object "Badge source: $newBadge"
$badgePath = 'resources/ghcr-badge.svg'
$badgeUpdated = $true
if (Test-Path -LiteralPath $badgePath -PathType Leaf) {
$oldBadge = (Get-Content -LiteralPath $badgePath -Encoding utf8 -Raw).Trim()
if ($oldBadge -eq $newBadge) {
Write-Host -Object 'Badge is already up-to-date'
$badgeUpdated = $false
} else {
Write-Host -Object 'Badge is updated'
}
} else {
Write-Host -Object 'Badge is new'
}
if ($badgeUpdated) {
Set-Content -LiteralPath $badgePath -Value $newBadge -NoNewline -Encoding utf8 -ErrorAction Stop
'updated=yes' | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding utf8
}
-
name: Commit
if: steps.build-badge.outputs.updated == 'yes'
run: |
git add resources/ghcr-badge.svg
git config user.name GitHub
git config user.email [email protected]
git commit -m 'Update GitHub Registry badge'
git push