update-organization-readme-badges #52
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: update-organization-readme-badges | |
on: | |
schedule: | |
- cron: '0 7 * * *' # runs daily at 07:00 | |
workflow_dispatch: | |
permissions: | |
contents: write | |
jobs: | |
generate-badges: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/create-github-app-token@v1 | |
id: app-token | |
with: | |
app-id: ${{ vars.APP_ID }} | |
private-key: ${{ secrets.PRIVATE_KEY }} | |
owner: ${{ github.repository_owner }} | |
- name: organization-readme-badge-generator | |
id: organization-readme-badge-generator | |
uses: joshjohanning/organization-readme-badge-generator@v1 | |
with: | |
organization: ${{ github.repository_owner }} | |
token: ${{ steps.app-token.outputs.token }} # recommend to use a GitHub App and not a PAT | |
- name: write to job summary | |
run: | | |
echo "${{ steps.organization-readme-badge-generator.outputs.badges }}" >> $GITHUB_STEP_SUMMARY | |
- name: add to readme | |
run: | | |
readme=profile/README.md | |
# get SHA256 before | |
beforeHash=$(sha256sum $readme | awk '{ print $1 }') | |
# Define start and end markers | |
startMarker="<!-- start organization badges -->" | |
endMarker="<!-- end organization badges -->" | |
replacement="${{ steps.organization-readme-badge-generator.outputs.badges }}" | |
# Escape special characters in the replacement text | |
replacementEscaped=$(printf '%s\n' "$replacement" | perl -pe 's/([\\\/\$\(\)@])/\\$1/g') | |
# Use perl to replace the text between the markers | |
perl -i -pe "BEGIN{undef $/;} s/\Q$startMarker\E.*?\Q$endMarker\E/$startMarker\n$replacementEscaped\n$endMarker/smg" $readme | |
# get SHA256 after | |
afterHash=$(sha256sum $readme | awk '{ print $1 }') | |
# Compare the hashes and commit if required | |
if [ "$afterHash" = "$beforeHash" ]; then | |
echo "The hashes are equal - exiting script" | |
exit 0 | |
else | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
git add $readme | |
git commit -m "docs: update organization readme badges" | |
git push | |
fi |