Merge pull request #311 from farid-zare/master #146
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 Tutorial | |
| on: | |
| push: | |
| branches: [ master ] | |
| paths: | |
| - '**.mlx' | |
| permissions: | |
| contents: write | |
| jobs: | |
| copy-changes: | |
| runs-on: self-hosted | |
| steps: | |
| - name: Checkout Source Repo | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: '${{ github.repository_owner }}/COBRA.tutorials' | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| fetch-depth: 0 | |
| - name: Get the repository's owner name | |
| shell: bash | |
| run: | | |
| echo "REPO_OWNER=${{ github.repository_owner }}" >> "$GITHUB_ENV" | |
| - name: Clone the destination repository | |
| shell: bash | |
| run: | | |
| rm -rf cobratoolbox | |
| echo "Cloning the destination repository: git@github.com:opencobra/cobratoolbox.git" | |
| git clone --depth 1 --branch gh-pages "https://x-access-token:${{ secrets.DEST_REPO_TOKEN }}@github.com/opencobra/cobratoolbox.git" | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.x' | |
| - name: Install Python dependencies | |
| shell: bash | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install beautifulsoup4 | |
| - name: Locate MATLAB | |
| id: locate_matlab | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| if command -v matlab >/dev/null 2>&1; then | |
| ML_BIN="$(command -v matlab)" | |
| else | |
| ML_BIN="$(ls -1d /usr/local/MATLAB/*/bin/matlab 2>/dev/null | sort -V | tail -n1 || true)" | |
| fi | |
| if [[ -z "${ML_BIN:-}" || ! -x "${ML_BIN}" ]]; then | |
| echo "MATLAB executable not found" >&2 | |
| exit 1 | |
| fi | |
| echo "Using MATLAB at: ${ML_BIN}" | |
| echo "ML_BIN=${ML_BIN}" >> "$GITHUB_ENV" | |
| - name: Check Xvfb and MATLAB | |
| shell: bash | |
| run: | | |
| which xvfb-run | |
| xvfb-run -a "$ML_BIN" -batch "disp(version);" | |
| - name: Get Changed mlx Files Since Last Sync Commit | |
| id: getFile | |
| shell: bash | |
| run: | | |
| echo "Looking for last sync commit..." | |
| last_sync_commit=$(git log --grep="created .pdf, .mlx and .m files" -n 1 --pretty=format:%H) | |
| if [[ -z "$last_sync_commit" ]]; then | |
| echo "No previous sync commit found. Processing all .mlx files." | |
| changed_files=$(git ls-files '*.mlx') | |
| else | |
| echo "Last sync commit: $last_sync_commit" | |
| changed_files=$( | |
| git diff --name-only "$last_sync_commit"..HEAD | | |
| grep '\.mlx' | | |
| grep -v '^deprecated/' || true | |
| ) | |
| fi | |
| changed_files=$(echo "$changed_files" | tr '\n' ' ') | |
| if [[ -z "$changed_files" ]]; then | |
| echo "No .mlx files changed since last sync." | |
| else | |
| echo "Changed .mlx files: $changed_files" | |
| fi | |
| # Export only changed mlx files | |
| for file in $changed_files; do | |
| if [[ -n "$file" && -f "$file" ]]; then | |
| echo "Processing mlx: $file" | |
| ABSOLUTE_FILE_PATH=$(realpath "$file") | |
| HTML_FILE_NAME=$(basename "$file" .mlx).html | |
| HTML_OUTPUT_DIR="cobratoolbox/stable/tutorials/$(dirname "$file")" | |
| HTML_FILE_PATH="$HTML_OUTPUT_DIR/$HTML_FILE_NAME" | |
| mkdir -p "$HTML_OUTPUT_DIR" | |
| M_FILE_PATH=$(echo "$ABSOLUTE_FILE_PATH" | sed 's/.mlx/.m/') | |
| xvfb-run -a "$ML_BIN" -batch "try, export('$ABSOLUTE_FILE_PATH', '$HTML_FILE_PATH', 'Format', 'html'); catch e, disp(getReport(e,'extended')); exit(1); end" | |
| sleep 5 | |
| xvfb-run -a "$ML_BIN" -batch "try, export('$ABSOLUTE_FILE_PATH', '$M_FILE_PATH', 'Format', 'm'); catch e, disp(getReport(e,'extended')); exit(1); end" | |
| else | |
| echo "Skipping missing or invalid file: $file" | |
| fi | |
| done | |
| echo "Rebuilding ALL tutorial wrapper pages (fast)..." | |
| cd cobratoolbox | |
| # Only process HTML files in subdirectories (raw MATLAB exports) | |
| for raw_html in $(find stable/tutorials -mindepth 2 -type f -name "*.html"); do | |
| echo "Wrapping tutorial: $raw_html" | |
| python stable/extract_info.py "$raw_html" | |
| done | |
| cd .. | |
| - name: Pushing the changes | |
| shell: bash | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| cd cobratoolbox | |
| git add . | |
| git commit -m "Sync tutorial files" || echo "No changes to commit" | |
| git push "https://x-access-token:${{ secrets.DEST_REPO_TOKEN }}@github.com/opencobra/cobratoolbox.git" gh-pages | |
| cd .. | |
| rm -rf cobratoolbox | |
| git add . | |
| git commit -m "created .mlx and .m files" || echo "No changes to commit" | |
| git push origin master | |
| echo "Script execution completed." |