|
| 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 | + - name: Delete all artifacts in org repos |
| 26 | + uses: actions/github-script@v6 |
| 27 | + with: |
| 28 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + script: | |
| 30 | + const org = 'iDataVisualizationLab'; |
| 31 | +
|
| 32 | + try { |
| 33 | + // Get all repositories in the org |
| 34 | + const repos = await github.paginate(github.rest.repos.listForOrg, { |
| 35 | + org, |
| 36 | + per_page: 100 |
| 37 | + }); |
| 38 | +
|
| 39 | + for (const repo of repos) { |
| 40 | + console.log(`📦 Checking artifacts in ${repo.name}...`); |
| 41 | +
|
| 42 | + let artifacts = []; |
| 43 | + try { |
| 44 | + artifacts = await github.paginate(github.rest.actions.listArtifactsForRepo, { |
| 45 | + owner: org, |
| 46 | + repo: repo.name, |
| 47 | + per_page: 100 |
| 48 | + }); |
| 49 | + } catch (err) { |
| 50 | + console.warn(`⚠️ Failed to list artifacts for ${repo.name}: ${err.message}`); |
| 51 | + continue; |
| 52 | + } |
| 53 | +
|
| 54 | + if (artifacts.length === 0) { |
| 55 | + console.log(`✅ No artifacts in ${repo.name}`); |
| 56 | + continue; |
| 57 | + } |
| 58 | +
|
| 59 | + for (const artifact of artifacts) { |
| 60 | + try { |
| 61 | + console.log(`🗑️ Deleting ${artifact.name} in ${repo.name}`); |
| 62 | + await github.rest.actions.deleteArtifact({ |
| 63 | + owner: org, |
| 64 | + repo: repo.name, |
| 65 | + artifact_id: artifact.id, |
| 66 | + }); |
| 67 | + } catch (err) { |
| 68 | + console.warn(`⚠️ Failed to delete ${artifact.name} in ${repo.name}: ${err.message}`); |
| 69 | + } |
| 70 | + } |
| 71 | +
|
| 72 | + console.log(`✅ Finished ${repo.name}`); |
| 73 | + } |
| 74 | +
|
| 75 | + console.log("🎉 All repositories processed."); |
| 76 | + } catch (err) { |
| 77 | + console.error(`❌ Unexpected error: ${err.message}`); |
| 78 | + core.setFailed(err.message); |
| 79 | + } |
| 80 | +
|
| 81 | + - name: Clean up workspace |
| 82 | + run: | |
| 83 | + sudo rm -rf $HOME/.npm |
| 84 | + sudo rm -rf $HOME/.cache |
| 85 | + sudo apt-get clean |
| 86 | + sudo rm -rf /tmp/* |
| 87 | +
|
| 88 | + - name: Aggressive Clean Up |
| 89 | + run: | |
| 90 | + echo "Freeing disk space..." |
| 91 | + sudo rm -rf $HOME/.npm |
| 92 | + sudo rm -rf $HOME/.cache |
| 93 | + sudo rm -rf /usr/share/dotnet |
| 94 | + sudo rm -rf /opt/hostedtoolcache |
| 95 | + sudo rm -rf /usr/local/share/boost |
| 96 | + sudo rm -rf /usr/lib/jvm |
| 97 | + sudo apt-get clean |
| 98 | + sudo rm -rf /tmp/* |
| 99 | + sudo journalctl --rotate |
| 100 | + sudo journalctl --vacuum-time=1s |
| 101 | + echo "Remaining space:" |
| 102 | + df -h |
| 103 | + |
| 104 | + - name: Delete Old Artifacts |
| 105 | + uses: actions/github-script@v6 |
| 106 | + id: artifact |
| 107 | + with: |
| 108 | + script: | |
| 109 | + if (context.repo.owner === 'iDataVisualizationLab') { |
| 110 | + const res = await github.rest.actions.listArtifactsForRepo({ |
| 111 | + owner: context.repo.owner, |
| 112 | + repo: context.repo.repo, |
| 113 | + }); |
| 114 | +
|
| 115 | + for (const { id } of res.data.artifacts) { |
| 116 | + await github.rest.actions.deleteArtifact({ |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + artifact_id: id, |
| 120 | + }); |
| 121 | + } |
| 122 | +
|
| 123 | + console.log(`Deleted ${res.data.artifacts.length} artifacts.`); |
| 124 | + } else { |
| 125 | + console.log("Skipping artifact deletion: not in the main repo context."); |
| 126 | + } |
| 127 | + - name: Delete All Artifacts |
| 128 | + uses: actions/github-script@v6 |
| 129 | + with: |
| 130 | + script: | |
| 131 | + const res = await github.rest.actions.listArtifactsForRepo({ |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + }); |
| 135 | +
|
| 136 | + console.log(`Found ${res.data.artifacts.length} artifacts`); |
| 137 | + for (const { id, name, size_in_bytes, created_at } of res.data.artifacts) { |
| 138 | + console.log(`Deleting ${name} (${(size_in_bytes / 1024 / 1024).toFixed(2)} MB) created at ${created_at}`); |
| 139 | + await github.rest.actions.deleteArtifact({ |
| 140 | + owner: context.repo.owner, |
| 141 | + repo: context.repo.repo, |
| 142 | + artifact_id: id, |
| 143 | + }); |
| 144 | + } |
| 145 | +
|
| 146 | + console.log("All deletions attempted."); |
| 147 | + build: |
| 148 | + needs: cleanup |
| 149 | + runs-on: ubuntu-latest |
| 150 | + steps: |
| 151 | + - name: Checkout repository |
| 152 | + uses: actions/checkout@v4 |
| 153 | + with: |
| 154 | + persist-credentials: false |
| 155 | + |
| 156 | + - name: Setup Node.js |
| 157 | + uses: actions/setup-node@v4 |
| 158 | + with: |
| 159 | + node-version: "20" |
| 160 | + cache: "npm" |
| 161 | + |
| 162 | + - name: Cache dependencies |
| 163 | + uses: actions/cache@v4 |
| 164 | + with: |
| 165 | + path: .next/cache |
| 166 | + key: ${{ runner.os }}-nextjs-${{ hashFiles('package-lock.json') }} |
| 167 | + restore-keys: | |
| 168 | + ${{ runner.os }}-nextjs- |
| 169 | +
|
| 170 | + - name: Install dependencies |
| 171 | + run: npm install --force |
| 172 | + |
| 173 | + - name: Set deployment environment variable |
| 174 | + run: echo "NEXT_PUBLIC_DEPLOYMENT=PRODUCTION" >> $GITHUB_ENV |
| 175 | + |
| 176 | + - name: Set Version Environment Variable |
| 177 | + run: echo "NEXT_PUBLIC_VERSION=$VERSION" >> $GITHUB_ENV |
| 178 | + |
| 179 | + - name: Set NEXT_PUBLIC_REPO_NAME |
| 180 | + run: | |
| 181 | + echo "NEXT_PUBLIC_REPO_NAME=${{ github.event.repository.name }}" >> $GITHUB_ENV |
| 182 | +
|
| 183 | + - name: Build Next.js site |
| 184 | + run: npm run build |
| 185 | + |
| 186 | + - name: Upload static site artifact |
| 187 | + uses: actions/upload-pages-artifact@v3 |
| 188 | + with: |
| 189 | + path: ./out |
| 190 | + |
| 191 | + deploy: |
| 192 | + environment: |
| 193 | + name: github-pages |
| 194 | + url: ${{ steps.deployment.outputs.page_url }} |
| 195 | + runs-on: ubuntu-latest |
| 196 | + needs: build |
| 197 | + steps: |
| 198 | + - name: Deploy to GitHub Pages |
| 199 | + id: deployment |
| 200 | + uses: actions/deploy-pages@v4 |
0 commit comments