An open-source, faster Devin alternative
TypeScript
Switch preview envs to comment trigger with auto-sync on push
motatoes12951edd79c9
diff --git a/.github/workflows/preview-env-destroy.yml b/.github/workflows/preview-env-destroy.ymlindex 3804a70f..8a4c46bc 100644--- a/.github/workflows/preview-env-destroy.yml+++ b/.github/workflows/preview-env-destroy.yml@@ -3,6 +3,8 @@ name: Preview Environment — Destroyon:pull_request:types: [closed]+ issue_comment:+ types: [created]permissions:contents: read@@ -10,17 +12,43 @@ permissions:id-token: writeenv:- ENV_NAME: dev-pr-${{ github.event.pull_request.number }}TF_DIR: deploy/terraform-concurrency:- group: preview-env-${{ github.event.pull_request.number }}-jobs:destroy:name: Destroy Preview Environment+ if: >-+ (github.event_name == 'pull_request' && github.event.action == 'closed') ||+ (github.event_name == 'issue_comment' &&+ github.event.issue.pull_request &&+ contains(github.event.comment.body, '/destroy-preview'))runs-on: ubuntu-lateststeps:+ - name: Determine PR number+ id: pr+ uses: actions/github-script@v7+ with:+ script: |+ let prNumber;+ if (context.eventName === 'pull_request') {+ prNumber = context.payload.pull_request.number;+ } else {+ prNumber = context.payload.issue.number;+ }+ core.setOutput('number', prNumber);++ - name: React to comment+ if: github.event_name == 'issue_comment'+ uses: actions/github-script@v7+ with:+ script: |+ await github.rest.reactions.createForIssueComment({+ owner: context.repo.owner,+ repo: context.repo.repo,+ comment_id: context.payload.comment.id,+ content: 'eyes',+ });+- name: Checkout codeuses: actions/checkout@v4@@ -40,7 +68,7 @@ jobs:run: |terraform init \-backend-config="bucket=${{ secrets.TF_STATE_BUCKET }}" \- -backend-config="key=pr-${{ github.event.pull_request.number }}/terraform.tfstate" \+ -backend-config="key=pr-${{ steps.pr.outputs.number }}/terraform.tfstate" \-backend-config="dynamodb_table=${{ secrets.TF_LOCK_TABLE }}" \-backend-config="region=us-east-1"@@ -48,26 +76,24 @@ jobs:working-directory: ${{ env.TF_DIR }}run: |terraform destroy -auto-approve \- -var="environment=${{ env.ENV_NAME }}" \+ -var="environment=dev-pr-${{ steps.pr.outputs.number }}" \-var="key_pair_name=${{ secrets.KEY_PAIR_NAME || 'opensandbox-dev' }}" \- -var="api_key=pr-${{ github.event.pull_request.number }}-key" \- -var="jwt_secret=pr-${{ github.event.pull_request.number }}-jwt"+ -var="api_key=pr-${{ steps.pr.outputs.number }}-key" \+ -var="jwt_secret=pr-${{ steps.pr.outputs.number }}-jwt"- name: Clean up state from S3run: |- aws s3 rm "s3://${{ secrets.TF_STATE_BUCKET }}/pr-${{ github.event.pull_request.number }}/" --recursive || true+ aws s3 rm "s3://${{ secrets.TF_STATE_BUCKET }}/pr-${{ steps.pr.outputs.number }}/" --recursive || true- name: Comment on PRuses: actions/github-script@v7with:script: |- const prNumber = context.payload.pull_request.number;- const merged = context.payload.pull_request.merged;- const action = merged ? 'merged' : 'closed';+ const prNumber = ${{ steps.pr.outputs.number }};const body = [`## Preview Environment Destroyed`,``,- `The preview environment \`${{ env.ENV_NAME }}\` has been torn down (PR ${action}).`,+ `The preview environment \`dev-pr-${prNumber}\` has been torn down.`,`All AWS resources for this environment have been cleaned up.`,].join('\n');diff --git a/.github/workflows/preview-env.yml b/.github/workflows/preview-env.ymlindex 7b0e8767..6f23e786 100644--- a/.github/workflows/preview-env.yml+++ b/.github/workflows/preview-env.yml@@ -1,8 +1,10 @@name: Preview Environment — Deployon:+ issue_comment:+ types: [created]pull_request:- types: [opened, synchronize, reopened]+ types: [synchronize]permissions:contents: read@@ -10,27 +12,53 @@ permissions:id-token: writeenv:- ENV_NAME: dev-pr-${{ github.event.pull_request.number }}TF_DIR: deploy/terraformSSH_USER: ubuntuREMOTE_DIR: /home/ubuntu/opensandbox-concurrency:- group: preview-env-${{ github.event.pull_request.number }}- cancel-in-progress: true-jobs:deploy:name: Deploy Preview Environment+ # Run if: /deploy-preview comment on a PR, OR push to a PR (synchronize)+ if: >-+ (github.event_name == 'issue_comment' &&+ github.event.issue.pull_request &&+ contains(github.event.comment.body, '/deploy-preview')) ||+ (github.event_name == 'pull_request' &&+ github.event.action == 'synchronize')runs-on: ubuntu-lateststeps:- - name: Checkout code- uses: actions/checkout@v4+ - name: React to comment+ if: github.event_name == 'issue_comment'+ uses: actions/github-script@v7+ with:+ script: |+ await github.rest.reactions.createForIssueComment({+ owner: context.repo.owner,+ repo: context.repo.repo,+ comment_id: context.payload.comment.id,+ content: 'rocket',+ });- - name: Setup Terraform- uses: hashicorp/setup-terraform@v3+ - name: Get PR details+ id: pr+ uses: actions/github-script@v7with:- terraform_wrapper: false+ script: |+ let pr;+ if (context.eventName === 'issue_comment') {+ const { data } = await github.rest.pulls.get({+ owner: context.repo.owner,+ repo: context.repo.repo,+ pull_number: context.payload.issue.number,+ });+ pr = data;+ } else {+ pr = context.payload.pull_request;+ }+ core.setOutput('number', pr.number);+ core.setOutput('ref', pr.head.ref);+ core.setOutput('sha', pr.head.sha);- name: Configure AWS credentials (OIDC)uses: aws-actions/configure-aws-credentials@v4@@ -38,25 +66,69 @@ jobs:role-to-assume: ${{ secrets.AWS_ROLE_ARN }}aws-region: us-east-1+ - name: Check if preview environment exists+ if: github.event_name == 'pull_request'+ id: check_env+ run: |+ if aws s3 ls "s3://${{ secrets.TF_STATE_BUCKET }}/pr-${{ steps.pr.outputs.number }}/terraform.tfstate" 2>/dev/null; then+ echo "exists=true" >> "$GITHUB_OUTPUT"+ else+ echo "exists=false" >> "$GITHUB_OUTPUT"+ echo "No preview environment found for PR ${{ steps.pr.outputs.number }}, skipping update."+ fi++ - name: Skip if no existing environment+ if: >-+ github.event_name == 'pull_request' &&+ steps.check_env.outputs.exists != 'true'+ run: |+ echo "::notice::No preview environment exists for this PR. Comment /deploy-preview to create one."+ exit 0++ - name: Checkout PR branch+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'+ uses: actions/checkout@v4+ with:+ ref: ${{ steps.pr.outputs.ref }}++ - name: Setup Terraform+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'+ uses: hashicorp/setup-terraform@v3+ with:+ terraform_wrapper: false+- name: Terraform Init+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'working-directory: ${{ env.TF_DIR }}run: |terraform init \-backend-config="bucket=${{ secrets.TF_STATE_BUCKET }}" \- -backend-config="key=pr-${{ github.event.pull_request.number }}/terraform.tfstate" \+ -backend-config="key=pr-${{ steps.pr.outputs.number }}/terraform.tfstate" \-backend-config="dynamodb_table=${{ secrets.TF_LOCK_TABLE }}" \-backend-config="region=us-east-1"- name: Terraform Apply+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'working-directory: ${{ env.TF_DIR }}run: |terraform apply -auto-approve \- -var="environment=${{ env.ENV_NAME }}" \+ -var="environment=dev-pr-${{ steps.pr.outputs.number }}" \-var="key_pair_name=${{ secrets.KEY_PAIR_NAME || 'opensandbox-dev' }}" \- -var="api_key=pr-${{ github.event.pull_request.number }}-key" \- -var="jwt_secret=pr-${{ github.event.pull_request.number }}-jwt"+ -var="api_key=pr-${{ steps.pr.outputs.number }}-key" \+ -var="jwt_secret=pr-${{ steps.pr.outputs.number }}-jwt"- name: Get instance IP+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'id: tf_outputworking-directory: ${{ env.TF_DIR }}run: |@@ -64,11 +136,13 @@ jobs:echo "dev_ip=$DEV_IP" >> "$GITHUB_OUTPUT"- name: Configure SSH+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |mkdir -p ~/.sshecho "${{ secrets.PREVIEW_SSH_PRIVATE_KEY }}" > ~/.ssh/preview.pemchmod 600 ~/.ssh/preview.pem- # Retry ssh-keyscan a few times while instance bootsfor i in 1 2 3 4 5; dossh-keyscan -H ${{ steps.tf_output.outputs.dev_ip }} >> ~/.ssh/known_hosts 2>/dev/null && breakecho "Waiting for SSH to become available (attempt $i)..."@@ -76,6 +150,9 @@ jobs:done- name: Wait for instance to be ready+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |for i in $(seq 1 30); doif ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 -i ~/.ssh/preview.pem \@@ -88,6 +165,9 @@ jobs:done- name: Provision instance (idempotent)+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |ssh -o StrictHostKeyChecking=no -i ~/.ssh/preview.pem \${{ env.SSH_USER }}@${{ steps.tf_output.outputs.dev_ip }} \@@ -97,6 +177,9 @@ jobs:'sudo bash -s' < deploy/ec2/setup-single-host.sh- name: Rsync repo to instance+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |rsync -az --delete \-e "ssh -o StrictHostKeyChecking=no -i ~/.ssh/preview.pem" \@@ -108,6 +191,9 @@ jobs:./ ${{ env.SSH_USER }}@${{ steps.tf_output.outputs.dev_ip }}:${{ env.REMOTE_DIR }}/- name: Build binaries on instance+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |ssh -o StrictHostKeyChecking=no -i ~/.ssh/preview.pem \${{ env.SSH_USER }}@${{ steps.tf_output.outputs.dev_ip }} "@@ -128,6 +214,9 @@ jobs:"- name: Build rootfs (idempotent)+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |ssh -o StrictHostKeyChecking=no -i ~/.ssh/preview.pem \${{ env.SSH_USER }}@${{ steps.tf_output.outputs.dev_ip }} "@@ -142,36 +231,45 @@ jobs:"- name: Install env files and restart services+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |ssh -o StrictHostKeyChecking=no -i ~/.ssh/preview.pem \${{ env.SSH_USER }}@${{ steps.tf_output.outputs.dev_ip }} "- sudo bash ${{ env.REMOTE_DIR }}/deploy/ec2/setup-dev-env.sh 'pr-${{ github.event.pull_request.number }}-key'+ sudo bash ${{ env.REMOTE_DIR }}/deploy/ec2/setup-dev-env.sh 'pr-${{ steps.pr.outputs.number }}-key'sudo systemctl restart opensandbox-serversudo systemctl restart opensandbox-workerecho 'Services restarted'"- name: Wait for services and seed database+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |sleep 5ssh -o StrictHostKeyChecking=no -i ~/.ssh/preview.pem \${{ env.SSH_USER }}@${{ steps.tf_output.outputs.dev_ip }} "- API_KEY='pr-${{ github.event.pull_request.number }}-key'+ API_KEY='pr-${{ steps.pr.outputs.number }}-key'for i in \$(seq 1 15); dosudo docker exec postgres psql -U opensandbox -d opensandbox -q -c 'SELECT 1 FROM orgs LIMIT 0' 2>/dev/null && breakecho 'Waiting for migrations...'sleep 2donesudo docker exec postgres psql -U opensandbox -d opensandbox -q -c \"- INSERT INTO orgs (name, slug) VALUES ('PR ${{ github.event.pull_request.number }}', 'pr-${{ github.event.pull_request.number }}') ON CONFLICT (slug) DO NOTHING;+ INSERT INTO orgs (name, slug) VALUES ('PR ${{ steps.pr.outputs.number }}', 'pr-${{ steps.pr.outputs.number }}') ON CONFLICT (slug) DO NOTHING;INSERT INTO api_keys (org_id, key_hash, key_prefix, name)SELECT id, encode(sha256('\${API_KEY}'::bytea), 'hex'), '\${API_KEY}', 'PR Key'- FROM orgs WHERE slug = 'pr-${{ github.event.pull_request.number }}'+ FROM orgs WHERE slug = 'pr-${{ steps.pr.outputs.number }}'ON CONFLICT (key_hash) DO NOTHING;\" && echo 'Database seeded' || echo 'WARNING: Seed failed'"- name: Health check+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'run: |sleep 3ssh -o StrictHostKeyChecking=no -i ~/.ssh/preview.pem \@@ -179,20 +277,25 @@ jobs:"curl -sf http://localhost:8080/health > /dev/null 2>&1 && echo 'Server health: OK' || echo 'Server health: waiting...'"- name: Comment on PR+ if: >-+ github.event_name == 'issue_comment' ||+ steps.check_env.outputs.exists == 'true'uses: actions/github-script@v7with:script: |const ip = '${{ steps.tf_output.outputs.dev_ip }}';- const prNumber = context.payload.pull_request.number;+ const prNumber = ${{ steps.pr.outputs.number }};const apiKey = `pr-${prNumber}-key`;+ const isUpdate = '${{ github.event_name }}' === 'pull_request';const body = [- `## Preview Environment Ready`,+ `## Preview Environment ${isUpdate ? 'Updated' : 'Ready'}`,``,`| Resource | Value |`,`|----------|-------|`,`| **Server URL** | \`http://${ip}:8080\` |`,`| **API Key** | \`${apiKey}\` |`,- `| **Environment** | \`${{ env.ENV_NAME }}\` |`,+ `| **Environment** | \`dev-pr-${prNumber}\` |`,+ `| **Commit** | \`${{ steps.pr.outputs.sha }}\` |`,``,`### Quick test`,`\`\`\`bash`,@@ -207,7 +310,7 @@ jobs:`ssh -i ~/.ssh/opensandbox-dev.pem ubuntu@${ip}`,`\`\`\``,``,- `> This environment will be destroyed when the PR is closed or merged.`,+ `> Comment \`/destroy-preview\` or close the PR to tear down this environment.`,].join('\n');// Find and update existing comment, or create new one