From dd952fcf7d3cb17c82b5c9829240c2dcde7a1282 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Fri, 29 Nov 2024 12:56:08 +0100 Subject: [PATCH] Add Update GitHub Registry badge action --- .github/workflows/update-ghcr-badge.yml | 57 +++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 .github/workflows/update-ghcr-badge.yml diff --git a/.github/workflows/update-ghcr-badge.yml b/.github/workflows/update-ghcr-badge.yml new file mode 100644 index 0000000..e90cad6 --- /dev/null +++ b/.github/workflows/update-ghcr-badge.yml @@ -0,0 +1,57 @@ +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.*?> *(?\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 = 'assets/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 assets/ghcr-badge.svg + git config user.name GitHub + git config user.email noreply@github.com + git commit -m 'Update GitHub Registry badge' + git push