Skip to content

Sync Lessons to LabEx #2

Sync Lessons to LabEx

Sync Lessons to LabEx #2

Workflow file for this run

name: Sync Lessons to LabEx
on:
push:
branches: [master]
paths:
- "lessons/**/*.md"
pull_request:
branches: [master]
paths:
- "lessons/**/*.md"
workflow_dispatch:
inputs:
sync_path:
description: "Path to sync specific lesson file (e.g., lessons/en/logging/syslog.md). Leave empty to sync all changed files."
required: false
default: ""
type: string
jobs:
sync-lessons:
name: Sync LabEx Lessons
runs-on: ubuntu-latest
env:
LABEX_USERNAME: ${{ secrets.LABEX_USERNAME }}
LABEX_PASSWORD: ${{ secrets.LABEX_PASSWORD }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
WORKSPACE: ${{ github.workspace }}
steps:
- name: Clean workspace
run: |
mkdir -p $WORKSPACE
rm -rf "$WORKSPACE/labex-auto"
echo "Workspace cleaned"
ls -la $WORKSPACE
- name: Checkout labex-auto
uses: actions/checkout@v4
with:
repository: labex-labs/labex-auto
path: labex-auto
token: ${{ env.GH_TOKEN }}
- name: Checkout linuxjourney (for getting changed files)
uses: actions/checkout@v4
with:
path: linuxjourney
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pip"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
cd labex-auto
pip install -e .
- name: Verify environment variables
run: |
for var in LABEX_USERNAME LABEX_PASSWORD; do
if [ -z "${!var}" ]; then
echo "Error: Missing environment variable $var"
exit 1
fi
done
echo "All required environment variables are set"
- name: Get changed lesson files
id: changed-files
run: |
cd linuxjourney
# Get list of changed files
if [[ "${{ github.event_name }}" == "push" ]]; then
# For push events, compare current commit with previous commit
CHANGED_FILES=$(git diff --name-only HEAD~1..HEAD -- 'lessons/**/*.md' | tr '\n' ' ')
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
# For PR events, compare target branch with source branch
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}..HEAD -- 'lessons/**/*.md' | tr '\n' ' ')
elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]] && [[ -n "${{ github.event.inputs.sync_path }}" ]]; then
# For manual trigger with specified path, use the specified path
SYNC_PATH="${{ github.event.inputs.sync_path }}"
if [[ -f "$SYNC_PATH" ]] && [[ "$SYNC_PATH" =~ ^lessons/.*\.md$ ]]; then
CHANGED_FILES="$SYNC_PATH"
else
echo "Error: Specified path does not exist or is not a valid lesson document: $SYNC_PATH"
echo "Path should start with 'lessons/' and end with '.md'"
exit 1
fi
else
# For manual trigger without specified path, get all md files in lessons directory
CHANGED_FILES=$(find lessons -name "*.md" | tr '\n' ' ')
fi
echo "Changed files: $CHANGED_FILES"
echo "changed_files=$CHANGED_FILES" >> $GITHUB_OUTPUT
- name: Sync changed lessons to LabEx
run: |
cd labex-auto
# Get list of changed files
CHANGED_FILES="${{ steps.changed-files.outputs.changed_files }}"
if [ -z "$CHANGED_FILES" ]; then
echo "No changed files, skipping sync"
exit 0
fi
echo "Starting sync of changed documents to LabEx..."
echo "Execution time: $(date)"
# Sync each changed document
SYNC_COUNT=0
FAILED_FILES=""
SUCCESS_FILES=""
for file in $CHANGED_FILES; do
if [ -f "../linuxjourney/$file" ]; then
echo "Syncing document: $file"
if labex-auto labex sync linuxjourney --path "$file"; then
SUCCESS_FILES="$SUCCESS_FILES\n- $file"
SYNC_COUNT=$((SYNC_COUNT + 1))
else
FAILED_FILES="$FAILED_FILES\n- $file"
fi
fi
done
# Save sync results to output
echo "sync_count=$SYNC_COUNT" >> $GITHUB_OUTPUT
echo "success_files<<EOF" >> $GITHUB_OUTPUT
echo -e "$SUCCESS_FILES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "failed_files<<EOF" >> $GITHUB_OUTPUT
echo -e "$FAILED_FILES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Generate sync summary
run: |
SYNC_COUNT="${{ steps.sync-lessons.outputs.sync_count }}"
SUCCESS_FILES="${{ steps.sync-lessons.outputs.success_files }}"
FAILED_FILES="${{ steps.sync-lessons.outputs.failed_files }}"
CHANGED_FILES="${{ steps.changed-files.outputs.changed_files }}"
# Generate GitHub Actions summary
{
echo "## LabEx Lesson Sync Report"
echo ""
echo "### Statistics"
echo "- Files synced: $SYNC_COUNT"
echo "- Trigger: ${{ github.event_name }}"
echo "- Time: $(date)"
echo ""
if [ -n "$CHANGED_FILES" ]; then
echo "### Changed Files"
for file in $CHANGED_FILES; do
echo "- $file"
done
echo ""
fi
if [ -n "$SUCCESS_FILES" ]; then
echo "### Successfully Synced"
echo "$SUCCESS_FILES"
echo ""
fi
if [ -n "$FAILED_FILES" ]; then
echo "### Failed to Sync"
echo "$FAILED_FILES"
echo ""
fi
echo "### Links"
echo "- [Commit](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})"
echo "- [Action Logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
echo ""
if [ -n "$FAILED_FILES" ]; then
echo "**Warning**: Some files failed to sync. Please check the failed files list above."
else
echo "**Completed**: All detected documents have been successfully synced to LabEx."
fi
} >> $GITHUB_STEP_SUMMARY
- name: Notify completion
run: |
echo "Sync task completed"
echo "Completion time: $(date)"