|
| 1 | +# Workflow to build and deploy a Next.js site to GitHub Pages |
| 2 | +name: Deploy |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: ["main"] |
| 7 | + tags: |
| 8 | + - "v*" |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +permissions: |
| 12 | + actions: write |
| 13 | + contents: write |
| 14 | + pages: write |
| 15 | + id-token: write |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: "pages" |
| 19 | + cancel-in-progress: false |
| 20 | + |
| 21 | +jobs: |
| 22 | + cleanup: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + |
| 26 | + - name: Clean up workspace |
| 27 | + run: | |
| 28 | + sudo rm -rf $HOME/.npm |
| 29 | + sudo rm -rf $HOME/.cache |
| 30 | + sudo apt-get clean |
| 31 | + sudo rm -rf /tmp/* |
| 32 | +
|
| 33 | + - name: Aggressive Clean Up |
| 34 | + run: | |
| 35 | + echo "Freeing disk space..." |
| 36 | + sudo rm -rf $HOME/.npm |
| 37 | + sudo rm -rf $HOME/.cache |
| 38 | + sudo rm -rf /usr/share/dotnet |
| 39 | + sudo rm -rf /opt/hostedtoolcache |
| 40 | + sudo rm -rf /usr/local/share/boost |
| 41 | + sudo rm -rf /usr/lib/jvm |
| 42 | + sudo apt-get clean |
| 43 | + sudo rm -rf /tmp/* |
| 44 | + sudo journalctl --rotate |
| 45 | + sudo journalctl --vacuum-time=1s |
| 46 | + echo "Remaining space:" |
| 47 | + df -h |
| 48 | + |
| 49 | + - name: Delete Old Artifacts |
| 50 | + uses: actions/github-script@v6 |
| 51 | + id: artifact |
| 52 | + with: |
| 53 | + script: | |
| 54 | + if (context.repo.owner === 'iDataVisualizationLab') { |
| 55 | + const res = await github.rest.actions.listArtifactsForRepo({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + }); |
| 59 | +
|
| 60 | + for (const { id } of res.data.artifacts) { |
| 61 | + await github.rest.actions.deleteArtifact({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + artifact_id: id, |
| 65 | + }); |
| 66 | + } |
| 67 | +
|
| 68 | + console.log(`Deleted ${res.data.artifacts.length} artifacts.`); |
| 69 | + } else { |
| 70 | + console.log("Skipping artifact deletion: not in the main repo context."); |
| 71 | + } |
| 72 | + - name: Delete All Artifacts |
| 73 | + uses: actions/github-script@v6 |
| 74 | + with: |
| 75 | + script: | |
| 76 | + const res = await github.rest.actions.listArtifactsForRepo({ |
| 77 | + owner: context.repo.owner, |
| 78 | + repo: context.repo.repo, |
| 79 | + }); |
| 80 | +
|
| 81 | + console.log(`Found ${res.data.artifacts.length} artifacts`); |
| 82 | + for (const { id, name, size_in_bytes, created_at } of res.data.artifacts) { |
| 83 | + console.log(`Deleting ${name} (${(size_in_bytes / 1024 / 1024).toFixed(2)} MB) created at ${created_at}`); |
| 84 | + await github.rest.actions.deleteArtifact({ |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + artifact_id: id, |
| 88 | + }); |
| 89 | + } |
| 90 | +
|
| 91 | + console.log("All deletions attempted."); |
| 92 | + build: |
| 93 | + needs: cleanup |
| 94 | + runs-on: ubuntu-latest |
| 95 | + steps: |
| 96 | + - name: Checkout repository |
| 97 | + uses: actions/checkout@v4 |
| 98 | + with: |
| 99 | + persist-credentials: false |
| 100 | + |
| 101 | + - name: Setup Node.js |
| 102 | + uses: actions/setup-node@v4 |
| 103 | + with: |
| 104 | + node-version: "20" |
| 105 | + cache: "npm" |
| 106 | + |
| 107 | + - name: Cache dependencies |
| 108 | + uses: actions/cache@v4 |
| 109 | + with: |
| 110 | + path: .next/cache |
| 111 | + key: ${{ runner.os }}-nextjs-${{ hashFiles('package-lock.json') }} |
| 112 | + restore-keys: | |
| 113 | + ${{ runner.os }}-nextjs- |
| 114 | +
|
| 115 | + - name: Install dependencies |
| 116 | + run: npm install --force |
| 117 | + |
| 118 | + - name: Set deployment environment variable |
| 119 | + run: echo "NEXT_PUBLIC_DEPLOYMENT=PRODUCTION" >> $GITHUB_ENV |
| 120 | + |
| 121 | + - name: Set Version Environment Variable |
| 122 | + run: echo "NEXT_PUBLIC_VERSION=$VERSION" >> $GITHUB_ENV |
| 123 | + |
| 124 | + - name: Set NEXT_PUBLIC_REPO_NAME |
| 125 | + run: | |
| 126 | + echo "NEXT_PUBLIC_REPO_NAME=${{ github.event.repository.name }}" >> $GITHUB_ENV |
| 127 | +
|
| 128 | + - name: Build Next.js site |
| 129 | + run: npm run build |
| 130 | + |
| 131 | + - name: Upload static site artifact |
| 132 | + uses: actions/upload-pages-artifact@v3 |
| 133 | + with: |
| 134 | + path: ./out |
| 135 | + |
| 136 | + deploy: |
| 137 | + environment: |
| 138 | + name: github-pages |
| 139 | + url: ${{ steps.deployment.outputs.page_url }} |
| 140 | + runs-on: ubuntu-latest |
| 141 | + needs: build |
| 142 | + steps: |
| 143 | + - name: Deploy to GitHub Pages |
| 144 | + id: deployment |
| 145 | + uses: actions/deploy-pages@v4 |
0 commit comments