Add OpenCode integration for Sandbox SDK #336
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish Preview to pkg.pr.new | |
| permissions: | |
| contents: read | |
| pull-requests: write # Required for pkg.pr.new to comment on PRs | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| paths: | |
| - '**' | |
| - '!**/*.md' | |
| - '!.changeset/**' | |
| concurrency: | |
| group: pkg-pr-new-${{ github.event.pull_request.number }} | |
| cancel-in-progress: true | |
| jobs: | |
| publish-preview: | |
| if: ${{ !contains(github.event.pull_request.title, 'Version Packages') }} | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 30 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 24 | |
| cache: 'npm' | |
| - name: Setup Bun | |
| uses: oven-sh/setup-bun@v2 | |
| with: | |
| bun-version: latest | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build packages | |
| run: npm run build | |
| - name: Set preview version | |
| id: package-version | |
| run: | | |
| PR_NUMBER=${{ github.event.pull_request.number }} | |
| GIT_HASH=$(git rev-parse --short HEAD) | |
| VERSION="0.0.0-pr-${PR_NUMBER}-${GIT_HASH}" | |
| # Update package.json with the preview version | |
| node -e " | |
| const fs = require('fs'); | |
| const path = './packages/sandbox/package.json'; | |
| const pkg = JSON.parse(fs.readFileSync(path, 'utf8')); | |
| pkg.version = '${VERSION}'; | |
| fs.writeFileSync(path, JSON.stringify(pkg, null, 2) + '\n'); | |
| " | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "Docker image will be tagged as: cloudflare/sandbox:${VERSION}" | |
| echo "Preview version: $VERSION" | |
| - name: Resolve workspace dependencies | |
| run: npx tsx .github/resolve-workspace-versions.ts | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
| password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | |
| - name: Build and push Docker image (default) | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: packages/sandbox/Dockerfile | |
| target: default | |
| platforms: linux/amd64 | |
| push: true | |
| tags: cloudflare/sandbox:${{ steps.package-version.outputs.version }} | |
| cache-from: | | |
| type=gha,scope=preview-pr-${{ github.event.pull_request.number }}-default | |
| type=gha,scope=release-default | |
| cache-to: type=gha,mode=max,scope=preview-pr-${{ github.event.pull_request.number }}-default | |
| build-args: | | |
| SANDBOX_VERSION=${{ steps.package-version.outputs.version }} | |
| - name: Build and push Docker image (python) | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: packages/sandbox/Dockerfile | |
| target: python | |
| platforms: linux/amd64 | |
| push: true | |
| tags: cloudflare/sandbox:${{ steps.package-version.outputs.version }}-python | |
| cache-from: | | |
| type=gha,scope=preview-pr-${{ github.event.pull_request.number }}-python | |
| type=gha,scope=release-python | |
| cache-to: type=gha,mode=max,scope=preview-pr-${{ github.event.pull_request.number }}-python | |
| build-args: | | |
| SANDBOX_VERSION=${{ steps.package-version.outputs.version }} | |
| - name: Build and push Docker image (opencode) | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| file: packages/sandbox/Dockerfile | |
| target: opencode | |
| platforms: linux/amd64 | |
| push: true | |
| tags: cloudflare/sandbox:${{ steps.package-version.outputs.version }}-opencode | |
| cache-from: | | |
| type=gha,scope=preview-pr-${{ github.event.pull_request.number }}-opencode | |
| type=gha,scope=release-opencode | |
| cache-to: type=gha,mode=max,scope=preview-pr-${{ github.event.pull_request.number }}-opencode | |
| build-args: | | |
| SANDBOX_VERSION=${{ steps.package-version.outputs.version }} | |
| - name: Publish to pkg.pr.new | |
| run: npx pkg-pr-new publish './packages/sandbox' | |
| - name: Comment Docker image tag | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const version = '${{ steps.package-version.outputs.version }}'; | |
| const defaultTag = `cloudflare/sandbox:${version}`; | |
| const pythonTag = `cloudflare/sandbox:${version}-python`; | |
| const opencodeTag = `cloudflare/sandbox:${version}-opencode`; | |
| const body = `### 🐳 Docker Images Published\n\n**Default (no Python):**\n\`\`\`dockerfile\nFROM ${defaultTag}\n\`\`\`\n\n**With Python:**\n\`\`\`dockerfile\nFROM ${pythonTag}\n\`\`\`\n\n**With OpenCode:**\n\`\`\`dockerfile\nFROM ${opencodeTag}\n\`\`\`\n\n**Version:** \`${version}\`\n\nUse the \`-python\` variant for Python code execution, or \`-opencode\` for the OpenCode AI coding agent.`; | |
| // Find existing comment | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| }); | |
| const botComment = comments.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('Docker Images Published') | |
| ); | |
| if (botComment) { | |
| // Update existing comment | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: botComment.id, | |
| body: body | |
| }); | |
| } else { | |
| // Create new comment | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: context.issue.number, | |
| body: body | |
| }); | |
| } |