Skip to content

fix(builds): reuse the cloned branch when pushing a template into a repository - #14549

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/template-push-existing-branch
Oct 15, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/template-push-existing-branch

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Deploying a template into a connected repository fails with Unable to push code repository: fatal: a branch named 'main' already exists.

The Builds worker clones the repository, merges the template in, then runs git checkout -b <providerBranch> before committing and pushing. When the deployment has no commit hash, the clone is a branch clone, and the shared clone command already leaves that branch checked out (git pull + git checkout <branch>, or git checkout -b <branch> when the remote branch is missing). The second checkout -b then fails. This happens on every tag-type template (redeployVcsFunction/redeployVcsSite set only providerBranch for tags), and on branch-type templates when the head commit lookup fails.

The worker now runs git checkout -B <branch>. On a branch clone the branch is already at HEAD, so this is a no-op. On a commit clone (detached HEAD) it creates the branch at that commit, as -b did. Commit and push are unchanged.

#14137 (the earlier CLOUD-3R60 fix) is in cloud 1.50.182 (server-ce 939fa5e8cc). It fixed wildcard tag resolution when cloning the template. That let tag-type template deployments get as far as the push, where they hit this bug.

Fixes CLOUD-3R7M
Fixes CLOUD-3R60

Test Plan

  • New VCSGiteaConsoleClientTest::testCreateTemplateDeploymentIntoRepository deploys the starter template into a Gitea repository for both clone types: a tag template (branch clone of main) and a branch template (commit clone, detached HEAD). It asserts that the build is ready, the template commit is the head of main, the deployment's providerCommitHash matches it, and the template files are in the repository.
  • Without the fix, the branch-clone case fails, and the worker logs Unable to push code repository: fatal: a branch named 'main' already exists. With the fix, both cases pass, and the full VCSGitea suite passes (26 tests).
  • The VCSGitea lane in .github/ci/e2e.json now also claims Functions/Workers/Builds.php, so it runs on changes to this worker.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

Closes #14550

@luxor-code-review

luxor-code-review Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to

🟡 Medium — Failed build leaves stale /tmp/builds/{id} breaking retries · Builds.php:427 · unchanged line

// src/Appwrite/Platform/Modules/Functions/Workers/Builds.php
427	        } catch (\Throwable $th) {

The build working tree is keyed by deployment id (/tmp/builds/{deploymentId}, Builds.php:279) and is only removed after a fully successful run at Builds.php:426; the catch block starting at Builds.php:427 never cleans it. A build that fails partway leaves a half-initialized repo, and the clone chain (packages/vcs/src/Adapter/Git.php:330-345) re-runs git remote add origin on retry, which exits non-zero because origin already exists — so the retried build fails before reaching the fixed -B checkout. The deployment becomes permanently un-buildable despite the retry mechanism.


The change addresses a template-deployment failure for Gitea-backed functions and pins it with an e2e test covering both clone paths.

Files Change
src/Appwrite/Platform/Modules/Functions/Workers/Builds.php Adjusts the git clone/checkout flow for template deployments so branch and commit clones check out the target ref correctly before committing and pushing the template content.
tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php Adds a dataProvider-driven testCreateTemplateDeploymentIntoRepository exercising both tag-clone and commit-clone paths end to end via the Gitea provider.
.github/ci/e2e.json Adds Workers/Builds.php to the VCSGitea e2e lane so build-worker changes run the new suite.

Reviewed commit: a6c05ce

@ghost

ghost commented Oct 15, 2026

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The checkout change handles both clone states without disrupting the intended commit and push flow, and the added integration cases exercise each state.

The Builds worker now reuses an already checked-out branch or creates it from a detached commit before committing and pushing template files. The VCSGitea e2e suite covers both clone modes, and its CI lane now includes the worker path.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Platform/Modules/Functions/Workers/Builds.php Switches the push checkout to git checkout -B for branch and commit clones.
tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php Adds template deployment coverage for branch-clone and detached-commit-clone cases.
.github/ci/e2e.json Includes the Builds worker among paths that select the VCSGitea e2e lane.

Reviewed a6c05ce · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 15, 2026
@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/template-push-existing-branch (after).

Metric Before After Change
🚀 Requests/sec 189.72 205.79 🟢 +8.5%
⏱️ Latency P50 88.6 ms 84.67 ms ⚪ -4.4%
⏱️ Latency P95 215.33 ms 191.24 ms 🟢 -11.2%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 84.67 191.24 12,711 205.79 -24.09
Account 150.04 279.44 669 11.12 -48.32
TablesDB 82.47 149.47 6,913 113.72 -17.63
Storage 79.28 187.74 3,345 57.04 -5.3
Functions 114.36 220.04 1,784 31.29 -25.38

Top API waits (after)

API request Max wait (ms)
account.name.update 523.06
tablesdb.rows.update 471.94
functions.delete 454.87
functions.create 417.28
account.prefs.update 403.04

@ChiragAgg5k
ChiragAgg5k merged commit 5e3b37d into main Oct 15, 2026
68 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/template-push-existing-branch branch October 15, 2026 11:17
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reuse the cloned branch when pushing a template into a repository

1 participant