Guides as notebooks #21
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 Tutorials | |
on: | |
pull_request: | |
branches: | |
- develop | |
paths: | |
- 'tutorials/**' | |
jobs: | |
update-tutorials: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- name: Checkout code | |
uses: actions/[email protected] | |
with: | |
fetch-depth: 0 # Fetch all history for all branches and tags | |
- name: Set up Python 3.9 | |
uses: actions/[email protected] | |
with: | |
python-version: '3.9' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install nbconvert pyyaml jupyter | |
- name: Clear notebook outputs | |
run: | | |
find tutorials -name "*.ipynb" -exec jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace {} \; | |
- name: Update Tutorials | |
id: update_tutorials | |
run: python scripts/update_tutorials.py | |
- name: Check for changes | |
id: git-check | |
run: | | |
git add tutorials docs/book | |
git status | |
changed_files=$(git diff --staged --name-only) | |
if [ -n "$changed_files" ]; then | |
echo "changes=true" >> $GITHUB_OUTPUT | |
echo "changed_files<<EOF" >> $GITHUB_OUTPUT | |
echo "$changed_files" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
fi | |
- name: Commit changes | |
if: steps.git-check.outputs.changes == 'true' | |
run: | | |
git config --local user.email "[email protected]" | |
git config --local user.name "GitHub Action" | |
changed_files="${{ steps.git-check.outputs.changed_files }}" | |
num_changed=$(echo "$changed_files" | wc -l) | |
if [ $num_changed -eq 1 ]; then | |
file=$(echo "$changed_files" | tr -d '\n') | |
commit_message="Update tutorial: ${file##*/}" | |
else | |
commit_message="Update ${num_changed} tutorial files" | |
fi | |
git commit -m "$commit_message" | |
- name: Push changes | |
if: steps.git-check.outputs.changes == 'true' | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
branch: ${{ github.head_ref }} | |
- name: Create PR comment | |
if: steps.git-check.outputs.changes == 'true' | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const prNumber = context.payload.pull_request.number; | |
const changedFiles = `${{ steps.git-check.outputs.changed_files }}`.split('\n'); | |
let commentBody = '### Tutorial Update Summary\n\n'; | |
commentBody += 'The following files have been updated:\n\n'; | |
changedFiles.forEach(file => { | |
commentBody += `- \`${file}\`\n`; | |
}); | |
commentBody += '\nPlease review these changes.\n\n'; | |
// Add suggested TOC for each guide type | |
const guideTypes = ['starter_guide', 'advanced_guide']; // Add all your guide types here | |
guideTypes.forEach(guideType => { | |
const suggestedTocKey = `suggested_toc_${guideType}`; | |
const suggestedToc = process.env[suggestedTocKey]; | |
if (suggestedToc) { | |
commentBody += `### Suggested TOC for ${guideType.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase())}:\n\n`; | |
commentBody += '```\n' + suggestedToc + '\n```\n\n'; | |
} | |
}); | |
try { | |
await github.rest.issues.createComment({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: commentBody | |
}); | |
} catch (error) { | |
console.error('Error creating comment:', error); | |
core.setFailed(`Failed to create comment: ${error.message}`); | |
} | |
- name: Create PR comment on no changes | |
if: steps.git-check.outputs.changes != 'true' | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const prNumber = context.payload.pull_request.number; | |
try { | |
await github.rest.issues.createComment({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: 'No changes were detected in the tutorials content. All files are up to date.' | |
}); | |
} catch (error) { | |
console.error('Error creating comment:', error); | |
core.setFailed(`Failed to create comment: ${error.message}`); | |
} |