|
10 | 10 | - main |
11 | 11 | - next |
12 | 12 | pull_request: |
13 | | - types: [closed] |
| 13 | + types: [opened, reopened, closed] |
14 | 14 | delete: |
15 | 15 |
|
16 | 16 | jobs: |
@@ -223,6 +223,96 @@ jobs: |
223 | 223 | }); |
224 | 224 | } |
225 | 225 |
|
| 226 | + notify-pr-on-open: |
| 227 | + runs-on: ubuntu-latest |
| 228 | + if: | |
| 229 | + github.event_name == 'pull_request' && |
| 230 | + (github.event.action == 'opened' || github.event.action == 'reopened') && |
| 231 | + !contains(fromJson('["main","next"]'), github.event.pull_request.head.ref) |
| 232 | + permissions: |
| 233 | + contents: read |
| 234 | + deployments: read |
| 235 | + issues: write |
| 236 | + pull-requests: write |
| 237 | + |
| 238 | + steps: |
| 239 | + - name: Set preview app name and domain |
| 240 | + run: | |
| 241 | + BRANCH_SLUG=$(echo "${{ github.event.pull_request.head.ref }}" \ |
| 242 | + | tr '[:upper:]' '[:lower:]' \ |
| 243 | + | sed 's/[^a-z0-9-]/-/g' \ |
| 244 | + | sed 's/-\+/-/g' \ |
| 245 | + | sed 's/^-//;s/-$//') |
| 246 | +
|
| 247 | + APP_NAME="fairdataihub-website-${BRANCH_SLUG}" |
| 248 | + APP_DOMAIN="website-${BRANCH_SLUG}.fairdataihub.org" |
| 249 | +
|
| 250 | + echo "KAMAL_APP_NAME=${APP_NAME}" >> "$GITHUB_ENV" |
| 251 | + echo "KAMAL_APP_DOMAIN=${APP_DOMAIN}" >> "$GITHUB_ENV" |
| 252 | +
|
| 253 | + - name: Post preview status comment |
| 254 | + uses: actions/github-script@v7 |
| 255 | + with: |
| 256 | + github-token: ${{ secrets.GH_PAT || secrets.GITHUB_TOKEN }} |
| 257 | + script: | |
| 258 | + const previewUrl = `https://${process.env.KAMAL_APP_DOMAIN}`; |
| 259 | + const issue_number = context.payload.pull_request.number; |
| 260 | +
|
| 261 | + // Look up the latest deployment for this preview environment |
| 262 | + const { data: deployments } = await github.rest.repos.listDeployments({ |
| 263 | + owner: context.repo.owner, |
| 264 | + repo: context.repo.repo, |
| 265 | + environment: process.env.KAMAL_APP_NAME, |
| 266 | + per_page: 1, |
| 267 | + }); |
| 268 | +
|
| 269 | + let body; |
| 270 | + if (deployments.length > 0) { |
| 271 | + const { data: statuses } = await github.rest.repos.listDeploymentStatuses({ |
| 272 | + owner: context.repo.owner, |
| 273 | + repo: context.repo.repo, |
| 274 | + deployment_id: deployments[0].id, |
| 275 | + per_page: 1, |
| 276 | + }); |
| 277 | + const state = statuses[0]?.state; |
| 278 | + if (state === 'success') { |
| 279 | + const updatedAt = new Date(deployments[0].updated_at).toUTCString(); |
| 280 | + body = `## Preview deployment ready\n\n**Preview URL:** [${previewUrl}](${previewUrl})\n\n> Last deployed: ${updatedAt}`; |
| 281 | + } else if (state === 'in_progress') { |
| 282 | + body = `## Preview deployment in progress\n\nA preview environment is being built and will be available at [${previewUrl}](${previewUrl}) once ready. Please wait...`; |
| 283 | + } else { |
| 284 | + body = `## Preview deployment\n\nA preview environment will be available at [${previewUrl}](${previewUrl}) after the next push to this branch.`; |
| 285 | + } |
| 286 | + } else { |
| 287 | + body = `## Preview deployment\n\nA preview environment will be available at [${previewUrl}](${previewUrl}) after the next push to this branch.`; |
| 288 | + } |
| 289 | +
|
| 290 | + const { data: comments } = await github.rest.issues.listComments({ |
| 291 | + owner: context.repo.owner, |
| 292 | + repo: context.repo.repo, |
| 293 | + issue_number, |
| 294 | + }); |
| 295 | +
|
| 296 | + const existing = comments.find( |
| 297 | + c => c.user.type === 'Bot' && c.body.startsWith('## Preview deployment') |
| 298 | + ); |
| 299 | +
|
| 300 | + if (existing) { |
| 301 | + await github.rest.issues.updateComment({ |
| 302 | + owner: context.repo.owner, |
| 303 | + repo: context.repo.repo, |
| 304 | + comment_id: existing.id, |
| 305 | + body, |
| 306 | + }); |
| 307 | + } else { |
| 308 | + await github.rest.issues.createComment({ |
| 309 | + owner: context.repo.owner, |
| 310 | + repo: context.repo.repo, |
| 311 | + issue_number, |
| 312 | + body, |
| 313 | + }); |
| 314 | + } |
| 315 | +
|
226 | 316 | remove-preview: |
227 | 317 | runs-on: ubuntu-latest |
228 | 318 | if: | |
|
0 commit comments