Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

dx(platform): auto-redeploy active previews when a PR is updated (#13485)

Nicholas Tindlee59ceff3c75d
diff --git a/.github/workflows/platform-dev-deploy-event-dispatcher.yml b/.github/workflows/platform-dev-deploy-event-dispatcher.yml
index 1a581c55c2..354ce5d7c4 100644
--- a/.github/workflows/platform-dev-deploy-event-dispatcher.yml
+++ b/.github/workflows/platform-dev-deploy-event-dispatcher.yml
@@ -2,7 +2,7 @@ name: AutoGPT Platform - Dev Deploy PR Event Dispatcher
on:
pull_request:
- types: [closed]
+ types: [closed, synchronize]
issue_comment:
types: [created]
@@ -13,6 +13,11 @@ permissions:
jobs:
dispatch:
runs-on: ubuntu-latest
+ # Serialize per PR so rapid pushes/comments don't race each other's
+ # dispatches. Deliberately NOT cancel-in-progress: cancelling could kill an
+ # in-flight !undeploy dispatch when a push lands right after it.
+ concurrency:
+ group: preview-dispatch-${{ github.event.pull_request.number || github.event.issue.number }}
steps:
- name: Check comment permissions and deployment status
id: check_status
@@ -91,7 +96,7 @@ jobs:
{
"action": "deploy",
"pr_number": "${{ steps.pr_details.outputs.pr_number }}",
- "pr_title": "${{ steps.pr_details.outputs.pr_title }}",
+ "pr_title": ${{ toJSON(steps.pr_details.outputs.pr_title) }},
"pr_state": "${{ steps.pr_details.outputs.pr_state }}",
"repo": "${{ github.repository }}"
}
@@ -119,7 +124,7 @@ jobs:
{
"action": "undeploy",
"pr_number": "${{ steps.pr_details.outputs.pr_number }}",
- "pr_title": "${{ steps.pr_details.outputs.pr_title }}",
+ "pr_title": ${{ toJSON(steps.pr_details.outputs.pr_title) }},
"pr_state": "${{ steps.pr_details.outputs.pr_state }}",
"repo": "${{ github.repository }}"
}
@@ -142,23 +147,27 @@ jobs:
uses: actions/github-script@v8
with:
script: |
- const comments = await github.rest.issues.listComments({
+ // Paginate: listComments returns only the first 30 by default, so a
+ // later !deploy/!undeploy on a busy PR would be missed and corrupt the
+ // active-deployment scan.
+ const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
- issue_number: context.issue.number
+ issue_number: context.issue.number,
+ per_page: 100
});
-
+
let lastDeployIndex = -1;
let lastUndeployIndex = -1;
-
- comments.data.forEach((comment, index) => {
+
+ comments.forEach((comment, index) => {
if (comment.body.trim() === '!deploy') {
lastDeployIndex = index;
} else if (comment.body.trim() === '!undeploy') {
lastUndeployIndex = index;
}
});
-
+
// Should undeploy if there's a !deploy without a subsequent !undeploy
const shouldUndeploy = lastDeployIndex !== -1 && lastDeployIndex > lastUndeployIndex;
core.setOutput('should_undeploy', shouldUndeploy);
@@ -177,7 +186,7 @@ jobs:
{
"action": "undeploy",
"pr_number": "${{ github.event.pull_request.number }}",
- "pr_title": "${{ github.event.pull_request.title }}",
+ "pr_title": ${{ toJSON(github.event.pull_request.title) }},
"pr_state": "${{ github.event.pull_request.state }}",
"repo": "${{ github.repository }}"
}
@@ -195,4 +204,84 @@ jobs:
repo: context.repo.repo,
issue_number: context.issue.number,
body: `🧹 **Auto-undeploying**: PR closed with active deployment. Cleaning up development environment for PR #${{ github.event.pull_request.number }}.`
- });
\ No newline at end of file
+ });
+
+ # Auto-redeploy: previews go stale silently when a PR is pushed to and nobody
+ # remembers to re-run !deploy. Reuse the close-handler's active-deployment scan
+ # (last !deploy with no later !undeploy) and refresh the preview if one is active.
+ - name: Check deployment status on PR update
+ id: check_pr_sync
+ # Same-repo branches only: a fork author must not be able to push
+ # unreviewed code onto a preview that a collaborator !deployed once.
+ # Fork PRs keep the manual !deploy flow (explicit human per update).
+ if: >-
+ github.event_name == 'pull_request' &&
+ github.event.action == 'synchronize' &&
+ github.event.pull_request.head.repo.full_name == github.repository
+ uses: actions/github-script@v8
+ with:
+ script: |
+ // Paginate: listComments returns only the first 30 by default, so a
+ // later !deploy/!undeploy on a busy PR would be missed and corrupt the
+ // active-deployment scan.
+ const comments = await github.paginate(github.rest.issues.listComments, {
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ issue_number: context.issue.number,
+ per_page: 100
+ });
+
+ let lastDeployIndex = -1;
+ let lastUndeployIndex = -1;
+
+ // Unlike the close/undeploy scan, this path TRIGGERS a deploy, so
+ // only commands from authorized authors count — otherwise a
+ // drive-by !deploy comment (ignored by the comment handler's
+ // permission gate) would arm auto-redeploy for the next push.
+ const allowed = ['OWNER', 'MEMBER', 'COLLABORATOR'];
+ comments.forEach((comment, index) => {
+ if (!allowed.includes(comment.author_association)) return;
+ if (comment.body.trim() === '!deploy') {
+ lastDeployIndex = index;
+ } else if (comment.body.trim() === '!undeploy') {
+ lastUndeployIndex = index;
+ }
+ });
+
+ // Redeploy only if there's an active deployment (a !deploy without a subsequent !undeploy)
+ const shouldDeploy = lastDeployIndex !== -1 && lastDeployIndex > lastUndeployIndex;
+ core.setOutput('should_deploy', shouldDeploy);
+
+ - name: Dispatch Deploy Event (PR updated with active deployment)
+ if: >-
+ github.event_name == 'pull_request' &&
+ github.event.action == 'synchronize' &&
+ steps.check_pr_sync.outputs.should_deploy == 'true'
+ uses: peter-evans/repository-dispatch@v4
+ with:
+ token: ${{ secrets.DISPATCH_TOKEN }}
+ repository: Significant-Gravitas/AutoGPT_cloud_infrastructure
+ event-type: pr-event
+ client-payload: |
+ {
+ "action": "deploy",
+ "pr_number": "${{ github.event.pull_request.number }}",
+ "pr_title": ${{ toJSON(github.event.pull_request.title) }},
+ "pr_state": "${{ github.event.pull_request.state }}",
+ "repo": "${{ github.repository }}"
+ }
+
+ - name: Post PR update redeploy comment
+ if: >-
+ github.event_name == 'pull_request' &&
+ github.event.action == 'synchronize' &&
+ steps.check_pr_sync.outputs.should_deploy == 'true'
+ uses: actions/github-script@v8
+ with:
+ script: |
+ await github.rest.issues.createComment({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ issue_number: context.issue.number,
+ body: `🔄 **Auto-redeploying**: new commits pushed to a PR with an active deployment. Refreshing development environment for PR #${{ github.event.pull_request.number }}.`
+ });