Skip to content

Commit

Permalink
Add ghactions to auto update readme with links to latest releases
Browse files Browse the repository at this point in the history
  • Loading branch information
praveen-palanisamy committed Apr 18, 2023
1 parent eb9f001 commit 0e10e64
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/scripts/update_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python
import os
import re
from github import Github

GITHUB_TOKEN = os.environ["GITHUB_TOKEN"]
REPO_NAME = os.environ["GITHUB_ACTION_REPOSITORY"]

g = Github(GITHUB_TOKEN)
repo = g.get_repo(REPO_NAME)
latest_release = repo.get_latest_release()

assets = latest_release.get_assets()
asset_links = {
"windows": "",
"mac": "",
"linux_deb": "",
"linux_appimage": "",
"linux_rpm": "",
}

for asset in assets:
if asset.name.endswith(".exe"):
asset_links["windows"] = asset.browser_download_url
elif asset.name.endswith(".dmg"):
asset_links["mac"] = asset.browser_download_url
elif asset.name.endswith(".deb"):
asset_links["linux_deb"] = asset.browser_download_url
elif asset.name.endswith(".AppImage"):
asset_links["linux_appimage"] = asset.browser_download_url
elif asset.name.endswith(".rpm"):
asset_links["linux_rpm"] = asset.browser_download_url

with open("README.md", "r") as f:
readme_content = f.read()

new_readme_content = re.sub(
r"<!-- ASSETS_START -->(.|\n)*?<!-- ASSETS_END -->",
f"<!-- ASSETS_START -->\n"
f"### Download links\n\n"
f"| Platform | Download |\n"
f"| -------------- | -------- |\n"
f"| Windows | [Download .exe]({asset_links['windows']}) |\n"
f"| Mac | [Download .dmg]({asset_links['mac']}) |\n"
f"| Linux | |\n"
f"| &emsp;Debian/Ubuntu | [Download .deb]({asset_links['linux_deb']}) |\n"
f"| &emsp;AppImage | [Download .appimage]({asset_links['linux_appimage']}) |\n"
f"| &emsp;RPM-based | [Download .rpm]({asset_links['linux_rpm']}) |\n"
f"<!-- ASSETS_END -->",
readme_content,
)

with open("README.md", "w") as f:
f.write(new_readme_content)
31 changes: 31 additions & 0 deletions .github/workflows/update_readme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Update README with Releases

on:
release:
types: [published]

jobs:
update-readme:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v3
with:
python-version: "3.x"
- name: Install dependencies
run: pip install PyGithub
- name: Update README.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_ACTION_REPOSITORY: ${{ github.repository }}
run: python .github/scripts/update_readme.py

- name: Commit and push changes
run: |
git config user.name "GitHub Actions Bot"
git config user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"
git add README.md
git commit -m "Update README.md with latest release links"
git push

0 comments on commit 0e10e64

Please sign in to comment.