Update README with latest blog posts #31
This file contains hidden or 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 README with latest blog posts | |
| on: | |
| push: | |
| paths: | |
| - 'src/content/blog/**' | |
| schedule: | |
| - cron: '0 0 * * *' # daily at midnight UTC | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| update-readme: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Fetch RSS and update README | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const https = require('https'); | |
| const fs = require('fs'); | |
| function fetchRSS(url) { | |
| return new Promise((resolve, reject) => { | |
| const client = url.startsWith('https') ? https : require('http'); | |
| client.get(url, (res) => { | |
| if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { | |
| return fetchRSS(res.headers.location).then(resolve).catch(reject); | |
| } | |
| let data = ''; | |
| res.on('data', chunk => data += chunk); | |
| res.on('end', () => resolve(data)); | |
| }).on('error', reject); | |
| }); | |
| } | |
| try { | |
| const rss = await fetchRSS('https://codestz.dev/feed'); | |
| const items = [...rss.matchAll(/<item>[\s\S]*?<title>(.*?)<\/title>[\s\S]*?<link>(.*?)<\/link>[\s\S]*?<\/item>/g)]; | |
| if (items.length === 0) { | |
| console.log('No items found in RSS feed, skipping update'); | |
| return; | |
| } | |
| const posts = items.slice(0, 7).map(match => { | |
| const title = match[1].replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'"); | |
| const link = match[2]; | |
| return `- [${title}](${link})`; | |
| }).join('\n'); | |
| const readme = fs.readFileSync('README.md', 'utf8'); | |
| const updated = readme.replace( | |
| /<!-- BLOG-POST-LIST:START -->[\s\S]*?<!-- BLOG-POST-LIST:END -->/, | |
| `<!-- BLOG-POST-LIST:START -->\n${posts}\n<!-- BLOG-POST-LIST:END -->` | |
| ); | |
| if (updated !== readme) { | |
| fs.writeFileSync('README.md', updated); | |
| console.log(`Updated README with ${items.slice(0, 7).length} posts`); | |
| } else { | |
| console.log('README already up to date'); | |
| } | |
| } catch (error) { | |
| console.log(`Failed to fetch RSS: ${error.message}`); | |
| console.log('Skipping README update'); | |
| } | |
| - name: Commit changes | |
| run: | | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git config --local user.name "github-actions[bot]" | |
| git diff --quiet README.md || (git add README.md && git commit -m "chore: update README blog posts from RSS feed" && git push) |