diff --git a/.github/workflows/nextjs.yml b/.github/workflows/nextjs.yml index 353a8c3..3d026b6 100644 --- a/.github/workflows/nextjs.yml +++ b/.github/workflows/nextjs.yml @@ -1,11 +1,10 @@ -# Build and deploy a Next.js site to GitHub Pages +# Build and deploy a Next.js site to GitHub Pages with Python artifact fallback name: Deploy Next.js site to Pages on: # Runs on pushes targeting the default branch push: - branches: ["main"] - + # branches: ["main"] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: @@ -22,10 +21,11 @@ concurrency: cancel-in-progress: false jobs: - # Data fetch data_fetch: name: Run Python Script runs-on: ubuntu-latest + outputs: + python_artifact_path: ./python-output steps: - name: Checkout repository uses: actions/checkout@v4 @@ -40,19 +40,66 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt - - name: Run Python script + - name: Run Python script (with fallback) + id: run-python run: | - set -e + set +e python run_scripts.py - # python scripts/ngs_passing.py - # python scripts/ngs_receiving.py - # python scripts/ngs_rushing.py - # python scripts/qb_season.py - # python scripts/qb_weekly.py - # python scripts/team_data.py - - # Upload the file the script produced - - name: Upload Python output + status=$? + echo "script_status=$status" >> $GITHUB_OUTPUT + if [ $status -ne 0 ]; then + echo "Python script failed, will use last successful artifact" + fi + exit 0 + + - name: Download previous successful Python artifact (if needed) + if: steps.run-python.outputs.script_status != '0' + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const artifactName = 'python-output'; + const owner = context.repo.owner; + const repo = context.repo.repo; + const workflow_id = context.workflow; + + // Get last successful run + const runs = await github.actions.listWorkflowRuns({ + owner, + repo, + workflow_id, + status: 'success', + per_page: 10 + }); + + if (!runs.data.workflow_runs.length) { + core.setFailed('No previous successful runs found'); + } + + const lastRunId = runs.data.workflow_runs[0].id; + + // Find artifact from that run + const artifacts = await github.actions.listWorkflowRunArtifacts({ + owner, + repo, + run_id: lastRunId + }); + + const artifact = artifacts.data.artifacts.find(a => a.name === artifactName); + + if (!artifact) { + core.setFailed('No previous artifact found'); + } + + const artifactUrl = artifact.archive_download_url; + core.info(`Artifact URL: ${artifactUrl}`); + + // Download and unzip artifact + const exec = require('@actions/exec'); + await exec.exec(`curl -L -o ${artifactName}.zip -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "${artifactUrl}"`); + await exec.exec(`unzip -o ${artifactName}.zip -d ./python-output`); + + - name: Upload Python output (always) uses: actions/upload-artifact@v4 with: name: python-output @@ -65,14 +112,12 @@ jobs: scripts/generated-files/ngs-data-receiving.ts scripts/generated-files/ngs-data-rushing.ts - # Build job build: needs: data_fetch runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - # Download Python artifact before building - name: Download Python artifact uses: actions/download-artifact@v4 @@ -81,8 +126,7 @@ jobs: path: ./python-output - name: Move Python output into project - run: | - cp -r ./python-output/* scripts/generated-files/ + run: cp -r ./python-output/* scripts/generated-files/ - name: Detect package manager id: detect-package-manager @@ -115,20 +159,16 @@ jobs: - name: Restore cache uses: actions/cache@v4 with: - path: | - .next/cache - # Generate a new cache whenever packages or source files change. + path: .next/cache key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} # If source files changed but packages didn't, rebuild from a prior cache. - restore-keys: | - ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}- + restore-keys: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}- - name: Install dependencies run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }} - name: Verify generated data files - run: | - ls -l scripts + run: ls -l scripts - name: Build with Next.js run: ${{ steps.detect-package-manager.outputs.runner }} next build @@ -138,7 +178,6 @@ jobs: with: path: ./out - # Deployment job deploy: environment: name: github-pages diff --git a/components/qb-touchdowns/index.tsx b/components/qb-touchdowns/index.tsx index 4c154d7..28658f8 100644 --- a/components/qb-touchdowns/index.tsx +++ b/components/qb-touchdowns/index.tsx @@ -26,7 +26,7 @@ export const QBTouchdowns = () => {