close
Skip to content

fix(preview): re-clone source on rebuild so PR pushes propagate#4319

Open
budivoogt wants to merge 1 commit intoDokploy:canaryfrom
budivoogt:fix/preview-rebuild-fetches-source
Open

fix(preview): re-clone source on rebuild so PR pushes propagate#4319
budivoogt wants to merge 1 commit intoDokploy:canaryfrom
budivoogt:fix/preview-rebuild-fetches-source

Conversation

@budivoogt
Copy link
Copy Markdown

@budivoogt budivoogt commented Apr 28, 2026

Problem

rebuildPreviewApplication skips the clone step that deployPreviewApplication runs on first deploy. As a result, every PR synchronize webhook (i.e., every push to an existing preview branch after the first) only re-runs docker build against the original snapshot taken at PR open.

BuildKit content-hashes the unchanged source, cache-hits every layer including COPY . ., and the deploy finishes in seconds while still serving the original commit. The GitHub Deployments API gets posted `success`, but the running container is never replaced.

Reproduction

Verified empirically on a self-hosted v0.28.8 install:

  1. Open a PR at SHA A — real ~4 min build, image hash A1, container running new code. ✅
  2. Push SHA B, then C, then D to the same PR. Three synchronize events, three Dokploy "deploys" each finishing in 9–10 s, GitHub Deployments reports each as `success`. The Docker image and running container keep hash `A1`. Working tree on disk and BuildKit cache both stuck at SHA `A`.
  3. Manually run `git fetch + git reset --hard origin/` on `/etc/dokploy/applications//code/`, then trigger a redeploy. The next build cache-misses `COPY . .`, takes ~4 min, and produces a fresh image with the new commit's code.

The only thing missing for the rebuild path was the fetch.

Fix

Mirror deployPreviewApplication's pre-build clone in rebuildPreviewApplication. cloneGithubRepository already does `rm -rf code && git clone --depth 1 --branch ` with a fresh installation token issued via the GitHub App — so each rebuild gets the latest tip of the preview branch.

The diff is symmetric with the existing block at line ~427 inside deployPreviewApplication.

Scope

GitHub source type only, matching the existing scope of deployPreviewApplication's clone. GitLab / Gitea / Bitbucket preview rebuilds remain on the previous behavior; extending parity to those providers belongs in a follow-up. Happy to do that in this PR if maintainers prefer — just let me know.

Test plan

  • Open a PR against an app with GitHub source + preview deployments enabled. Confirm build runs to completion.
  • Push a follow-up commit to that PR. Confirm the next deploy:
    • Shows in build log: a fresh `git clone` step (not an immediately-cached `COPY . .` layer)
    • Takes a real build duration (4–5 min for our codebase, will vary)
    • Produces a new docker image hash
    • Container picks up the new commit's code (verified in browser, e.g. a string change in the page)

Related

Verified on v0.28.8; same bug is in v0.29.2 (latest as of this PR — same line, same comment). No prior issue or PR found in tracker for this specific behavior. Closest existing reports are about preview deployment lifecycle (#3248 Destroy & Re-Deploy button, #4203 orphan Swarm services) but not the stale-source-on-rebuild path.

Greptile Summary

This PR fixes a real bug where rebuildPreviewApplication skipped the repository clone step, causing every PR push after the first to rebuild against the original source snapshot. The fix mirrors deployPreviewApplication's clone block exactly, scoped to sourceType === "github" as acknowledged in the description.

Confidence Score: 5/5

This PR is safe to merge — it's a targeted, well-scoped fix with no regressions.

The change is a minimal, symmetric addition that directly mirrors the already-working pattern in deployPreviewApplication. Error handling flows through the existing catch block, the GitHub App token is freshly issued on each call, and non-GitHub source types are explicitly left unchanged (as stated in the description). No new execution paths, no security concerns.

No files require special attention.

Reviews (1): Last reviewed commit: "fix(preview): re-clone source on rebuild..." | Re-trigger Greptile

`rebuildPreviewApplication` (the path that runs for every `synchronize`
webhook on an existing preview deployment) skipped the git clone step
that `deployPreviewApplication` runs on first deploy. As a result, every
push to a PR after the first triggered a `docker build` against the
original snapshot taken at PR open. BuildKit content-hashed the
unchanged source, cache-hit every layer including `COPY . .`, and the
deploy finished in seconds while still serving the original commit.
The GitHub Deployments API was posted `success`, but the running
container was never replaced.

Symptom (verified on a real preview):

- PR opened at SHA A → real ~4 min build, image hash A1, container OK.
- PR pushed SHA B, C, D → three `synchronize` events, three
  Dokploy "deploys" each finishing in 9-10 s, GitHub Deployments
  reports `success`, but the container kept image hash A1. Working
  tree on disk and BuildKit cache both stuck at SHA A.
- After a manual `git fetch + git reset --hard origin/<branch>` on
  the server's `/etc/dokploy/applications/<preview>/code/`, the next
  webhook-triggered build cache-missed `COPY . .` and produced a real
  rebuild — confirming the only thing missing was the fetch.

Fix: mirror `deployPreviewApplication`'s pre-build clone in
`rebuildPreviewApplication`. `cloneGithubRepository` already does
`rm -rf code && git clone --depth 1 --branch <branch>` with a fresh
installation token, so each rebuild gets the latest tip of the
preview branch.

Scope: github source type only, matching the existing scope of
`deployPreviewApplication`'s clone. GitLab / Gitea / Bitbucket preview
rebuilds remain on the previous behavior; extending parity to those
providers belongs in a follow-up.
@budivoogt budivoogt requested a review from Siumauricio as a code owner April 28, 2026 12:48
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Apr 28, 2026
budivoogt added a commit to budivoogt/dokploy that referenced this pull request Apr 28, 2026
Two additions to CTD-FORK.md:

- New row in the "Active fork customizations" table for the
  preview-rebuild fetch fix, with link to upstream PR Dokploy#4319 and a note
  to remove this row when upstream merges.
- A "Fresh-install / disaster-recovery" section flagging that
  Dokploy's official installer pulls `dokploy/dokploy:latest` from
  Docker Hub (unpatched upstream), so any host rebuild needs to be
  followed immediately by `bin/deploy-ctd.sh <tag>` to swap to the
  patched image. Same applies to upstream version upgrades — rebase
  the CTD branch first, don't update directly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant