Skip to content

Modern Docker Management, Designed for Everyone

feat: add Back up to Git sync mode and link existing projects to syncs (#3932)

Kyle Mendelldadf87f93d92
diff --git a/backend/go.mod b/backend/go.mod
index a04f6faa9..ba1dd96c1 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -34,6 +34,7 @@ require (
github.com/fsnotify/fsnotify v1.10.1
github.com/getarcaneapp/arcane/cli/v2 v2.11.1
github.com/getarcaneapp/arcane/types/v2 v2.11.1
+ github.com/go-git/go-billy/v5 v5.9.0
github.com/go-git/go-git/v5 v5.19.2
github.com/go-webauthn/webauthn v0.18.1
github.com/gofrs/flock v0.13.1
@@ -162,7 +163,6 @@ require (
github.com/fvbommel/sortorder v1.2.0 // indirect
github.com/fxamacker/cbor/v2 v2.9.3 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
- github.com/go-git/go-billy/v5 v5.9.0 // indirect
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
github.com/go-logr/logr v1.4.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.go
index ab78cb08e..fa0ada1c7 100644
--- a/backend/internal/bootstrap/bootstrap.go
+++ b/backend/internal/bootstrap/bootstrap.go
@@ -329,6 +329,9 @@ func initializeGitOpsStartupStateInternal(appCtx context.Context, gitOpsSync *gi
if err := gitOpsSync.CleanupOrphanedSyncsOnStartup(appCtx); err != nil {
slog.WarnContext(appCtx, "Failed to clean up orphaned GitOps syncs on startup", "error", err)
}
+ if err := gitOpsSync.ReconcileInterruptedBackupsOnStartup(appCtx); err != nil {
+ slog.WarnContext(appCtx, "Failed to reconcile interrupted Git backups on startup", "error", err)
+ }
// Sweep leaked gitops scratch dirs before the filesystem watcher can import
// them as phantom projects and before the directory-sync reconcile runs.
if err := gitOpsSync.CleanupLeakedScratchDirsOnStartup(appCtx); err != nil {
diff --git a/backend/internal/bootstrap/jobs_bootstrap.go b/backend/internal/bootstrap/jobs_bootstrap.go
index 63b91ba35..ad7e4a1d5 100644
--- a/backend/internal/bootstrap/jobs_bootstrap.go
+++ b/backend/internal/bootstrap/jobs_bootstrap.go
@@ -249,6 +249,7 @@ func registerDynamicJobs(params dynamicJobsParams) error {
return err
}
params.GitOpsSync.RegisterAutoSyncJobsOnStartup(params.AppCtx)
+ params.GitOpsSync.SubscribeProjectFileChanges(params.AppCtx)
}
// Environment health: one job per enabled environment (manager only). The Jobs
diff --git a/backend/internal/gitops/backup.go b/backend/internal/gitops/backup.go
new file mode 100644
index 000000000..a6330e67a
--- /dev/null
+++ b/backend/internal/gitops/backup.go
@@ -0,0 +1,645 @@
+package gitops
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+ "path"
+ "strings"
+ "sync"
+ "time"
+
+ "emperror.dev/errors"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/common"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/database"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/event"
+ projectpkg "github.com/getarcaneapp/arcane/backend/v2/internal/project"
+ git "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
+ "github.com/getarcaneapp/arcane/types/v2/gitops"
+ schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"
+ "gorm.io/gorm"
+ "gorm.io/gorm/clause"
+)
+
+const (
+ defaultBackupIntervalMinutes = 5
+ defaultBackupSaveDebounce = 10 * time.Second
+ backupPushAttempts = 3
+ backupStatusRunning = "running"
+ backupStatusConflict = "conflict"
+ backupAuthorName = "Arcane"
+ backupAuthorEmail = "arcane@localhost"
+ backupCommitFileLines = 20
+)
+
+// backupRuntimeInternal holds the in-memory coordination for backup syncs:
+// per-branch write serialization and the save debounce timers.
+type backupRuntimeInternal struct {
+ mu sync.Mutex
+ branches map[string]*sync.Mutex
+ timers map[string]*time.Timer
+ debounce time.Duration
+}
+
+func newBackupRuntimeInternal() *backupRuntimeInternal {
+ return &backupRuntimeInternal{
+ branches: make(map[string]*sync.Mutex),
+ timers: make(map[string]*time.Timer),
+ debounce: defaultBackupSaveDebounce,
+ }
+}
+
+func (r *backupRuntimeInternal) branchLock(repositoryID, branch string) *sync.Mutex {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ key := repositoryID + "\x00" + branch
+ lock, ok := r.branches[key]
+ if !ok {
+ lock = &sync.Mutex{}
+ r.branches[key] = lock
+ }
+ return lock
+}
+
+func (r *backupRuntimeInternal) schedule(syncID string, run func()) {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ if timer, ok := r.timers[syncID]; ok {
+ timer.Stop()
+ }
+ r.timers[syncID] = time.AfterFunc(r.debounce, func() {
+ r.mu.Lock()
+ delete(r.timers, syncID)
+ r.mu.Unlock()
+ run()
+ })
+}
+
+func (r *backupRuntimeInternal) cancel(syncID string) {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ if timer, ok := r.timers[syncID]; ok {
+ timer.Stop()
+ delete(r.timers, syncID)
+ }
+}
+
+func normalizeSyncModeInternal(mode string) (string, error) {
+ switch strings.TrimSpace(mode) {
+ case "", gitops.SyncModeDeploy:
+ return gitops.SyncModeDeploy, nil
+ case gitops.SyncModeBackup:
+ return gitops.SyncModeBackup, nil
+ default:
+ return "", common.Classify(common.ErrValidation, errors.WithDetails(errors.Errorf("unsupported sync mode %q", mode), "field", "mode"))
+ }
+}
+
+// backupCreateConfigInternal is the validated backup configuration for a new sync.
+type backupCreateConfigInternal struct {
+ project *projectpkg.Project
+ directory string
+ paths []string
+ composeFile string
+ backupOnSave bool
+}
+
+// prepareBackupCreateInternal validates a backup-mode create request against
+// the project, its compose files, and other backups on the same branch.
+func (s *GitOpsSyncService) prepareBackupCreateInternal(ctx context.Context, tx *gorm.DB, req gitops.CreateSyncRequest) (*backupCreateConfigInternal, error) {
+ if req.HasDeploymentOptions() {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(errors.New("deployment options cannot be set on a backup sync"), "field", "mode"))
+ }
+ if strings.TrimSpace(req.ProjectID) == "" {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(errors.New("a project is required for a backup sync"), "field", "projectId"))
+ }
+ project, err := lockProjectForSyncInternal(tx, req.ProjectID)
+ if err != nil {
+ return nil, err
+ }
+ if project.GitOpsManagedBy != nil && strings.TrimSpace(*project.GitOpsManagedBy) != "" {
+ return nil, common.Classify(common.ErrConflict, errors.New("project is deployed from Git; disconnect that sync before backing it up"))
+ }
+ var existing int64
+ if err := tx.Model(&projectpkg.GitOpsSync{}).
+ Where("mode = ? AND project_id = ?", gitops.SyncModeBackup, project.ID).
+ Count(&existing).Error; err != nil {
+ return nil, errors.WrapIf(err, "failed to check existing backups")
+ }
+ if existing > 0 {
+ return nil, common.Classify(common.ErrConflict, errors.New("project already has a Git backup; disconnect it first"))
+ }
+
+ directory, err := normalizeBackupDirectoryInternal(req.BackupDirectory)
+ if err != nil {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(err, "field", "backupDirectory"))
+ }
+ if err := ensureBackupDestinationFreeInternal(tx, "", req.RepositoryID, req.Branch, directory); err != nil {
+ return nil, err
+ }
+ if err := s.projectService.EnsureProjectPathUnderRoot(ctx, project, false); err != nil {
+ return nil, err
+ }
+ composeFile, composeFiles, err := s.backupComposeFilesInternal(ctx, project)
+ if err != nil {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(err, "field", "projectId"))
+ }
+ paths, err := normalizeBackupPathsInternal(req.BackupPaths)
+ if err != nil {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(err, "field", "backupPaths"))
+ }
+ if len(paths) == 0 {
+ paths = composeFiles
+ }
+ if !backupSelectionCoversInternal(paths, composeFile) {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(errors.New("the backup selection must include the compose file "+composeFile), "field", "backupPaths"))
+ }
+ config := &backupCreateConfigInternal{project: project, directory: directory, paths: paths, composeFile: composeFile, backupOnSave: true}
+ if req.BackupOnSave != nil {
+ config.backupOnSave = *req.BackupOnSave
+ }
+ return config, nil
+}
+
+// lockProjectForSyncInternal loads a project inside tx with a row lock so the
+// one-Git-relationship-per-project checks and the sync insert are atomic.
+func lockProjectForSyncInternal(tx *gorm.DB, projectID string) (*projectpkg.Project, error) {
+ var project projectpkg.Project
+ if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", projectID).First(&project).Error; err != nil {
+ if errors.Is(err, gorm.ErrRecordNotFound) {
+ return nil, common.ErrProjectNotFound
+ }
+ return nil, errors.WrapIff(err, "failed to get project %s", projectID)
+ }
+ return &project, nil
+}
+
+// ensureBackupDestinationFreeInternal rejects a destination that equals or
+// nests inside another backup's destination on the same repository branch.
+func ensureBackupDestinationFreeInternal(tx *gorm.DB, excludeSyncID, repositoryID, branch, directory string) error {
+ var others []projectpkg.GitOpsSync
+ q := tx.
+ Select("id", "backup_directory").
+ Where("mode = ? AND repository_id = ? AND branch = ?", gitops.SyncModeBackup, repositoryID, branch)
+ if excludeSyncID != "" {
+ q = q.Where("id <> ?", excludeSyncID)
+ }
+ if err := q.Find(&others).Error; err != nil {
+ return errors.WrapIf(err, "failed to check backup destinations")
+ }
+ for _, other := range others {
+ if backupDirectoriesOverlapInternal(other.BackupDirectory, directory) {
+ return common.Classify(common.ErrConflict, errors.Errorf("backup directory %q overlaps with an existing backup at %q on this branch", directory, other.BackupDirectory))
+ }
+ }
+ return nil
+}
+
+// applyModeUpdatesInternal validates the mode-specific fields of an update and
+// appends the resulting column updates.
+func (s *GitOpsSyncService) applyModeUpdatesInternal(ctx context.Context, current *projectpkg.GitOpsSync, req gitops.UpdateSyncRequest, updates map[string]any) error {
+ if current.IsBackup() {
+ return s.applyBackupUpdateInternal(ctx, current, req, updates)
+ }
+ if req.HasBackupOptions() {
+ return common.Classify(common.ErrValidation, errors.WithDetails(errors.New("backup options cannot be set on a deployment sync"), "field", "mode"))
+ }
+ return nil
+}
+
+// applyBackupUpdateInternal validates backup-mode update fields and appends the
+// resulting column updates.
+func (s *GitOpsSyncService) applyBackupUpdateInternal(ctx context.Context, current *projectpkg.GitOpsSync, req gitops.UpdateSyncRequest, updates map[string]any) error {
+ if req.HasDeploymentOptions() {
+ return common.Classify(common.ErrValidation, errors.WithDetails(errors.New("deployment options cannot be set on a backup sync"), "field", "mode"))
+ }
+ repositoryID := current.RepositoryID
+ if req.RepositoryID != nil {
+ repositoryID = *req.RepositoryID
+ }
+ branch := current.Branch
+ if req.Branch != nil {
+ branch = *req.Branch
+ }
+ if repositoryID != current.RepositoryID || branch != current.Branch {
+ if err := ensureBackupDestinationFreeInternal(s.db.WithContext(ctx), current.ID, repositoryID, branch, current.BackupDirectory); err != nil {
+ return err
+ }
+ updates["last_backup_snapshot"] = nil
+ updates["backup_conflict"] = false
+ updates["backup_failure_reason"] = nil
+ }
+ if len(req.BackupPaths) > 0 {
+ paths, err := normalizeBackupPathsInternal(req.BackupPaths)
+ if err != nil {
+ return common.Classify(common.ErrValidation, errors.WithDetails(err, "field", "backupPaths"))
+ }
+ composeFile := strings.TrimPrefix(strings.TrimPrefix(current.ComposePath, current.BackupDirectory), "/")
+ if current.ProjectID != nil {
+ if project, found, lookupErr := s.lookupProjectByIDInternal(ctx, *current.ProjectID); lookupErr == nil && found {
+ if resolved, _, resolveErr := s.backupComposeFilesInternal(ctx, project); resolveErr == nil {
+ composeFile = resolved
+ }
+ }
+ }
+ if !backupSelectionCoversInternal(paths, composeFile) {
+ return common.Classify(common.ErrValidation, errors.WithDetails(errors.New("the backup selection must include the compose file "+composeFile), "field", "backupPaths"))
+ }
+ updates["backup_paths"] = database.StringSlice(paths)
+ }
+ if req.BackupOnSave != nil {
+ updates["backup_on_save"] = *req.BackupOnSave
+ }
+ return nil
+}
+
+// performBackupInternal snapshots the project, reconciles it against the
+// branch, and pushes a commit when the snapshot differs. adopt replaces
+// whatever the remote holds in the backup directory.
+func (s *GitOpsSyncService) performBackupInternal(ctx context.Context, sync *projectpkg.GitOpsSync, actor common.User, result *gitops.SyncResult, adopt bool) (*gitops.SyncResult, error) {
+ if sync.Repository == nil {
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureRepository, "Repository not found", errors.New("repository not found"))
+ }
+ if sync.ProjectID == nil || strings.TrimSpace(*sync.ProjectID) == "" {
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project not linked", errors.New("backup sync has no project"))
+ }
+ project, found, err := s.lookupProjectByIDInternal(ctx, *sync.ProjectID)
+ if err != nil {
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Failed to load project", err)
+ }
+ if !found {
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project not found", errors.Errorf("project %s no longer exists", *sync.ProjectID))
+ }
+ if err := s.projectService.EnsureProjectPathUnderRoot(ctx, project, true); err != nil {
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project directory is unavailable", err)
+ }
+ authConfig, err := s.repoService.GetAuthConfig(ctx, sync.Repository)
+ if err != nil {
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureAuth, "Failed to get authentication config", err)
+ }
+
+ startedAt := time.Now()
+ s.updateBackupRunningInternal(ctx, sync.ID)
+
+ lock := s.backups.branchLock(sync.RepositoryID, sync.Branch)
+ lock.Lock()
+ defer lock.Unlock()
+
+ snapshot, err := s.buildBackupSnapshotInternal(ctx, sync, project)
+ if err != nil {
+ return result, s.failBackupInternal(ctx, sync, result, actor, backupSnapshotFailureReasonInternal(err), "Failed to snapshot project files", err)
+ }
+ baseline := parseBackupSnapshotInternal(sync.LastBackupSnapshot)
+
+ for attempt := 1; attempt <= backupPushAttempts; attempt++ {
+ outcome, retry, err := s.runBackupAttemptInternal(ctx, sync, project, actor, result, authConfig, snapshot, baseline, adopt, startedAt)
+ if retry {
+ slog.InfoContext(ctx, "git backup push rejected; retrying with a fresh checkout", "syncId", sync.ID, "attempt", attempt, "error", err)
+ continue
+ }
+ return outcome, err
+ }
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailurePushRejected, "Push rejected by remote", errors.Errorf("another writer kept updating branch %s; giving up after %d attempts", sync.Branch, backupPushAttempts))
+}
+
+func (s *GitOpsSyncService) runBackupAttemptInternal(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project, actor common.User, result *gitops.SyncResult, authConfig git.AuthConfig, snapshot *backupSnapshotInternal, baseline map[string]string, adopt bool, startedAt time.Time) (*gitops.SyncResult, bool, error) {
+ checkout, err := s.repoService.CheckoutForWrite(ctx, sync.Repository.URL, sync.Branch, authConfig)
+ if err != nil {
+ return result, false, s.failBackupInternal(ctx, sync, result, actor, backupCheckoutFailureReasonInternal(err), "Failed to prepare repository checkout", err)
+ }
+ defer func() {
+ if cleanupErr := s.repoService.Cleanup(checkout.RepoPath); cleanupErr != nil {
+ slog.WarnContext(ctx, "Failed to cleanup repository", "path", checkout.RepoPath, "error", cleanupErr)
+ }
+ }()
+
+ remote, err := readRemoteBackupStateInternal(ctx, checkout.RepoPath, sync.BackupDirectory)
+ if err != nil {
+ return result, false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureRepository, "Failed to read remote backup", err)
+ }
+ analysis := analyzeBackupInternal(snapshot, remote, baseline, adopt)
+
+ switch analysis.state {
+ case backupPreviewClean:
+ s.recordBackupSuccessInternal(ctx, sync, snapshot, checkout.HeadCommit, false, startedAt)
+ result.Success = true
+ result.Message = "Repository already contains the current files for project " + project.Name
+ return result, false, nil
+ case backupPreviewConflict:
+ return result, false, s.failBackupNeedsAttentionInternal(ctx, sync, result, actor, gitops.BackupFailureConflict, "Backup files changed in the repository", errors.Errorf("%d backup file(s) changed in the repository since the last backup: %s", len(analysis.conflicts), summarizeBackupChangesInternal(analysis.conflicts)))
+ case backupPreviewOccupied:
+ return result, false, s.failBackupNeedsAttentionInternal(ctx, sync, result, actor, gitops.BackupFailureDestinationOccupied, "Backup directory already contains unrelated files", errors.Errorf("directory %s on branch %s already contains files that are not an Arcane backup", sync.BackupDirectory, sync.Branch))
+ }
+
+ now := time.Now()
+ manifest, err := buildBackupManifestInternal(sync, project.Name, snapshot, now)
+ if err != nil {
+ return result, false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureSnapshot, "Failed to build backup manifest", err)
+ }
+ request := git.CommitRequest{
+ Message: backupCommitMessageInternal(project.Name, actor, analysis.changes),
+ AuthorName: backupAuthorName,
+ AuthorEmail: backupAuthorEmail,
+ }
+ for _, file := range snapshot.files {
+ request.Files = append(request.Files, git.CommitFile{Path: path.Join(sync.BackupDirectory, file.Path), Content: file.Content, Executable: file.Executable})
+ }
+ request.Files = append(request.Files, git.CommitFile{Path: path.Join(sync.BackupDirectory, gitops.BackupManifestFileName), Content: manifest})
+ for _, removed := range analysis.remove {
+ request.Remove = append(request.Remove, path.Join(sync.BackupDirectory, removed))
+ }
+
+ commit, committed, err := s.repoService.CommitAndPush(ctx, checkout, request, authConfig)
+ if err != nil {
+ if errors.Is(err, git.ErrPushRejected) {
+ return result, true, err
+ }
+ return result, false, s.failBackupInternal(ctx, sync, result, actor, backupCheckoutFailureReasonInternal(err), "Failed to push backup", err)
+ }
+
+ s.recordBackupSuccessInternal(ctx, sync, snapshot, commit, committed, startedAt)
+ result.Success = true
+ result.Message = fmt.Sprintf("Backed up %d file(s) for project %s to %s", len(snapshot.files), project.Name, sync.BackupDirectory)
+ if _, eventErr := s.eventService.CreateEvent(ctx, event.CreateEventRequest{
+ Type: event.EventTypeGitSyncRun,
+ Severity: event.EventSeveritySuccess,
+ Title: "Git backup completed",
+ Description: fmt.Sprintf("Backed up project '%s' to '%s' (%s)", project.Name, sync.BackupDirectory, shortCommitInternal(commit)),
+ ResourceType: new("git_sync"),
+ ResourceID: new(sync.ID),
+ ResourceName: new(sync.Name),
+ UserID: new(actor.ID),
+ Username: new(actor.Username),
+ EnvironmentID: new(sync.EnvironmentID),
+ }); eventErr != nil {
+ slog.WarnContext(ctx, "Failed to record git backup audit event", "syncId", sync.ID, "commit", commit, "error", eventErr)
+ }
+ slog.InfoContext(ctx, "Git backup completed", "syncId", sync.ID, "project", project.Name, "commit", commit, "committed", committed)
+ return result, false, nil
+}
+
+func shortCommitInternal(commit string) string {
+ if len(commit) > 12 {
+ return commit[:12]
+ }
+ return commit
+}
+
+func summarizeBackupChangesInternal(changes []gitops.BackupFileChange) string {
+ names := make([]string, 0, min(len(changes), backupCommitFileLines))
+ for i, change := range changes {
+ if i >= backupCommitFileLines {
+ names = append(names, "...")
+ break
+ }
+ names = append(names, change.Path)
+ }
+ return strings.Join(names, ", ")
+}
+
+func backupCommitMessageInternal(projectName string, actor common.User, changes []gitops.BackupFileChange) string {
+ var builder strings.Builder
+ fmt.Fprintf(&builder, "Back up %s from Arcane", projectName)
+ if actor.Username != "" && actor.ID != common.SystemUser.ID {
+ fmt.Fprintf(&builder, " (%s)", actor.Username)
+ }
+ builder.WriteString("\n")
+ for i, change := range changes {
+ if i >= backupCommitFileLines {
+ fmt.Fprintf(&builder, "\n... and %d more", len(changes)-backupCommitFileLines)
+ break
+ }
+ fmt.Fprintf(&builder, "\n%s %s", change.Change, change.Path)
+ }
+ return builder.String()
+}
+
+func backupSnapshotFailureReasonInternal(err error) string {
+ switch {
+ case errors.Is(err, errBackupUnreadableInternal):
+ return gitops.BackupFailureUnreadableFiles
+ case errors.Is(err, errBackupLimitsInternal):
+ return gitops.BackupFailureLimits
+ default:
+ return gitops.BackupFailureSnapshot
+ }
+}
+
+func backupCheckoutFailureReasonInternal(err error) string {
+ message := strings.ToLower(err.Error())
+ if strings.Contains(message, "authentication") || strings.Contains(message, "authorization") || strings.Contains(message, "ssh") || strings.Contains(message, "credential") {
+ return gitops.BackupFailureAuth
+ }
+ return gitops.BackupFailureRepository
+}
+
+func (s *GitOpsSyncService) updateBackupRunningInternal(ctx context.Context, id string) {
+ if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", id).Update("last_sync_status", backupStatusRunning).Error; err != nil {
+ slog.ErrorContext(ctx, "Failed to mark git backup running", "error", err, "syncId", id)
+ }
+}
+
+func (s *GitOpsSyncService) recordBackupSuccessInternal(ctx context.Context, sync *projectpkg.GitOpsSync, snapshot *backupSnapshotInternal, commit string, committed bool, startedAt time.Time) {
+ now := time.Now()
+ paths := make([]string, 0, len(snapshot.files))
+ for _, file := range snapshot.files {
+ paths = append(paths, file.Path)
+ }
+ updates := map[string]any{
+ "last_sync_at": now,
+ "last_sync_status": "success",
+ "last_sync_error": nil,
+ "last_backup_snapshot": marshalBackupSnapshotInternal(snapshot.hashes),
+ "synced_files": marshalSyncedFiles(paths),
+ "backup_conflict": false,
+ "backup_failure_reason": nil,
+ }
+ if commit != "" {
+ updates["last_sync_commit"] = commit
+ }
+ if committed || sync.LastBackupAt == nil {
+ updates["last_backup_at"] = now
+ }
+ if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", sync.ID).Updates(updates).Error; err != nil {
+ slog.ErrorContext(ctx, "Failed to record git backup success", "error", err, "syncId", sync.ID)
+ }
+ s.clearBackupPendingInternal(ctx, sync.ID, startedAt)
+}
+
+// clearBackupPendingInternal clears the pending flag unless a save arrived
+// after the run started, in which case the next run picks it up.
+func (s *GitOpsSyncService) clearBackupPendingInternal(ctx context.Context, id string, startedAt time.Time) {
+ if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).
+ Where("id = ? AND (backup_pending_since IS NULL OR backup_pending_since <= ?)", id, startedAt).
+ Updates(map[string]any{"backup_pending": false, "backup_pending_since": nil}).Error; err != nil {
+ slog.ErrorContext(ctx, "Failed to clear git backup pending flag", "error", err, "syncId", id)
+ }
+}
+
+func (s *GitOpsSyncService) failBackupInternal(ctx context.Context, sync *projectpkg.GitOpsSync, result *gitops.SyncResult, actor common.User, reason, message string, failure error) error {
+ errMsg := failure.Error()
+ result.Message = message
+ result.Error = new(errMsg)
+ s.updateBackupFailureInternal(ctx, sync.ID, "failed", false, reason, errMsg)
+ s.logSyncError(ctx, sync, actor, errMsg)
+ return errors.WithMessage(failure, message)
+}
+
+func (s *GitOpsSyncService) failBackupNeedsAttentionInternal(ctx context.Context, sync *projectpkg.GitOpsSync, result *gitops.SyncResult, actor common.User, reason, message string, failure error) error {
+ errMsg := failure.Error()
+ result.Message = message
+ result.Error = new(errMsg)
+ s.updateBackupFailureInternal(ctx, sync.ID, backupStatusConflict, true, reason, errMsg)
+ s.logSyncError(ctx, sync, actor, errMsg)
+ return common.Classify(common.ErrConflict, errors.WithMessage(failure, message))
+}
+
+func (s *GitOpsSyncService) updateBackupFailureInternal(ctx context.Context, id, status string, conflict bool, reason, errMsg string) {
+ updates := map[string]any{
+ "last_sync_at": time.Now(),
+ "last_sync_status": status,
+ "last_sync_error": errMsg,
+ "backup_conflict": conflict,
+ "backup_failure_reason": reason,
+ }
+ if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", id).Updates(updates).Error; err != nil {
+ slog.ErrorContext(ctx, "Failed to record git backup failure", "error", err, "syncId", id)
+ }
+}
+
+// PreviewBackup reports what the next backup run would commit or why it needs attention.
+func (s *GitOpsSyncService) PreviewBackup(ctx context.Context, environmentID, id string) (*gitops.BackupPreview, error) {
+ previewCtx, cancel := context.WithTimeout(ctx, defaultGitSyncTimeout)
+ defer cancel()
+
+ syncRecord, err := s.getBackupSyncInternal(previewCtx, environmentID, id)
+ if err != nil {
+ return nil, err
+ }
+ if syncRecord.ProjectID == nil {
+ return nil, common.Classify(common.ErrNotFound, errors.New("backup sync has no project"))
+ }
+ project, found, err := s.lookupProjectByIDInternal(previewCtx, *syncRecord.ProjectID)
+ if err != nil {
+ return nil, err
+ }
+ if !found {
+ return nil, common.ErrProjectNotFound
+ }
+ if err := s.projectService.EnsureProjectPathUnderRoot(previewCtx, project, false); err != nil {
+ return nil, err
+ }
+ snapshot, err := s.buildBackupSnapshotInternal(previewCtx, syncRecord, project)
+ if err != nil {
+ return nil, common.Classify(common.ErrBadRequest, err)
+ }
+ authConfig, err := s.repoService.GetAuthConfig(previewCtx, syncRecord.Repository)
+ if err != nil {
+ return nil, err
+ }
+ checkout, err := s.repoService.CheckoutForWrite(previewCtx, syncRecord.Repository.URL, syncRecord.Branch, authConfig)
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to clone repository")
+ }
+ defer func() {
+ if cleanupErr := s.repoService.Cleanup(checkout.RepoPath); cleanupErr != nil {
+ slog.WarnContext(previewCtx, "Failed to cleanup repository", "path", checkout.RepoPath, "error", cleanupErr)
+ }
+ }()
+ remote, err := readRemoteBackupStateInternal(previewCtx, checkout.RepoPath, syncRecord.BackupDirectory)
+ if err != nil {
+ return nil, err
+ }
+ analysis := analyzeBackupInternal(snapshot, remote, parseBackupSnapshotInternal(syncRecord.LastBackupSnapshot), false)
+ files := make([]string, 0, len(snapshot.files))
+ for _, file := range snapshot.files {
+ files = append(files, file.Path)
+ }
+ return &gitops.BackupPreview{
+ State: analysis.state,
+ RemoteCommit: checkout.HeadCommit,
+ Changes: analysis.changes,
+ Conflicts: analysis.conflicts,
+ Files: files,
+ }, nil
+}
+
+// ResolveBackupConflict applies the chosen strategy to a backup that needs attention.
+func (s *GitOpsSyncService) ResolveBackupConflict(ctx context.Context, environmentID, id string, req gitops.ResolveBackupConflictRequest, actor common.User) (*gitops.SyncResult, error) {
+ if req.Strategy != gitops.BackupConflictUseArcane {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(errors.Errorf("unsupported strategy %q", req.Strategy), "field", "strategy"))
+ }
+ syncRecord, err := s.getBackupSyncInternal(ctx, environmentID, id)
+ if err != nil {
+ return nil, err
+ }
+ return s.performSyncAdmittedInternal(ctx, syncRecord.EnvironmentID, syncRecord.ID, actor, true)
+}
+
+// SubscribeProjectFileChanges marks backups pending when project files are
+// saved and, when automatic backup is on, runs them after a short debounce.
+func (s *GitOpsSyncService) SubscribeProjectFileChanges(ctx context.Context) {
+ if s.projectService == nil {
+ return
+ }
+ runCtx := s.jobs.Context(ctx)
+ s.projectService.FilesChanged().Subscribe(func(projectID string) {
+ s.onProjectFilesChangedInternal(runCtx, projectID)
+ })
+}
+
+func (s *GitOpsSyncService) onProjectFilesChangedInternal(ctx context.Context, projectID string) {
+ var syncs []projectpkg.GitOpsSync
+ if err := s.db.WithContext(ctx).
+ Where("mode = ? AND project_id = ?", gitops.SyncModeBackup, projectID).
+ Find(&syncs).Error; err != nil {
+ slog.ErrorContext(ctx, "Failed to load git backups for changed project", "projectId", projectID, "error", err)
+ return
+ }
+ now := time.Now()
+ for i := range syncs {
+ syncRecord := syncs[i]
+ if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", syncRecord.ID).
+ Updates(map[string]any{"backup_pending": true, "backup_pending_since": now}).Error; err != nil {
+ slog.ErrorContext(ctx, "Failed to mark git backup pending", "syncId", syncRecord.ID, "error", err)
+ continue
+ }
+ if !syncRecord.AutoSync || !syncRecord.BackupOnSave {
+ continue
+ }
+ syncID, environmentID := syncRecord.ID, syncRecord.EnvironmentID
+ s.backups.schedule(syncID, func() { s.triggerBackupInternal(ctx, syncID, environmentID) })
+ }
+}
+
+func (s *GitOpsSyncService) triggerBackupInternal(ctx context.Context, syncID, environmentID string) {
+ if s.jobs.Enabled() {
+ if _, err := s.jobs.Scheduler().Submit(ctx, schedulertypes.Request{JobID: s.jobs.JobName(syncID), EnvironmentID: "0", Trigger: "save"}); err != nil {
+ slog.ErrorContext(ctx, "git backup admission after save failed", "syncId", syncID, "error", err)
+ }
+ return
+ }
+ go func() {
+ if _, err := s.PerformSync(ctx, environmentID, syncID, common.SystemUser); err != nil {
+ slog.ErrorContext(ctx, "git backup after save failed", "syncId", syncID, "error", err)
+ }
+ }()
+}
+
+// ReconcileInterruptedBackupsOnStartup turns backups left in the running state
+// by a restart into pending failures so they are retried or surfaced.
+func (s *GitOpsSyncService) ReconcileInterruptedBackupsOnStartup(ctx context.Context) error {
+ err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).
+ Where("mode = ? AND last_sync_status = ?", gitops.SyncModeBackup, backupStatusRunning).
+ Updates(map[string]any{
+ "last_sync_status": "failed",
+ "last_sync_error": "backup was interrupted by a restart",
+ "backup_failure_reason": gitops.BackupFailureRepository,
+ "backup_pending": true,
+ "backup_pending_since": time.Now(),
+ }).Error
+ if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+ return errors.WrapIf(err, "failed to reconcile interrupted git backups")
+ }
+ return nil
+}
diff --git a/backend/internal/gitops/backup_history.go b/backend/internal/gitops/backup_history.go
new file mode 100644
index 000000000..e620fdf1e
--- /dev/null
+++ b/backend/internal/gitops/backup_history.go
@@ -0,0 +1,130 @@
+package gitops
+
+import (
+ "context"
+ "log/slog"
+ "strings"
+
+ "emperror.dev/errors"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/common"
+ projectpkg "github.com/getarcaneapp/arcane/backend/v2/internal/project"
+ git "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
+ "github.com/getarcaneapp/arcane/types/v2/gitops"
+)
+
+const defaultBackupHistoryLimit = 20
+
+func (s *GitOpsSyncService) getBackupSyncInternal(ctx context.Context, environmentID, id string) (*projectpkg.GitOpsSync, error) {
+ syncRecord, err := s.GetSyncByID(ctx, environmentID, id)
+ if err != nil {
+ return nil, err
+ }
+ if !syncRecord.IsBackup() {
+ return nil, common.Classify(common.ErrBadRequest, errors.New("sync does not back up to Git"))
+ }
+ if syncRecord.Repository == nil {
+ return nil, common.Classify(common.ErrNotFound, errors.New("repository not found"))
+ }
+ return syncRecord, nil
+}
+
+// cloneBackupBranchInternal clones the backup branch for read-only inspection.
+// A missing branch or empty repository yields an empty path.
+func (s *GitOpsSyncService) cloneBackupBranchInternal(ctx context.Context, syncRecord *projectpkg.GitOpsSync) (string, func(), error) {
+ authConfig, err := s.repoService.GetAuthConfig(ctx, syncRecord.Repository)
+ if err != nil {
+ return "", func() {}, err
+ }
+ checkout, err := s.repoService.CheckoutForWrite(ctx, syncRecord.Repository.URL, syncRecord.Branch, authConfig)
+ if err != nil {
+ return "", func() {}, errors.WrapIf(err, "failed to clone repository")
+ }
+ cleanup := func() {
+ if cleanupErr := s.repoService.Cleanup(checkout.RepoPath); cleanupErr != nil {
+ slog.WarnContext(ctx, "Failed to cleanup repository", "path", checkout.RepoPath, "error", cleanupErr)
+ }
+ }
+ if !checkout.BranchExists {
+ return "", cleanup, nil
+ }
+ return checkout.RepoPath, cleanup, nil
+}
+
+func historyEntryToAPIInternal(entry git.HistoryEntry) gitops.BackupHistoryEntry {
+ return gitops.BackupHistoryEntry{
+ Commit: entry.Hash,
+ Author: entry.Author,
+ Message: entry.Message,
+ Date: entry.Date,
+ Files: entry.Files,
+ }
+}
+
+// GetBackupHistory lists revisions that touched the backup directory.
+func (s *GitOpsSyncService) GetBackupHistory(ctx context.Context, environmentID, id string, limit int) (*gitops.BackupHistoryResponse, error) {
+ historyCtx, cancel := context.WithTimeout(ctx, defaultGitSyncTimeout)
+ defer cancel()
+
+ syncRecord, err := s.getBackupSyncInternal(historyCtx, environmentID, id)
+ if err != nil {
+ return nil, err
+ }
+ repoPath, cleanup, err := s.cloneBackupBranchInternal(historyCtx, syncRecord)
+ defer cleanup()
+ if err != nil {
+ return nil, err
+ }
+ response := &gitops.BackupHistoryResponse{Entries: []gitops.BackupHistoryEntry{}}
+ if repoPath == "" {
+ return response, nil
+ }
+ if limit <= 0 {
+ limit = defaultBackupHistoryLimit
+ }
+ entries, err := s.repoService.DirectoryHistory(historyCtx, repoPath, syncRecord.BackupDirectory, limit)
+ if err != nil {
+ return nil, err
+ }
+ for _, entry := range entries {
+ response.Entries = append(response.Entries, historyEntryToAPIInternal(entry))
+ }
+ return response, nil
+}
+
+// GetBackupRevision returns one revision with per-file diffs inside the backup directory.
+func (s *GitOpsSyncService) GetBackupRevision(ctx context.Context, environmentID, id, commit string) (*gitops.BackupRevision, error) {
+ commit = strings.TrimSpace(commit)
+ if len(commit) < 7 || len(commit) > 64 {
+ return nil, common.Classify(common.ErrBadRequest, errors.New("invalid commit hash"))
+ }
+ for _, r := range commit {
+ if (r < '0' || r > '9') && (r < 'a' || r > 'f') && (r < 'A' || r > 'F') {
+ return nil, common.Classify(common.ErrBadRequest, errors.New("invalid commit hash"))
+ }
+ }
+
+ revisionCtx, cancel := context.WithTimeout(ctx, defaultGitSyncTimeout)
+ defer cancel()
+
+ syncRecord, err := s.getBackupSyncInternal(revisionCtx, environmentID, id)
+ if err != nil {
+ return nil, err
+ }
+ repoPath, cleanup, err := s.cloneBackupBranchInternal(revisionCtx, syncRecord)
+ defer cleanup()
+ if err != nil {
+ return nil, err
+ }
+ if repoPath == "" {
+ return nil, common.Classify(common.ErrNotFound, errors.New("revision not found"))
+ }
+ entry, diffs, err := s.repoService.CommitDiff(revisionCtx, repoPath, commit, syncRecord.BackupDirectory)
+ if err != nil {
+ return nil, common.Classify(common.ErrNotFound, err)
+ }
+ revision := &gitops.BackupRevision{Entry: historyEntryToAPIInternal(entry), Diffs: []gitops.BackupFileDiff{}}
+ for _, diff := range diffs {
+ revision.Diffs = append(revision.Diffs, gitops.BackupFileDiff{Path: diff.Path, Patch: diff.Patch})
+ }
+ return revision, nil
+}
diff --git a/backend/internal/gitops/backup_snapshot.go b/backend/internal/gitops/backup_snapshot.go
new file mode 100644
index 000000000..fa2244e54
--- /dev/null
+++ b/backend/internal/gitops/backup_snapshot.go
@@ -0,0 +1,472 @@
+package gitops
+
+import (
+ "context"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json/jsontext"
+ "encoding/json/v2"
+ "io/fs"
+ "os"
+ "path"
+ "path/filepath"
+ "slices"
+ "sort"
+ "strings"
+ "time"
+
+ "emperror.dev/errors"
+ projectpkg "github.com/getarcaneapp/arcane/backend/v2/internal/project"
+ git "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
+ "github.com/getarcaneapp/arcane/types/v2/gitops"
+ "go.getarcane.app/acfs"
+ acfstypes "go.getarcane.app/acfs/types"
+)
+
+const backupSnapshotAttemptsInternal = 3
+
+var (
+ errBackupUnreadableInternal = errors.Sentinel("backup selection is unreadable")
+ errBackupLimitsInternal = errors.Sentinel("backup selection exceeds sync limits")
+ errBackupSnapshotInternal = errors.Sentinel("backup selection is invalid")
+)
+
+// backupSnapshotInternal is the set of project files a backup run commits.
+type backupSnapshotInternal struct {
+ files []git.CommitFile
+ hashes map[string]string
+ composeFiles []string
+}
+
+// remoteBackupStateInternal describes the backup directory as it exists on the branch.
+type remoteBackupStateInternal struct {
+ manifest *gitops.BackupManifest
+ hashes map[string]string
+ occupied bool
+}
+
+// backupAnalysisInternal is the decision for one backup run.
+type backupAnalysisInternal struct {
+ state string
+ changes []gitops.BackupFileChange
+ conflicts []gitops.BackupFileChange
+ remove []string
+}
+
+const (
+ backupPreviewClean = "clean"
+ backupPreviewChanges = "changes"
+ backupPreviewConflict = "conflict"
+ backupPreviewOccupied = "destination_occupied"
+ backupChangeAdded = "added"
+ backupChangeModified = "modified"
+ backupChangeRemoved = "removed"
+ backupManifestVersion = 1
+ backupSnapshotMaxRetry = backupSnapshotAttemptsInternal
+)
+
+func hashBackupContentInternal(content []byte) string {
+ sum := sha256.Sum256(content)
+ return hex.EncodeToString(sum[:])
+}
+
+// isBackupEnvFileInternal reports whether a base name looks like an environment
+// file; these are excluded from directory expansion and only backed up when
+// listed explicitly.
+func isBackupEnvFileInternal(name string) bool {
+ return name == projects.EffectiveEnvFileName || strings.HasPrefix(name, ".env.") || strings.HasSuffix(name, ".env")
+}
+
+// isBackupReservedFileInternal reports whether a base name is Arcane bookkeeping
+// that never belongs in a backup.
+func isBackupReservedFileInternal(name string) bool {
+ return name == projects.GitSourceEnvFileName || name == projects.GlobalEnvFileName || name == gitops.BackupManifestFileName || name == ".git"
+}
+
+func containsGitSegmentInternal(relative string) bool {
+ for segment := range strings.SplitSeq(relative, "/") {
+ if segment == ".git" {
+ return true
+ }
+ }
+ return false
+}
+
+func normalizeBackupDirectoryInternal(raw string) (string, error) {
+ normalized, err := utils.NormalizeRelativePath(raw)
+ if err != nil {
+ return "", err
+ }
+ if containsGitSegmentInternal(normalized) {
+ return "", errors.New("backup directory must not contain a .git segment")
+ }
+ return normalized, nil
+}
+
+func normalizeBackupPathsInternal(raw []string) ([]string, error) {
+ seen := make(map[string]struct{}, len(raw))
+ normalized := make([]string, 0, len(raw))
+ for _, entry := range raw {
+ cleaned, err := utils.NormalizeRelativePath(entry)
+ if err != nil {
+ return nil, errors.WrapIff(err, "invalid backup path %q", entry)
+ }
+ if isBackupReservedFileInternal(path.Base(cleaned)) || containsGitSegmentInternal(cleaned) {
+ return nil, errors.Errorf("backup path %q is reserved", entry)
+ }
+ if _, ok := seen[cleaned]; ok {
+ continue
+ }
+ seen[cleaned] = struct{}{}
+ normalized = append(normalized, cleaned)
+ }
+ sort.Strings(normalized)
+ return normalized, nil
+}
+
+// backupSelectionCoversInternal reports whether the selection includes file
+// directly or through one of its parent directories.
+func backupSelectionCoversInternal(paths []string, file string) bool {
+ for _, selected := range paths {
+ if selected == file || strings.HasPrefix(file, selected+"/") {
+ return true
+ }
+ }
+ return false
+}
+
+func backupDirectoriesOverlapInternal(a, b string) bool {
+ return a == b || strings.HasPrefix(a, b+"/") || strings.HasPrefix(b, a+"/")
+}
+
+// backupComposeFilesInternal resolves the project's primary compose file and any
+// sibling overrides as project-relative paths.
+func (s *GitOpsSyncService) backupComposeFilesInternal(ctx context.Context, project *projectpkg.Project) (string, []string, error) {
+ composePath, err := s.projectService.ResolveProjectComposeFile(ctx, project)
+ if err != nil {
+ return "", nil, err
+ }
+ relative, err := filepath.Rel(project.Path, composePath)
+ if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
+ return "", nil, errors.Errorf("compose file %s is outside the project directory", composePath)
+ }
+ primary := filepath.ToSlash(relative)
+ composeFiles := []string{primary}
+ composeDir := path.Dir(primary)
+ for _, candidate := range projects.ComposeOverrideFileCandidates() {
+ candidatePath := path.Join(composeDir, candidate)
+ if exists, existsErr := acfs.Exists(ctx, project.Path, "/"+candidatePath); existsErr == nil && exists {
+ composeFiles = append(composeFiles, candidatePath)
+ }
+ }
+ return primary, composeFiles, nil
+}
+
+// buildBackupSnapshotInternal reads the selected project files, verifying that
+// none changed while being read.
+func (s *GitOpsSyncService) buildBackupSnapshotInternal(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project) (*backupSnapshotInternal, error) {
+ paths := []string(sync.BackupPaths)
+ if len(paths) == 0 {
+ return nil, errors.WrapIf(errBackupSnapshotInternal, "no files are selected for backup")
+ }
+ primary, composeFiles, err := s.backupComposeFilesInternal(ctx, project)
+ if err != nil {
+ return nil, errors.WrapIf(errBackupSnapshotInternal, err.Error())
+ }
+ maxFiles, maxTotalSize, _ := s.getEffectiveSyncLimits(ctx, sync)
+
+ var lastErr error
+ for range backupSnapshotMaxRetry {
+ snapshot, err := collectBackupFilesInternal(ctx, project.Path, paths, maxFiles, maxTotalSize)
+ if err != nil {
+ return nil, err
+ }
+ if _, ok := snapshot.hashes[primary]; !ok {
+ return nil, errors.WrapIff(errBackupSnapshotInternal, "compose file %s is not included in the backup selection", primary)
+ }
+ stable, err := backupSnapshotStableInternal(ctx, project.Path, snapshot)
+ if err != nil {
+ return nil, err
+ }
+ if stable {
+ for _, composeFile := range composeFiles {
+ if _, ok := snapshot.hashes[composeFile]; ok {
+ snapshot.composeFiles = append(snapshot.composeFiles, composeFile)
+ }
+ }
+ return snapshot, nil
+ }
+ lastErr = errors.WrapIf(errBackupSnapshotInternal, "project files changed while the backup snapshot was being taken")
+ }
+ return nil, lastErr
+}
+
+// backupCollectorInternal accumulates the selected project files under the sync limits.
+type backupCollectorInternal struct {
+ ctx context.Context
+ projectPath string
+ maxFiles int
+ maxTotalSize int64
+ totalSize int64
+ snapshot *backupSnapshotInternal
+}
+
+func collectBackupFilesInternal(ctx context.Context, projectPath string, paths []string, maxFiles int, maxTotalSize int64) (*backupSnapshotInternal, error) {
+ collector := &backupCollectorInternal{
+ ctx: ctx,
+ projectPath: projectPath,
+ maxFiles: maxFiles,
+ maxTotalSize: maxTotalSize,
+ snapshot: &backupSnapshotInternal{hashes: make(map[string]string)},
+ }
+ for _, selected := range paths {
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ if err := collector.addPathInternal(selected); err != nil {
+ return nil, err
+ }
+ }
+ if len(collector.snapshot.files) == 0 {
+ return nil, errors.WrapIf(errBackupSnapshotInternal, "the backup selection contains no files")
+ }
+ sort.Slice(collector.snapshot.files, func(i, j int) bool { return collector.snapshot.files[i].Path < collector.snapshot.files[j].Path })
+ return collector.snapshot, nil
+}
+
+func (c *backupCollectorInternal) addPathInternal(selected string) error {
+ logical := "/" + selected
+ entry, err := acfs.Stat(c.ctx, c.projectPath, logical, false)
+ if err != nil {
+ if errors.Is(err, fs.ErrNotExist) {
+ return errors.WrapIff(errBackupUnreadableInternal, "selected path %s does not exist", selected)
+ }
+ return errors.WrapIff(errBackupUnreadableInternal, "cannot inspect %s: %v", selected, err)
+ }
+ if entry.IsSymlink {
+ return errors.WrapIff(errBackupSnapshotInternal, "%s is a symbolic link and cannot be backed up", selected)
+ }
+ if !entry.IsDirectory {
+ return c.addFileInternal(entry)
+ }
+ walkErr := acfs.Walk(c.ctx, c.projectPath, logical, c.visitDirectoryEntryInternal)
+ if walkErr == nil {
+ return nil
+ }
+ if errors.Is(walkErr, errBackupUnreadableInternal) || errors.Is(walkErr, errBackupLimitsInternal) || errors.Is(walkErr, errBackupSnapshotInternal) {
+ return walkErr
+ }
+ return errors.WrapIff(errBackupUnreadableInternal, "cannot walk %s: %v", selected, walkErr)
+}
+
+func (c *backupCollectorInternal) visitDirectoryEntryInternal(child acfstypes.Entry) error {
+ if child.IsDirectory {
+ if child.Name == ".git" || projects.IsInternalScratchDirName(child.Name) {
+ return fs.SkipDir
+ }
+ return nil
+ }
+ if child.IsSymlink || isBackupReservedFileInternal(child.Name) || isBackupEnvFileInternal(child.Name) {
+ return nil
+ }
+ return c.addFileInternal(child)
+}
+
+func (c *backupCollectorInternal) addFileInternal(entry acfstypes.Entry) error {
+ relative := strings.TrimPrefix(entry.Path, "/")
+ if _, seen := c.snapshot.hashes[relative]; seen {
+ return nil
+ }
+ if c.maxFiles > 0 && len(c.snapshot.files) >= c.maxFiles {
+ return errors.WrapIff(errBackupLimitsInternal, "file count limit exceeded (max %d files)", c.maxFiles)
+ }
+ content, err := acfs.ReadFile(c.ctx, c.projectPath, entry.Path)
+ if err != nil {
+ return errors.WrapIff(errBackupUnreadableInternal, "cannot read %s: %v", relative, err)
+ }
+ if isBinaryContentForBackupInternal(content) {
+ return errors.WrapIff(errBackupSnapshotInternal, "%s is not a text file; only text configuration files can be backed up", relative)
+ }
+ c.totalSize += int64(len(content))
+ if c.maxTotalSize > 0 && c.totalSize > c.maxTotalSize {
+ return errors.WrapIff(errBackupLimitsInternal, "total size limit exceeded (max %d bytes)", c.maxTotalSize)
+ }
+ c.snapshot.files = append(c.snapshot.files, git.CommitFile{
+ Path: relative,
+ Content: content,
+ Executable: os.FileMode(entry.UnixMode)&0o111 != 0,
+ })
+ c.snapshot.hashes[relative] = hashBackupContentInternal(content)
+ return nil
+}
+
+func backupSnapshotStableInternal(ctx context.Context, projectPath string, snapshot *backupSnapshotInternal) (bool, error) {
+ for _, file := range snapshot.files {
+ content, err := acfs.ReadFile(ctx, projectPath, "/"+file.Path)
+ if err != nil {
+ return false, errors.WrapIff(errBackupUnreadableInternal, "cannot re-read %s: %v", file.Path, err)
+ }
+ if hashBackupContentInternal(content) != snapshot.hashes[file.Path] {
+ return false, nil
+ }
+ }
+ return true, nil
+}
+
+func isBinaryContentForBackupInternal(content []byte) bool {
+ if len(content) == 0 {
+ return false
+ }
+ return slices.Contains(content[:min(len(content), 8000)], 0)
+}
+
+func buildBackupManifestInternal(sync *projectpkg.GitOpsSync, projectName string, snapshot *backupSnapshotInternal, now time.Time) ([]byte, error) {
+ manifest := gitops.BackupManifest{
+ Version: backupManifestVersion,
+ SyncID: sync.ID,
+ ProjectName: projectName,
+ EnvironmentID: sync.EnvironmentID,
+ GeneratedAt: now.UTC(),
+ ComposeFiles: snapshot.composeFiles,
+ Files: snapshot.hashes,
+ }
+ if manifest.ComposeFiles == nil {
+ manifest.ComposeFiles = []string{}
+ }
+ data, err := json.Marshal(manifest, json.Deterministic(true), jsontext.WithIndent(" "))
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to encode backup manifest")
+ }
+ return append(data, '\n'), nil
+}
+
+// readRemoteBackupStateInternal inspects the backup directory in a checkout.
+func readRemoteBackupStateInternal(ctx context.Context, repoPath, directory string) (*remoteBackupStateInternal, error) {
+ state := &remoteBackupStateInternal{hashes: make(map[string]string)}
+ manifestPath := "/" + path.Join(directory, gitops.BackupManifestFileName)
+ data, err := acfs.ReadFile(ctx, repoPath, manifestPath)
+ if err != nil {
+ if !errors.Is(err, fs.ErrNotExist) {
+ return nil, errors.WrapIf(err, "failed to read remote backup manifest")
+ }
+ entries, listErr := acfs.List(ctx, repoPath, "/"+directory)
+ if listErr != nil {
+ if errors.Is(listErr, fs.ErrNotExist) {
+ return state, nil
+ }
+ return nil, errors.WrapIf(listErr, "failed to inspect remote backup directory")
+ }
+ state.occupied = len(entries) > 0
+ return state, nil
+ }
+
+ var manifest gitops.BackupManifest
+ if err := json.Unmarshal(data, &manifest); err != nil {
+ return nil, errors.WrapIf(err, "remote backup manifest is not valid")
+ }
+ state.manifest = &manifest
+ for file := range manifest.Files {
+ relative, err := utils.NormalizeRelativePath(file)
+ if err != nil {
+ continue
+ }
+ content, readErr := acfs.ReadFile(ctx, repoPath, "/"+path.Join(directory, relative))
+ if readErr != nil {
+ if errors.Is(readErr, fs.ErrNotExist) {
+ continue
+ }
+ return nil, errors.WrapIff(readErr, "failed to read remote backup file %s", relative)
+ }
+ state.hashes[relative] = hashBackupContentInternal(content)
+ }
+ return state, nil
+}
+
+func diffBackupHashesInternal(local, remote map[string]string) []gitops.BackupFileChange {
+ var changes []gitops.BackupFileChange
+ for file, hash := range local {
+ remoteHash, ok := remote[file]
+ switch {
+ case !ok:
+ changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeAdded})
+ case remoteHash != hash:
+ changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeModified})
+ }
+ }
+ for file := range remote {
+ if _, ok := local[file]; !ok {
+ changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeRemoved})
+ }
+ }
+ sort.Slice(changes, func(i, j int) bool { return changes[i].Path < changes[j].Path })
+ if changes == nil {
+ changes = []gitops.BackupFileChange{}
+ }
+ return changes
+}
+
+// analyzeBackupInternal decides whether the run is a no-op, a commit, or needs
+// attention. baseline is the last snapshot Arcane successfully pushed; adopt
+// skips the conflict and occupancy checks.
+func analyzeBackupInternal(snapshot *backupSnapshotInternal, remote *remoteBackupStateInternal, baseline map[string]string, adopt bool) backupAnalysisInternal {
+ analysis := backupAnalysisInternal{
+ changes: diffBackupHashesInternal(snapshot.hashes, remote.hashes),
+ conflicts: []gitops.BackupFileChange{},
+ }
+ for file := range remote.hashes {
+ if _, ok := snapshot.hashes[file]; !ok {
+ analysis.remove = append(analysis.remove, file)
+ }
+ }
+ sort.Strings(analysis.remove)
+
+ if len(analysis.changes) == 0 {
+ analysis.state = backupPreviewClean
+ return analysis
+ }
+ if adopt {
+ analysis.state = backupPreviewChanges
+ return analysis
+ }
+ if remote.manifest == nil {
+ if remote.occupied {
+ analysis.state = backupPreviewOccupied
+ } else {
+ analysis.state = backupPreviewChanges
+ }
+ return analysis
+ }
+ if baseline == nil {
+ analysis.conflicts = diffBackupHashesInternal(remote.hashes, map[string]string{})
+ analysis.state = backupPreviewConflict
+ return analysis
+ }
+ analysis.conflicts = diffBackupHashesInternal(remote.hashes, baseline)
+ if len(analysis.conflicts) > 0 {
+ analysis.state = backupPreviewConflict
+ return analysis
+ }
+ analysis.state = backupPreviewChanges
+ return analysis
+}
+
+func parseBackupSnapshotInternal(raw *string) map[string]string {
+ if raw == nil || *raw == "" {
+ return nil
+ }
+ var hashes map[string]string
+ if err := json.Unmarshal([]byte(*raw), &hashes); err != nil {
+ return nil
+ }
+ return hashes
+}
+
+func marshalBackupSnapshotInternal(hashes map[string]string) *string {
+ data, err := json.Marshal(hashes, json.Deterministic(true))
+ if err != nil {
+ return nil
+ }
+ return new(string(data))
+}
diff --git a/backend/internal/gitops/backup_test.go b/backend/internal/gitops/backup_test.go
new file mode 100644
index 000000000..e14cd6e7f
--- /dev/null
+++ b/backend/internal/gitops/backup_test.go
@@ -0,0 +1,677 @@
+package gitops
+
+import (
+ "context"
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/getarcaneapp/arcane/backend/v2/internal/common"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/config"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/database"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/environment"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/event"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/gitrepo"
+ projectpkg "github.com/getarcaneapp/arcane/backend/v2/internal/project"
+ git "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
+ "github.com/getarcaneapp/arcane/types/v2/gitops"
+ schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"
+ "github.com/go-git/go-billy/v5/osfs"
+ gogit "github.com/go-git/go-git/v5"
+ "github.com/go-git/go-git/v5/plumbing/transport/client"
+ "github.com/go-git/go-git/v5/plumbing/transport/server"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+var installBackupTestTransportOnceInternal sync.Once
+
+// installBackupTestTransportInternal serves bare repositories on disk over the
+// "http" scheme so backups push without a network or the git binary.
+func installBackupTestTransportInternal() {
+ installBackupTestTransportOnceInternal.Do(func() {
+ client.InstallProtocol("http", server.NewClient(server.NewFilesystemLoader(osfs.New("/"))))
+ })
+}
+
+// backupTestEnvInternal is one wired-up backup fixture: a bare remote, a git
+// repository row, a project directory and the sync service under test.
+type backupTestEnvInternal struct {
+ service *GitOpsSyncService
+ db *database.DB
+ scheduler *gitOpsBackupTestSchedulerInternal
+ projectsDir string
+ projectPath string
+ project *projectpkg.Project
+ repoURL string
+ remote *git.Client
+}
+
+// gitOpsBackupTestSchedulerInternal runs a submitted job body inline so the
+// save-debounce path reaches PerformSync the way the real scheduler does.
+type gitOpsBackupTestSchedulerInternal struct {
+ mu sync.Mutex
+ jobs map[string]schedulertypes.Job
+ submitted []schedulertypes.Request
+}
+
+func (s *gitOpsBackupTestSchedulerInternal) AddJob(_ context.Context, job schedulertypes.Job) error {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ if s.jobs == nil {
+ s.jobs = make(map[string]schedulertypes.Job)
+ }
+ s.jobs[job.Name()] = job
+ return nil
+}
+
+func (s *gitOpsBackupTestSchedulerInternal) RemoveJob(_ context.Context, name string) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ delete(s.jobs, name)
+}
+
+func (s *gitOpsBackupTestSchedulerInternal) HasJob(name string) bool {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ _, ok := s.jobs[name]
+ return ok
+}
+
+func (s *gitOpsBackupTestSchedulerInternal) Submit(ctx context.Context, request schedulertypes.Request) (schedulertypes.Run, error) {
+ s.mu.Lock()
+ s.submitted = append(s.submitted, request)
+ job := s.jobs[request.JobID]
+ s.mu.Unlock()
+ if job != nil {
+ _, _ = job.Run(ctx)
+ }
+ return schedulertypes.Run{ID: request.RunID, JobID: request.JobID, EnvironmentID: request.EnvironmentID, Status: schedulertypes.Queued}, nil
+}
+
+func (s *gitOpsBackupTestSchedulerInternal) submitCount() int {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ return len(s.submitted)
+}
+
+func setupGitOpsBackupTestServiceInternal(t *testing.T) *backupTestEnvInternal {
+ t.Helper()
+ installBackupTestTransportInternal()
+
+ ctx := context.Background()
+ db := setupGitOpsProjectTestDBInternal(t)
+ require.NoError(t, db.AutoMigrate(&projectpkg.GitOpsSync{}, &projectpkg.ProjectTag{}, &gitrepo.GitRepository{}, &environment.Environment{}))
+
+ settingsService, err := newGitOpsSettingsServiceForTestInternal(t, ctx, db)
+ require.NoError(t, err)
+
+ projectsDir := t.TempDir()
+ require.NoError(t, settingsService.SetStringSetting(ctx, "projectsDirectory", projectsDir))
+
+ eventService := event.NewEventService(db, config.Load(), nil)
+ projectService := projectpkg.NewProjectService(db, settingsService, eventService, nil, nil, nil, nil, nil, config.Load())
+ repoService := gitrepo.NewGitRepositoryService(db, t.TempDir(), eventService, settingsService)
+
+ service := NewGitOpsSyncService(db, repoService, projectService, nil, eventService, settingsService)
+ scheduler := &gitOpsBackupTestSchedulerInternal{}
+ require.NoError(t, service.SetScheduler(ctx, scheduler, newGitOpsAdmissionGateForTestInternal(t)))
+
+ bare := filepath.Join(t.TempDir(), "backups.git")
+ _, err = gogit.PlainInit(bare, true)
+ require.NoError(t, err)
+ repoURL := "http://localhost" + bare
+
+ require.NoError(t, db.Create(&gitrepo.GitRepository{
+ BaseModel: database.BaseModel{ID: "repo-backup"},
+ Name: "backup-remote",
+ URL: repoURL,
+ AuthType: "none",
+ Enabled: true,
+ }).Error)
+
+ projectPath := filepath.Join(projectsDir, "demo-project")
+ require.NoError(t, os.MkdirAll(projectPath, 0o755))
+ writeBackupProjectFileInternal(t, projectPath, "compose.yaml", "services:\n app:\n image: nginx:1.27-alpine\n")
+
+ project := &projectpkg.Project{
+ BaseModel: database.BaseModel{ID: "proj-backup"},
+ Name: "demo-project",
+ DirName: new("demo-project"),
+ Path: projectPath,
+ Status: projectpkg.ProjectStatusStopped,
+ }
+ require.NoError(t, db.Create(project).Error)
+
+ return &backupTestEnvInternal{
+ service: service,
+ db: db,
+ scheduler: scheduler,
+ projectsDir: projectsDir,
+ projectPath: projectPath,
+ project: project,
+ repoURL: repoURL,
+ remote: git.NewClient(t.TempDir()),
+ }
+}
+
+func writeBackupProjectFileInternal(t *testing.T, root, relative, content string) {
+ t.Helper()
+ target := filepath.Join(root, filepath.FromSlash(relative))
+ require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755))
+ require.NoError(t, os.WriteFile(target, []byte(content), 0o644))
+}
+
+func (e *backupTestEnvInternal) createBackupInternal(t *testing.T, req gitops.CreateSyncRequest) *projectpkg.GitOpsSync {
+ t.Helper()
+ if req.Name == "" {
+ req.Name = "demo-backup"
+ }
+ if req.RepositoryID == "" {
+ req.RepositoryID = "repo-backup"
+ }
+ if req.Branch == "" {
+ req.Branch = "main"
+ }
+ if req.Mode == "" {
+ req.Mode = gitops.SyncModeBackup
+ }
+ if req.ProjectID == "" {
+ req.ProjectID = e.project.ID
+ }
+ if req.BackupDirectory == "" {
+ req.BackupDirectory = "backups/demo"
+ }
+ syncRecord, err := e.service.CreateSync(t.Context(), "0", req, common.SystemUser)
+ require.NoError(t, err)
+ return syncRecord
+}
+
+func (e *backupTestEnvInternal) reloadInternal(t *testing.T, id string) *projectpkg.GitOpsSync {
+ t.Helper()
+ var syncRecord projectpkg.GitOpsSync
+ require.NoError(t, e.db.Where("id = ?", id).First(&syncRecord).Error)
+ return &syncRecord
+}
+
+// checkoutRemoteInternal clones the backup branch so the pushed tree can be inspected.
+func (e *backupTestEnvInternal) checkoutRemoteInternal(t *testing.T, branch string) string {
+ t.Helper()
+ repoPath, err := e.remote.Clone(t.Context(), e.repoURL, branch, git.AuthConfig{AuthType: "none"})
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = e.remote.Cleanup(repoPath) })
+ return repoPath
+}
+
+func (e *backupTestEnvInternal) remoteHeadInternal(t *testing.T, branch string) string {
+ t.Helper()
+ head, exists, err := e.remote.RemoteBranchHead(t.Context(), e.repoURL, branch, git.AuthConfig{AuthType: "none"})
+ require.NoError(t, err)
+ require.True(t, exists)
+ return head
+}
+
+// pushRemoteCommitInternal commits directly to the branch as another writer.
+func (e *backupTestEnvInternal) pushRemoteCommitInternal(t *testing.T, branch, message string, files map[string]string) {
+ t.Helper()
+ auth := git.AuthConfig{AuthType: "none"}
+ checkout, err := e.remote.CheckoutForWrite(t.Context(), e.repoURL, branch, auth)
+ require.NoError(t, err)
+ defer func() { _ = e.remote.Cleanup(checkout.RepoPath) }()
+
+ request := git.CommitRequest{Message: message, AuthorName: "Other", AuthorEmail: "other@localhost"}
+ for path, content := range files {
+ request.Files = append(request.Files, git.CommitFile{Path: path, Content: []byte(content)})
+ }
+ _, _, err = e.remote.CommitAndPush(t.Context(), checkout, request, auth)
+ require.NoError(t, err)
+}
+
+func readBackupManifestInternal(t *testing.T, repoPath, directory string) gitops.BackupManifest {
+ t.Helper()
+ data, err := os.ReadFile(filepath.Join(repoPath, filepath.FromSlash(directory), gitops.BackupManifestFileName))
+ require.NoError(t, err)
+ var manifest gitops.BackupManifest
+ require.NoError(t, json.Unmarshal(data, &manifest))
+ return manifest
+}
+
+func TestGitOpsBackup_FirstRunPushesFilesAndManifest(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ writeBackupProjectFileInternal(t, env.projectPath, "config/app.conf", "key = value\n")
+
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{BackupPaths: []string{"compose.yaml", "config"}})
+
+ require.Equal(t, gitops.SyncModeBackup, syncRecord.Mode)
+ require.Equal(t, "backups/demo", syncRecord.BackupDirectory)
+ require.Equal(t, "backups/demo/compose.yaml", syncRecord.ComposePath)
+
+ stored := env.reloadInternal(t, syncRecord.ID)
+ require.NotNil(t, stored.LastSyncStatus)
+ assert.Equal(t, "success", *stored.LastSyncStatus)
+ assert.NotNil(t, stored.LastBackupAt)
+ assert.Equal(t, gitops.BackupStateBackedUp, stored.BackupState())
+ assert.False(t, stored.BackupConflict)
+ assert.Nil(t, stored.BackupFailureReason)
+ require.NotNil(t, stored.LastSyncCommit)
+ assert.Equal(t, env.remoteHeadInternal(t, "main"), *stored.LastSyncCommit)
+ require.NotNil(t, stored.LastBackupSnapshot)
+
+ repoPath := env.checkoutRemoteInternal(t, "main")
+ composeBytes, err := os.ReadFile(filepath.Join(repoPath, "backups", "demo", "compose.yaml"))
+ require.NoError(t, err)
+ assert.Contains(t, string(composeBytes), "nginx:1.27-alpine")
+ assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", "config", "app.conf"))
+
+ manifest := readBackupManifestInternal(t, repoPath, "backups/demo")
+ assert.Equal(t, syncRecord.ID, manifest.SyncID)
+ assert.Equal(t, "demo-project", manifest.ProjectName)
+ assert.Contains(t, manifest.ComposeFiles, "compose.yaml")
+ assert.Len(t, manifest.Files, 2)
+ assert.Equal(t, hashBackupContentInternal(composeBytes), manifest.Files["compose.yaml"])
+}
+
+func TestGitOpsBackup_UnchangedContentMakesNoNewCommit(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+ firstHead := env.remoteHeadInternal(t, "main")
+
+ result, err := env.service.PerformSync(t.Context(), "0", syncRecord.ID, common.SystemUser)
+ require.NoError(t, err)
+ assert.True(t, result.Success)
+ assert.Contains(t, result.Message, "already contains")
+ assert.Equal(t, firstHead, env.remoteHeadInternal(t, "main"))
+
+ stored := env.reloadInternal(t, syncRecord.ID)
+ require.NotNil(t, stored.LastSyncStatus)
+ assert.Equal(t, "success", *stored.LastSyncStatus)
+ assert.Equal(t, gitops.BackupStateBackedUp, stored.BackupState())
+}
+
+func TestGitOpsBackup_AddsAndRemovesFilesAndKeepsUnrelatedRemoteFiles(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ writeBackupProjectFileInternal(t, env.projectPath, "config/app.conf", "key = value\n")
+ writeBackupProjectFileInternal(t, env.projectPath, "config/extra.conf", "extra = 1\n")
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{BackupPaths: []string{"compose.yaml", "config"}})
+
+ env.pushRemoteCommitInternal(t, "main", "unrelated docs", map[string]string{"docs/readme.md": "docs\n"})
+
+ require.NoError(t, os.Remove(filepath.Join(env.projectPath, "config", "extra.conf")))
+ writeBackupProjectFileInternal(t, env.projectPath, "scripts/run.sh", "#!/bin/sh\necho hi\n")
+
+ _, err := env.service.UpdateSync(t.Context(), "0", syncRecord.ID, gitops.UpdateSyncRequest{
+ BackupPaths: []string{"compose.yaml", "config", "scripts"},
+ }, common.SystemUser)
+ require.NoError(t, err)
+
+ result, err := env.service.PerformSync(t.Context(), "0", syncRecord.ID, common.SystemUser)
+ require.NoError(t, err)
+ require.True(t, result.Success)
+
+ repoPath := env.checkoutRemoteInternal(t, "main")
+ assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", "scripts", "run.sh"))
+ assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", "config", "app.conf"))
+ assert.FileExists(t, filepath.Join(repoPath, "docs", "readme.md"))
+ assert.NoFileExists(t, filepath.Join(repoPath, "backups", "demo", "config", "extra.conf"))
+
+ manifest := readBackupManifestInternal(t, repoPath, "backups/demo")
+ assert.NotContains(t, manifest.Files, "config/extra.conf")
+ assert.Contains(t, manifest.Files, "scripts/run.sh")
+}
+
+func TestGitOpsBackup_EnvFilesOnlyIncludedWhenListedExplicitly(t *testing.T) {
+ tests := []struct {
+ name string
+ paths []string
+ wantEnvFile bool
+ }{
+ {name: "directory selection skips env files", paths: []string{"compose.yaml", "config"}, wantEnvFile: false},
+ {name: "explicit env file is included", paths: []string{"compose.yaml", "config", ".env"}, wantEnvFile: true},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ writeBackupProjectFileInternal(t, env.projectPath, ".env", "TOKEN=secret\n")
+ writeBackupProjectFileInternal(t, env.projectPath, "config/app.conf", "key = value\n")
+ writeBackupProjectFileInternal(t, env.projectPath, "config/.env", "TOKEN=nested\n")
+ writeBackupProjectFileInternal(t, env.projectPath, "config/staging.env", "TOKEN=staging\n")
+
+ env.createBackupInternal(t, gitops.CreateSyncRequest{BackupPaths: test.paths})
+
+ repoPath := env.checkoutRemoteInternal(t, "main")
+ manifest := readBackupManifestInternal(t, repoPath, "backups/demo")
+ assert.Contains(t, manifest.Files, "compose.yaml")
+ assert.Contains(t, manifest.Files, "config/app.conf")
+ assert.NotContains(t, manifest.Files, "config/.env")
+ assert.NotContains(t, manifest.Files, "config/staging.env")
+ if test.wantEnvFile {
+ assert.Contains(t, manifest.Files, ".env")
+ assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", ".env"))
+ } else {
+ assert.NotContains(t, manifest.Files, ".env")
+ assert.NoFileExists(t, filepath.Join(repoPath, "backups", "demo", ".env"))
+ }
+ })
+ }
+}
+
+func TestGitOpsBackup_RemoteEditConflictsThenResolvesWithArcane(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+
+ env.pushRemoteCommitInternal(t, "main", "edited backup outside arcane", map[string]string{
+ "backups/demo/compose.yaml": "services:\n app:\n image: tampered\n",
+ })
+ writeBackupProjectFileInternal(t, env.projectPath, "compose.yaml", "services:\n app:\n image: nginx:1.28-alpine\n")
+
+ _, err := env.service.PerformSync(t.Context(), "0", syncRecord.ID, common.SystemUser)
+ require.Error(t, err)
+ require.ErrorIs(t, err, common.ErrConflict)
+
+ stored := env.reloadInternal(t, syncRecord.ID)
+ assert.True(t, stored.BackupConflict)
+ assert.Equal(t, gitops.BackupStateNeedsAttention, stored.BackupState())
+ require.NotNil(t, stored.BackupFailureReason)
+ assert.Equal(t, gitops.BackupFailureConflict, *stored.BackupFailureReason)
+
+ preview, err := env.service.PreviewBackup(t.Context(), "0", syncRecord.ID)
+ require.NoError(t, err)
+ assert.Equal(t, backupPreviewConflict, preview.State)
+ assert.NotEmpty(t, preview.Conflicts)
+
+ result, err := env.service.ResolveBackupConflict(t.Context(), "0", syncRecord.ID, gitops.ResolveBackupConflictRequest{
+ Strategy: gitops.BackupConflictUseArcane,
+ }, common.SystemUser)
+ require.NoError(t, err)
+ require.True(t, result.Success)
+
+ resolved := env.reloadInternal(t, syncRecord.ID)
+ assert.False(t, resolved.BackupConflict)
+ assert.Nil(t, resolved.BackupFailureReason)
+ assert.Equal(t, gitops.BackupStateBackedUp, resolved.BackupState())
+
+ repoPath := env.checkoutRemoteInternal(t, "main")
+ composeBytes, err := os.ReadFile(filepath.Join(repoPath, "backups", "demo", "compose.yaml"))
+ require.NoError(t, err)
+ assert.Contains(t, string(composeBytes), "nginx:1.28-alpine")
+}
+
+func TestGitOpsBackup_OccupiedDestinationNeedsAttention(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ env.pushRemoteCommitInternal(t, "main", "pre-existing files", map[string]string{
+ "backups/demo/unrelated.txt": "not an arcane backup\n",
+ })
+
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+
+ stored := env.reloadInternal(t, syncRecord.ID)
+ assert.True(t, stored.BackupConflict)
+ assert.Equal(t, gitops.BackupStateNeedsAttention, stored.BackupState())
+ require.NotNil(t, stored.BackupFailureReason)
+ assert.Equal(t, gitops.BackupFailureDestinationOccupied, *stored.BackupFailureReason)
+ assert.Nil(t, stored.LastBackupAt)
+}
+
+func TestGitOpsBackup_AdoptsMatchingRemoteWithoutSnapshot(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+ head := env.remoteHeadInternal(t, "main")
+
+ require.NoError(t, env.db.Model(&projectpkg.GitOpsSync{}).Where("id = ?", syncRecord.ID).
+ Update("last_backup_snapshot", nil).Error)
+
+ result, err := env.service.PerformSync(t.Context(), "0", syncRecord.ID, common.SystemUser)
+ require.NoError(t, err)
+ assert.True(t, result.Success)
+ assert.Equal(t, head, env.remoteHeadInternal(t, "main"))
+
+ stored := env.reloadInternal(t, syncRecord.ID)
+ assert.False(t, stored.BackupConflict)
+ assert.Equal(t, gitops.BackupStateBackedUp, stored.BackupState())
+}
+
+func TestGitOpsBackup_SucceedsOnTopOfAnotherWritersCommit(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+
+ env.pushRemoteCommitInternal(t, "main", "other writer", map[string]string{"other/notes.txt": "notes\n"})
+ writeBackupProjectFileInternal(t, env.projectPath, "compose.yaml", "services:\n app:\n image: nginx:1.29-alpine\n")
+
+ result, err := env.service.PerformSync(t.Context(), "0", syncRecord.ID, common.SystemUser)
+ require.NoError(t, err)
+ require.True(t, result.Success)
+
+ repoPath := env.checkoutRemoteInternal(t, "main")
+ assert.FileExists(t, filepath.Join(repoPath, "other", "notes.txt"))
+ composeBytes, err := os.ReadFile(filepath.Join(repoPath, "backups", "demo", "compose.yaml"))
+ require.NoError(t, err)
+ assert.Contains(t, string(composeBytes), "nginx:1.29-alpine")
+}
+
+func TestGitOpsBackup_CreateValidation(t *testing.T) {
+ syncDirectory := true
+ scriptPath := "deploy.sh"
+
+ tests := []struct {
+ name string
+ mutate func(t *testing.T, env *backupTestEnvInternal)
+ request gitops.CreateSyncRequest
+ wantErr error
+ }{
+ {
+ name: "project deployed from git",
+ mutate: func(t *testing.T, env *backupTestEnvInternal) {
+ t.Helper()
+ require.NoError(t, env.db.Model(&projectpkg.Project{}).Where("id = ?", env.project.ID).
+ Update("gitops_managed_by", "sync-deploy").Error)
+ },
+ wantErr: common.ErrConflict,
+ },
+ {
+ name: "second backup for the same project",
+ mutate: func(t *testing.T, env *backupTestEnvInternal) {
+ t.Helper()
+ env.createBackupInternal(t, gitops.CreateSyncRequest{})
+ },
+ request: gitops.CreateSyncRequest{Name: "second", BackupDirectory: "backups/other"},
+ wantErr: common.ErrConflict,
+ },
+ {
+ name: "overlapping destination on the same branch",
+ mutate: func(t *testing.T, env *backupTestEnvInternal) {
+ t.Helper()
+ other := &projectpkg.Project{
+ BaseModel: database.BaseModel{ID: "proj-other"},
+ Name: "other-project",
+ DirName: new("other-project"),
+ Path: filepath.Join(env.projectsDir, "other-project"),
+ Status: projectpkg.ProjectStatusStopped,
+ }
+ writeBackupProjectFileInternal(t, other.Path, "compose.yaml", "services: {}\n")
+ require.NoError(t, env.db.Create(other).Error)
+ env.createBackupInternal(t, gitops.CreateSyncRequest{})
+ env.project = other
+ },
+ request: gitops.CreateSyncRequest{Name: "nested", BackupDirectory: "backups/demo/nested"},
+ wantErr: common.ErrConflict,
+ },
+ {
+ name: "selection missing the compose file",
+ request: gitops.CreateSyncRequest{BackupPaths: []string{"config"}},
+ wantErr: common.ErrValidation,
+ },
+ {
+ name: "deployment-only directory sync",
+ request: gitops.CreateSyncRequest{SyncDirectory: &syncDirectory},
+ wantErr: common.ErrValidation,
+ },
+ {
+ name: "deployment-only pre-deploy script",
+ request: gitops.CreateSyncRequest{PreDeployScriptPath: &scriptPath},
+ wantErr: common.ErrValidation,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ writeBackupProjectFileInternal(t, env.projectPath, "config/app.conf", "key = value\n")
+ if test.mutate != nil {
+ test.mutate(t, env)
+ }
+ request := test.request
+ request.Mode = gitops.SyncModeBackup
+ if request.Name == "" {
+ request.Name = "demo-backup"
+ }
+ request.RepositoryID = "repo-backup"
+ request.Branch = "main"
+ request.ProjectID = env.project.ID
+ if request.BackupDirectory == "" {
+ request.BackupDirectory = "backups/demo"
+ }
+
+ _, err := env.service.CreateSync(t.Context(), "0", request, common.SystemUser)
+ require.Error(t, err)
+ assert.ErrorIs(t, err, test.wantErr)
+ })
+ }
+}
+
+func TestGitOpsBackup_CreateWithoutProjectIsRejected(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+
+ _, err := env.service.CreateSync(t.Context(), "0", gitops.CreateSyncRequest{
+ Name: "demo-backup",
+ RepositoryID: "repo-backup",
+ Branch: "main",
+ Mode: gitops.SyncModeBackup,
+ BackupDirectory: "backups/demo",
+ }, common.SystemUser)
+
+ require.Error(t, err)
+ assert.ErrorIs(t, err, common.ErrValidation)
+}
+
+func TestGitOpsBackup_SaveSignalMarksPendingAndRunsAfterDebounce(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+ firstHead := env.remoteHeadInternal(t, "main")
+
+ env.service.backups.debounce = 50 * time.Millisecond
+ env.service.SubscribeProjectFileChanges(t.Context())
+
+ writeBackupProjectFileInternal(t, env.projectPath, "compose.yaml", "services:\n app:\n image: nginx:1.29-alpine\n")
+ env.service.projectService.FilesChanged().Publish(env.project.ID)
+
+ require.Eventually(t, func() bool {
+ head, exists, err := env.remote.RemoteBranchHead(t.Context(), env.repoURL, "main", git.AuthConfig{AuthType: "none"})
+ return err == nil && exists && head != firstHead
+ }, 5*time.Second, 25*time.Millisecond)
+ require.Positive(t, env.scheduler.submitCount())
+ require.NotEqual(t, firstHead, env.remoteHeadInternal(t, "main"))
+
+ stored := env.reloadInternal(t, syncRecord.ID)
+ require.NotNil(t, stored.LastBackupAt)
+ assert.False(t, stored.BackupPending)
+ assert.Equal(t, gitops.BackupStateBackedUp, stored.BackupState())
+}
+
+func TestGitOpsBackup_SaveSignalOnlyMarksPendingWhenAutoSyncIsOff(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+ head := env.remoteHeadInternal(t, "main")
+
+ autoSync := false
+ _, err := env.service.UpdateSync(t.Context(), "0", syncRecord.ID, gitops.UpdateSyncRequest{AutoSync: &autoSync}, common.SystemUser)
+ require.NoError(t, err)
+
+ env.service.backups.debounce = 50 * time.Millisecond
+ env.service.SubscribeProjectFileChanges(t.Context())
+
+ writeBackupProjectFileInternal(t, env.projectPath, "compose.yaml", "services:\n app:\n image: nginx:1.29-alpine\n")
+ env.service.projectService.FilesChanged().Publish(env.project.ID)
+
+ require.Eventually(t, func() bool {
+ return env.reloadInternal(t, syncRecord.ID).BackupPending
+ }, 5*time.Second, 25*time.Millisecond)
+
+ time.Sleep(300 * time.Millisecond)
+ assert.Equal(t, head, env.remoteHeadInternal(t, "main"))
+
+ stored := env.reloadInternal(t, syncRecord.ID)
+ assert.True(t, stored.BackupPending)
+ assert.Equal(t, gitops.BackupStatePaused, stored.BackupState())
+}
+
+func TestGitOpsBackup_ReconcileInterruptedBackupsOnStartup(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+
+ require.NoError(t, env.db.Model(&projectpkg.GitOpsSync{}).Where("id = ?", syncRecord.ID).
+ Update("last_sync_status", backupStatusRunning).Error)
+
+ require.NoError(t, env.service.ReconcileInterruptedBackupsOnStartup(t.Context()))
+
+ stored := env.reloadInternal(t, syncRecord.ID)
+ require.NotNil(t, stored.LastSyncStatus)
+ assert.Equal(t, "failed", *stored.LastSyncStatus)
+ assert.True(t, stored.BackupPending)
+ assert.NotNil(t, stored.BackupPendingSince)
+ assert.Equal(t, gitops.BackupStateFailed, stored.BackupState())
+}
+
+func TestGitOpsBackup_DeletingProjectRemovesBackupSync(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ syncRecord := env.createBackupInternal(t, gitops.CreateSyncRequest{})
+
+ require.NoError(t, env.service.projectService.DestroyProject(t.Context(), env.project.ID, true, false, common.SystemUser))
+
+ var remaining int64
+ require.NoError(t, env.db.Model(&projectpkg.GitOpsSync{}).Where("id = ?", syncRecord.ID).Count(&remaining).Error)
+ assert.Zero(t, remaining)
+}
+
+func TestGitOpsDeploy_LinksExistingProject(t *testing.T) {
+ env := setupGitOpsBackupTestServiceInternal(t)
+ ctx := context.Background()
+ remoteCompose := "services:\n app:\n image: nginx:1.28-alpine\n"
+ env.pushRemoteCommitInternal(t, "main", "seed", map[string]string{"apps/demo/compose.yaml": remoteCompose})
+ writeBackupProjectFileInternal(t, env.projectPath, ".env", "TOKEN=keep-me\n")
+
+ created, err := env.service.CreateSync(ctx, "0", gitops.CreateSyncRequest{
+ Name: "deploy-existing",
+ RepositoryID: "repo-backup",
+ Branch: "main",
+ ComposePath: "apps/demo/compose.yaml",
+ ProjectID: env.project.ID,
+ }, common.User{ID: "user-1", Username: "tester"})
+ require.NoError(t, err)
+ require.NotNil(t, created.ProjectID)
+ require.Equal(t, env.project.ID, *created.ProjectID)
+ require.Equal(t, env.project.Name, created.ProjectName)
+
+ var project projectpkg.Project
+ require.NoError(t, env.db.Where("id = ?", env.project.ID).First(&project).Error)
+ require.NotNil(t, project.GitOpsManagedBy)
+ require.Equal(t, created.ID, *project.GitOpsManagedBy)
+
+ compose, err := os.ReadFile(filepath.Join(env.projectPath, "compose.yaml"))
+ require.NoError(t, err)
+ require.Equal(t, remoteCompose, string(compose))
+ envFile, err := os.ReadFile(filepath.Join(env.projectPath, ".env"))
+ require.NoError(t, err)
+ require.Contains(t, string(envFile), "TOKEN=keep-me")
+
+ _, err = env.service.CreateSync(ctx, "0", gitops.CreateSyncRequest{
+ Name: "deploy-again",
+ RepositoryID: "repo-backup",
+ Branch: "main",
+ ComposePath: "apps/demo/compose.yaml",
+ ProjectID: env.project.ID,
+ }, common.User{ID: "user-1", Username: "tester"})
+ require.ErrorIs(t, err, common.ErrConflict)
+}
diff --git a/backend/internal/gitops/gitops_sync.go b/backend/internal/gitops/gitops_sync.go
index 8e9f9ce3d..1d8a7839e 100644
--- a/backend/internal/gitops/gitops_sync.go
+++ b/backend/internal/gitops/gitops_sync.go
@@ -51,6 +51,8 @@ type GitOpsSyncService struct {
// jobs carries the scheduler and app lifecycle context, injected
// post-construction via SetScheduler.
jobs *entityjobs.Registry
+
+ backups *backupRuntimeInternal
}
const defaultGitSyncTimeout = 5 * time.Minute
@@ -360,6 +362,7 @@ func NewGitOpsSyncService(db *database.DB, repoService *gitrepo.GitRepositorySer
eventService: eventService,
settingsService: settingsService,
jobs: entityjobs.New(entityjobs.GitOpsSyncJobPrefix, gitOpsSyncAdmissionScopeInternal),
+ backups: newBackupRuntimeInternal(),
}
}
@@ -464,7 +467,7 @@ func (s *GitOpsSyncService) RegisterAutoSyncJobsOnStartup(ctx context.Context) {
for i := range syncs {
syncRecord := syncs[i]
s.registerSyncJobInternal(ctx, syncRecord.ID, syncRecord.EnvironmentID, syncRecord.SyncInterval)
- if isGitOpsSyncOverdueInternal(&syncRecord) {
+ if isGitOpsSyncOverdueInternal(&syncRecord) || (syncRecord.IsBackup() && syncRecord.BackupPending) {
s.kickSyncInternal(ctx, syncRecord.ID)
}
}
@@ -527,12 +530,13 @@ func (s *GitOpsSyncService) GetSyncsPaginated(ctx context.Context, environmentID
if term := strings.TrimSpace(params.Search); term != "" {
searchPattern := "%" + term + "%"
q = q.Where(
- "name LIKE ? OR branch LIKE ? OR compose_path LIKE ?",
- searchPattern, searchPattern, searchPattern,
+ "name LIKE ? OR branch LIKE ? OR compose_path LIKE ? OR backup_directory LIKE ?",
+ searchPattern, searchPattern, searchPattern, searchPattern,
)
}
q = pagination.ApplyBooleanFilter(q, "auto_sync", params.Filters["autoSync"])
+ q = pagination.ApplyFilter(q, "mode", params.Filters["mode"])
q = pagination.ApplyFilter(q, "repository_id", params.Filters["repositoryId"])
q = pagination.ApplyFilter(q, "project_id", params.Filters["projectId"])
@@ -571,10 +575,17 @@ func (s *GitOpsSyncService) getFilteredSyncCounts(query *gorm.DB) (gitops.SyncCo
return gitops.SyncCounts{}, err
}
+ var backupSyncs int64
+ if err := query.Session(&gorm.Session{}).Where("mode = ?", gitops.SyncModeBackup).Count(&backupSyncs).Error; err != nil {
+ return gitops.SyncCounts{}, err
+ }
+
return gitops.SyncCounts{
TotalSyncs: int(totalSyncs),
ActiveSyncs: int(activeSyncs),
SuccessfulSyncs: int(successfulSyncs),
+ DeploySyncs: int(totalSyncs - backupSyncs),
+ BackupSyncs: int(backupSyncs),
}, nil
}
@@ -616,6 +627,17 @@ func (s *GitOpsSyncService) getSyncRecordByIDInternal(ctx context.Context, envir
func (s *GitOpsSyncService) CreateSync(ctx context.Context, environmentID string, req gitops.CreateSyncRequest, actor common.User) (*projectpkg.GitOpsSync, error) {
slog.InfoContext(ctx, "Creating GitOps sync", "environmentID", environmentID, "name", req.Name, "repositoryID", req.RepositoryID)
+ mode, err := normalizeSyncModeInternal(req.Mode)
+ if err != nil {
+ return nil, err
+ }
+ if mode == gitops.SyncModeDeploy && req.HasBackupOptions() {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(errors.New("backup options require mode \"backup\""), "field", "mode"))
+ }
+ if mode == gitops.SyncModeDeploy && strings.TrimSpace(req.ComposePath) == "" {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(errors.New("compose path is required"), "field", "composePath"))
+ }
+
// Validate repository exists
repo, err := s.repoService.GetRepositoryByID(ctx, req.RepositoryID)
if err != nil {
@@ -641,14 +663,18 @@ func (s *GitOpsSyncService) CreateSync(ctx context.Context, environmentID string
TargetType: req.TargetType,
ProjectName: projectName,
ProjectID: nil, // Will be set during first sync
+ Mode: mode,
AutoSync: false,
SyncInterval: 60,
SyncDirectory: false, // Default to single-file sync
MaxSyncFiles: defaultMaxFiles,
MaxSyncTotalSize: defaultMaxTotalSize,
MaxSyncBinarySize: defaultMaxBinarySize,
+ BackupOnSave: true,
}
+ linkProjectID := strings.TrimSpace(req.ProjectID)
+
if req.AutoSync != nil {
syncRecord.AutoSync = *req.AutoSync
}
@@ -692,14 +718,19 @@ func (s *GitOpsSyncService) CreateSync(ctx context.Context, environmentID string
}
applyLifecycleFieldsToSyncInternal(&syncRecord, lifecycleCfg)
- // Select("*") forces explicit zero values (e.g. "0 = unlimited" sync limits and
- // unset pre-deploy fields) to persist instead of GORM substituting column defaults.
- if err := s.db.WithContext(ctx).Select("*").Omit("Environment", "Repository", "Project").Create(&syncRecord).Error; err != nil { //nolint:unqueryvet // intentional Select("*"); see comment above
- slog.ErrorContext(ctx, "Failed to create GitOps sync in database", "name", req.Name, "repositoryID", req.RepositoryID, "environmentID", environmentID, "error", err)
- return nil, errors.WrapIf(err, "failed to create sync")
+ adoptedProject, err := s.insertSyncRecordInternal(ctx, &syncRecord, req, mode, linkProjectID)
+ if err != nil {
+ return nil, err
}
slog.InfoContext(ctx, "GitOps sync created successfully", "syncID", syncRecord.ID, "name", syncRecord.Name)
+ if adoptedProject != nil {
+ adoptedProject.GitOpsManagedBy = &syncRecord.ID
+ if err := s.projectService.EnsureGitOpsProjectLinked(ctx, &syncRecord, adoptedProject); err != nil {
+ return nil, errors.WrapIf(err, "failed to link existing project")
+ }
+ }
+
// Log event
_, _ = s.eventService.CreateEvent(ctx, event.CreateEventRequest{
Type: event.EventTypeGitSyncCreate,
@@ -728,6 +759,109 @@ func (s *GitOpsSyncService) CreateSync(ctx context.Context, environmentID string
return s.GetSyncByID(ctx, "", syncRecord.ID)
}
+// prepareDeployProjectLinkInternal validates that an existing project can be
+// adopted by a deploy sync: it must exist, not be deployed from Git already,
+// and not be backed up to Git.
+func (s *GitOpsSyncService) prepareDeployProjectLinkInternal(ctx context.Context, tx *gorm.DB, req gitops.CreateSyncRequest) (*projectpkg.Project, error) {
+ if strings.TrimSpace(req.TargetType) == "swarm_stack" {
+ return nil, common.Classify(common.ErrValidation, errors.WithDetails(errors.New("an existing project cannot be linked to a swarm stack sync"), "field", "projectId"))
+ }
+ project, err := lockProjectForSyncInternal(tx, req.ProjectID)
+ if err != nil {
+ return nil, err
+ }
+ if project.GitOpsManagedBy != nil && strings.TrimSpace(*project.GitOpsManagedBy) != "" {
+ return nil, common.Classify(common.ErrConflict, errors.New("project is already deployed from Git"))
+ }
+ var backups int64
+ if err := tx.Model(&projectpkg.GitOpsSync{}).
+ Where("mode = ? AND project_id = ?", gitops.SyncModeBackup, project.ID).
+ Count(&backups).Error; err != nil {
+ return nil, errors.WrapIf(err, "failed to check existing backups")
+ }
+ if backups > 0 {
+ return nil, common.Classify(common.ErrConflict, errors.New("project is backed up to Git; disconnect that backup before deploying it from Git"))
+ }
+ if err := s.projectService.EnsureProjectPathUnderRoot(ctx, project, false); err != nil {
+ return nil, err
+ }
+ return project, nil
+}
+
+// insertSyncRecordInternal validates the project link and inserts the sync in
+// one transaction with the project row locked, so a project cannot end up with
+// two Git relationships. It returns the adopted project for deploy links.
+func (s *GitOpsSyncService) insertSyncRecordInternal(ctx context.Context, syncRecord *projectpkg.GitOpsSync, req gitops.CreateSyncRequest, mode, linkProjectID string) (*projectpkg.Project, error) {
+ var adoptedProject *projectpkg.Project
+ err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if mode == gitops.SyncModeDeploy && linkProjectID != "" {
+ project, err := s.prepareDeployProjectLinkInternal(ctx, tx, req)
+ if err != nil {
+ return err
+ }
+ adoptedProject = project
+ syncRecord.ProjectID = &project.ID
+ syncRecord.ProjectName = project.Name
+ }
+ if mode == gitops.SyncModeBackup {
+ backupConfig, err := s.prepareBackupCreateInternal(ctx, tx, req)
+ if err != nil {
+ return err
+ }
+ syncRecord.ProjectID = &backupConfig.project.ID
+ syncRecord.ProjectName = backupConfig.project.Name
+ syncRecord.TargetType = "project"
+ syncRecord.BackupDirectory = backupConfig.directory
+ syncRecord.ComposePath = path.Join(backupConfig.directory, backupConfig.composeFile)
+ syncRecord.BackupPaths = database.StringSlice(backupConfig.paths)
+ syncRecord.BackupOnSave = backupConfig.backupOnSave
+ syncRecord.AutoSync = true
+ syncRecord.SyncInterval = defaultBackupIntervalMinutes
+ if req.AutoSync != nil {
+ syncRecord.AutoSync = *req.AutoSync
+ }
+ if req.SyncInterval != nil {
+ syncRecord.SyncInterval = *req.SyncInterval
+ }
+ }
+
+ // Select("*") forces explicit zero values (e.g. "0 = unlimited" sync limits and
+ // unset pre-deploy fields) to persist instead of GORM substituting column defaults.
+ if err := tx.Select("*").Omit("Environment", "Repository", "Project").Create(syncRecord).Error; err != nil { //nolint:unqueryvet // intentional Select("*"); see comment above
+ if isUniqueViolationInternal(err) {
+ return common.Classify(common.ErrConflict, errors.New("project already has a Git backup; disconnect it first"))
+ }
+ slog.ErrorContext(ctx, "Failed to create GitOps sync in database", "name", req.Name, "repositoryID", req.RepositoryID, "environmentID", syncRecord.EnvironmentID, "error", err)
+ return errors.WrapIf(err, "failed to create sync")
+ }
+ if adoptedProject == nil {
+ return nil
+ }
+ linked := tx.Model(&projectpkg.Project{}).
+ Where("id = ? AND (gitops_managed_by IS NULL OR gitops_managed_by = '')", adoptedProject.ID).
+ Update("gitops_managed_by", syncRecord.ID)
+ if linked.Error != nil {
+ return errors.WrapIf(linked.Error, "failed to link existing project")
+ }
+ if linked.RowsAffected != 1 {
+ return common.Classify(common.ErrConflict, errors.New("project is already deployed from Git"))
+ }
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+ return adoptedProject, nil
+}
+
+func isUniqueViolationInternal(err error) bool {
+ if errors.Is(err, gorm.ErrDuplicatedKey) {
+ return true
+ }
+ message := strings.ToLower(err.Error())
+ return strings.Contains(message, "unique constraint") || strings.Contains(message, "duplicate key")
+}
+
func (s *GitOpsSyncService) UpdateSync(ctx context.Context, environmentID, id string, req gitops.UpdateSyncRequest, actor common.User) (*projectpkg.GitOpsSync, error) {
syncRecord, err := s.GetSyncByID(ctx, environmentID, id)
if err != nil {
@@ -798,6 +932,10 @@ func (s *GitOpsSyncService) UpdateSync(ctx context.Context, environmentID, id st
updates["max_sync_binary_size"] = *req.MaxSyncBinarySize
}
+ if err := s.applyModeUpdatesInternal(ctx, syncRecord, req, updates); err != nil {
+ return nil, err
+ }
+
lifecycleCfg := lifecycleConfigInputInternal{
targetType: req.TargetType,
scriptPath: req.PreDeployScriptPath,
@@ -853,6 +991,7 @@ func (s *GitOpsSyncService) DeleteSync(ctx context.Context, environmentID, id st
// longer be loaded (corrupt or environment-mismatched) must stop firing; any
// in-flight run re-reads the row and self-cancels once it is gone.
s.unregisterSyncJobInternal(ctx, id)
+ s.backups.cancel(id)
// Best-effort load for the audit-event metadata. A corrupt or env-mismatched row
// must still be deletable, so a load failure falls through to the direct delete
@@ -907,6 +1046,13 @@ func (s *GitOpsSyncService) DeleteSync(ctx context.Context, environmentID, id st
}
func (s *GitOpsSyncService) PerformSync(ctx context.Context, environmentID, id string, actor common.User) (*gitops.SyncResult, error) {
+ return s.performSyncAdmittedInternal(ctx, environmentID, id, actor, false)
+}
+
+// performSyncAdmittedInternal runs one sync under the per-sync admission lease
+// and dispatches by mode. backupAdopt makes a backup run replace whatever the
+// remote holds in its backup directory.
+func (s *GitOpsSyncService) performSyncAdmittedInternal(ctx context.Context, environmentID, id string, actor common.User, backupAdopt bool) (*gitops.SyncResult, error) {
// Coalesce overlapping runs for the same sync (scheduled fire, startup/enable
// kick, manual trigger, webhook) so they don't race the clone/redeploy.
lease, admitted, err := s.jobs.TryAcquire(ctx, id)
@@ -932,6 +1078,10 @@ func (s *GitOpsSyncService) PerformSync(ctx context.Context, environmentID, id s
SyncedAt: time.Now(),
}
+ if syncRecord.IsBackup() {
+ return s.performBackupInternal(syncCtx, syncRecord, actor, result, backupAdopt)
+ }
+
source, err := s.prepareSyncSource(syncCtx, syncRecord, result, actor)
if source != nil && source.repoPath != "" {
defer func() {
@@ -1307,12 +1457,17 @@ func (s *GitOpsSyncService) GetSyncStatus(ctx context.Context, environmentID, id
}
status := &gitops.SyncStatus{
- ID: syncRecord.ID,
- AutoSync: syncRecord.AutoSync,
- LastSyncAt: syncRecord.LastSyncAt,
- LastSyncStatus: syncRecord.LastSyncStatus,
- LastSyncError: syncRecord.LastSyncError,
- LastSyncCommit: syncRecord.LastSyncCommit,
+ ID: syncRecord.ID,
+ AutoSync: syncRecord.AutoSync,
+ LastSyncAt: syncRecord.LastSyncAt,
+ LastSyncStatus: syncRecord.LastSyncStatus,
+ LastSyncError: syncRecord.LastSyncError,
+ LastSyncCommit: syncRecord.LastSyncCommit,
+ Mode: syncRecord.Mode,
+ BackupState: syncRecord.BackupState(),
+ BackupPending: syncRecord.BackupPending,
+ BackupFailureReason: syncRecord.BackupFailureReason,
+ LastBackupAt: syncRecord.LastBackupAt,
}
// Calculate next sync time
diff --git a/backend/internal/gitops/handler.go b/backend/internal/gitops/handler.go
index 444165fdf..dc6b8be65 100644
--- a/backend/internal/gitops/handler.go
+++ b/backend/internal/gitops/handler.go
@@ -32,6 +32,10 @@ type ListGitOpsSyncsInput struct {
Order string `query:"order" default:"asc" doc:"Sort direction"`
Start int `query:"start" default:"0" doc:"Start index"`
Limit int `query:"limit" default:"20" doc:"Items per page"`
+ Mode string `query:"mode" doc:"Filter by direction (deploy or backup)"`
+ ProjectID string `query:"projectId" doc:"Filter by linked project ID"`
+ RepositoryID string `query:"repositoryId" doc:"Filter by repository ID"`
+ AutoSync string `query:"autoSync" doc:"Filter by automatic sync (true or false)"`
}
type ListGitOpsSyncsOutput struct {
@@ -80,6 +84,31 @@ type ImportGitOpsSyncsInput struct {
Body []gitops.ImportGitOpsSyncRequest
}
+const maxBackupHistoryLimit = 200
+
+type BackupPreviewInput struct {
+ EnvironmentID string `path:"id" doc:"Environment ID"`
+ SyncID string `path:"syncId" doc:"Sync ID"`
+}
+
+type BackupHistoryInput struct {
+ EnvironmentID string `path:"id" doc:"Environment ID"`
+ SyncID string `path:"syncId" doc:"Sync ID"`
+ Limit int `query:"limit" default:"20" doc:"Maximum number of revisions"`
+}
+
+type BackupRevisionInput struct {
+ EnvironmentID string `path:"id" doc:"Environment ID"`
+ SyncID string `path:"syncId" doc:"Sync ID"`
+ Commit string `path:"commit" doc:"Commit hash"`
+}
+
+type ResolveBackupConflictInput struct {
+ EnvironmentID string `path:"id" doc:"Environment ID"`
+ SyncID string `path:"syncId" doc:"Sync ID"`
+ Body gitops.ResolveBackupConflictRequest
+}
+
// ============================================================================
// Registration
// ============================================================================
@@ -100,6 +129,10 @@ func RegisterGitOpsSyncs(api huma.API, syncService *GitOpsSyncService) {
handlerutil.RegisterSecured(api, handlerutil.Operation("performGitOpsSync", "POST", syncPath+"/sync", "Perform a GitOps sync", "Manually trigger a sync operation", "GitOps Syncs"), authz.PermGitOpsSync, h.PerformSync)
handlerutil.RegisterSecured(api, handlerutil.Operation("getGitOpsSyncStatus", "GET", syncPath+"/status", "Get GitOps sync status", "Get the current status of a GitOps sync", "GitOps Syncs"), authz.PermGitOpsRead, h.GetStatus)
handlerutil.RegisterSecured(api, handlerutil.Operation("browseGitOpsSyncFiles", "GET", syncPath+"/files", "Browse GitOps sync files", "Browse files in the synced repository", "GitOps Syncs"), authz.PermGitOpsRead, h.BrowseFiles)
+ handlerutil.RegisterSecured(api, handlerutil.Operation("previewGitOpsBackup", "GET", syncPath+"/backup/preview", "Preview a Git backup", "Show the files the next backup would commit and any conflicts", "GitOps Syncs"), authz.PermGitOpsRead, h.PreviewBackup)
+ handlerutil.RegisterSecured(api, handlerutil.Operation("listGitOpsBackupHistory", "GET", syncPath+"/backup/history", "List Git backup history", "List repository revisions that changed the backup", "GitOps Syncs"), authz.PermGitOpsRead, h.BackupHistory)
+ handlerutil.RegisterSecured(api, handlerutil.Operation("getGitOpsBackupRevision", "GET", syncPath+"/backup/history/{commit}", "Get a Git backup revision", "Get one revision with per-file diffs", "GitOps Syncs"), authz.PermGitOpsRead, h.BackupRevision)
+ handlerutil.RegisterSecured(api, handlerutil.Operation("resolveGitOpsBackupConflict", "POST", syncPath+"/backup/resolve", "Resolve a Git backup conflict", "Resolve a backup that needs attention", "GitOps Syncs"), authz.PermGitOpsSync, h.ResolveBackupConflict)
}
// requireLifecyclePermissionInternal rejects callers lacking gitops:lifecycle
@@ -128,6 +161,11 @@ func requireLifecyclePermissionInternal(ctx context.Context, environmentID strin
// ListSyncs returns a paginated list of GitOps syncs.
func (h *GitOpsSyncHandler) ListSyncs(ctx context.Context, input *ListGitOpsSyncsInput) (*ListGitOpsSyncsOutput, error) {
params := handlerutil.PaginationParams(input.Start, input.Limit, input.Sort, input.Order, input.Search)
+ for key, value := range map[string]string{"mode": input.Mode, "projectId": input.ProjectID, "repositoryId": input.RepositoryID, "autoSync": input.AutoSync} {
+ if value != "" {
+ params.Filters[key] = value
+ }
+ }
syncs, paginationResp, counts, err := h.syncService.GetSyncsPaginated(ctx, input.EnvironmentID, params)
if err != nil {
@@ -301,3 +339,69 @@ func (h *GitOpsSyncHandler) BrowseFiles(ctx context.Context, input *BrowseSyncFi
},
}, nil
}
+
+// PreviewBackup shows what the next backup would commit.
+func (h *GitOpsSyncHandler) PreviewBackup(ctx context.Context, input *BackupPreviewInput) (*handlerutil.Out[gitops.BackupPreview], error) {
+ preview, err := h.syncService.PreviewBackup(ctx, input.EnvironmentID, input.SyncID)
+ if err != nil {
+ apiErr := common.ToAPIError(err)
+ return nil, huma.NewError(apiErr.HTTPStatus(), errors.WithMessage(err, "Failed to preview Git backup").Error())
+ }
+
+ return &handlerutil.Out[gitops.BackupPreview]{
+ Body: base.ApiResponse[gitops.BackupPreview]{
+ Success: true,
+ Data: *preview,
+ },
+ }, nil
+}
+
+// BackupHistory lists revisions affecting the backup.
+func (h *GitOpsSyncHandler) BackupHistory(ctx context.Context, input *BackupHistoryInput) (*handlerutil.Out[gitops.BackupHistoryResponse], error) {
+ history, err := h.syncService.GetBackupHistory(ctx, input.EnvironmentID, input.SyncID, min(input.Limit, maxBackupHistoryLimit))
+ if err != nil {
+ apiErr := common.ToAPIError(err)
+ return nil, huma.NewError(apiErr.HTTPStatus(), errors.WithMessage(err, "Failed to load Git backup history").Error())
+ }
+
+ return &handlerutil.Out[gitops.BackupHistoryResponse]{
+ Body: base.ApiResponse[gitops.BackupHistoryResponse]{
+ Success: true,
+ Data: *history,
+ },
+ }, nil
+}
+
+// BackupRevision returns one revision with diffs.
+func (h *GitOpsSyncHandler) BackupRevision(ctx context.Context, input *BackupRevisionInput) (*handlerutil.Out[gitops.BackupRevision], error) {
+ revision, err := h.syncService.GetBackupRevision(ctx, input.EnvironmentID, input.SyncID, input.Commit)
+ if err != nil {
+ apiErr := common.ToAPIError(err)
+ return nil, huma.NewError(apiErr.HTTPStatus(), errors.WithMessage(err, "Failed to load Git backup revision").Error())
+ }
+
+ return &handlerutil.Out[gitops.BackupRevision]{
+ Body: base.ApiResponse[gitops.BackupRevision]{
+ Success: true,
+ Data: *revision,
+ },
+ }, nil
+}
+
+// ResolveBackupConflict resolves a backup that needs attention.
+func (h *GitOpsSyncHandler) ResolveBackupConflict(ctx context.Context, input *ResolveBackupConflictInput) (*handlerutil.Out[gitops.SyncResult], error) {
+ actor := handlerutil.CurrentActor(ctx)
+
+ result, err := h.syncService.ResolveBackupConflict(ctx, input.EnvironmentID, input.SyncID, input.Body, actor)
+ if err != nil {
+ apiErr := common.ToAPIError(err)
+ return nil, huma.NewError(apiErr.HTTPStatus(), errors.WithMessage(err, "Failed to resolve Git backup conflict").Error())
+ }
+
+ return &handlerutil.Out[gitops.SyncResult]{
+ Body: base.ApiResponse[gitops.SyncResult]{
+ Success: result.Success,
+ Data: *result,
+ },
+ }, nil
+}
diff --git a/backend/internal/project/model.go b/backend/internal/project/model.go
index 3ae0c8f24..1457f5118 100644
--- a/backend/internal/project/model.go
+++ b/backend/internal/project/model.go
@@ -1,11 +1,12 @@
package project
import (
+ "time"
+
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/environment"
"github.com/getarcaneapp/arcane/backend/v2/internal/gitrepo"
-
- "time"
+ gitopstypes "github.com/getarcaneapp/arcane/types/v2/gitops"
)
type ProjectStatus string
@@ -100,6 +101,51 @@ type GitOpsSync struct {
SyncDirectory bool `json:"syncDirectory" gorm:"column:sync_directory"` // Sync entire directory containing compose file
PullImageAfterSync bool `json:"pullImageAfterSync" gorm:"column:pull_image_after_sync;default:false"`
RedeployAfterSync bool `json:"redeployAfterSync" gorm:"column:redeploy_after_sync;default:false"`
+
+ // Backup mode ("backup" commits saved project files to the repository;
+ // "deploy" is the original pull direction).
+ Mode string `json:"mode" gorm:"column:mode;default:'deploy'" sortable:"true" search:"mode,backup,deploy,direction"`
+ BackupDirectory string `json:"backupDirectory" gorm:"column:backup_directory" sortable:"true" search:"backup,directory,destination,folder"`
+ BackupPaths database.StringSlice `json:"backupPaths" gorm:"column:backup_paths;type:text"`
+ BackupOnSave bool `json:"backupOnSave" gorm:"column:backup_on_save;default:true"`
+ BackupPending bool `json:"backupPending" gorm:"column:backup_pending;default:false"`
+ BackupPendingSince *time.Time `json:"backupPendingSince,omitempty" gorm:"column:backup_pending_since"`
+ BackupConflict bool `json:"backupConflict" gorm:"column:backup_conflict;default:false"`
+ BackupFailureReason *string `json:"backupFailureReason,omitempty" gorm:"column:backup_failure_reason"`
+ LastBackupAt *time.Time `json:"lastBackupAt,omitempty" gorm:"column:last_backup_at" sortable:"true"`
+ LastBackupSnapshot *string `json:"-" gorm:"column:last_backup_snapshot"`
+}
+
+// IsBackup reports whether the sync commits project files to Git.
+func (s GitOpsSync) IsBackup() bool {
+ return s.Mode == gitopstypes.SyncModeBackup
+}
+
+// BackupState derives the persisted backup lifecycle state.
+func (s GitOpsSync) BackupState() string {
+ if !s.IsBackup() {
+ return ""
+ }
+ status := ""
+ if s.LastSyncStatus != nil {
+ status = *s.LastSyncStatus
+ }
+ switch {
+ case status == "running":
+ return gitopstypes.BackupStateBackingUp
+ case s.BackupConflict:
+ return gitopstypes.BackupStateNeedsAttention
+ case status == "failed":
+ return gitopstypes.BackupStateFailed
+ case !s.AutoSync:
+ return gitopstypes.BackupStatePaused
+ case s.BackupPending:
+ return gitopstypes.BackupStatePending
+ case s.LastBackupAt != nil:
+ return gitopstypes.BackupStateBackedUp
+ default:
+ return gitopstypes.BackupStateNever
+ }
}
func (GitOpsSync) TableName() string {
diff --git a/backend/internal/project/project.go b/backend/internal/project/project.go
index 2cc0c0dd2..bddbd06e5 100644
--- a/backend/internal/project/project.go
+++ b/backend/internal/project/project.go
@@ -12,6 +12,7 @@ import (
"emperror.dev/errors"
composetypes "github.com/compose-spec/compose-go/v2/types"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/actors"
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
@@ -62,6 +63,18 @@ type ProjectService struct {
// projects, a gitops_syncs lookup — per project, on every list request.
// Entries are validated by compose/include/env file mtimes rather than a TTL.
metaCache projecttypes.ComposeCache[projects.ArcaneComposeMetadata]
+
+ // filesChanged fires with the project ID after project files are saved
+ // through Arcane, so Git backups can react without polling.
+ filesChanged *actors.Signal[string]
+}
+
+// FilesChanged exposes the project-file-change signal for subscribers.
+func (s *ProjectService) FilesChanged() *actors.Signal[string] {
+ if s == nil {
+ return nil
+ }
+ return s.filesChanged
}
// EnsureGitOpsProjectLinked persists the bidirectional GitOps/project binding
@@ -208,6 +221,7 @@ func NewProjectService(db *database.DB, settingsService *settings.SettingsServic
config: cfg,
parsedCompose: projects.NewParsedComposeCache(),
metaCache: projects.NewComposeCache[projects.ArcaneComposeMetadata](1024, nil),
+ filesChanged: actors.NewSignal[string](),
}
}
diff --git a/backend/internal/project/project_tags.go b/backend/internal/project/project_tags.go
index b32ba3144..dfaf437c1 100644
--- a/backend/internal/project/project_tags.go
+++ b/backend/internal/project/project_tags.go
@@ -13,6 +13,7 @@ import (
"emperror.dev/errors"
projectpkg "github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
+ gitopstypes "github.com/getarcaneapp/arcane/types/v2/gitops"
projecttypes "github.com/getarcaneapp/arcane/types/v2/project"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -227,6 +228,9 @@ func deleteProjectWithTagsInternal(tx *gorm.DB, projectID string) error {
if err := tx.Where("project_id = ?", projectID).Delete(&ProjectTag{}).Error; err != nil {
return errors.WrapIf(err, "delete project tags")
}
+ if err := tx.Where("mode = ? AND project_id = ?", gitopstypes.SyncModeBackup, projectID).Delete(&GitOpsSync{}).Error; err != nil {
+ return errors.WrapIf(err, "delete project git backups")
+ }
return errors.WrapIf(tx.Delete(&Project{}, "id = ?", projectID).Error, "delete project")
}
diff --git a/backend/internal/project/project_update.go b/backend/internal/project/project_update.go
index 32a3a4973..0d885b6cb 100644
--- a/backend/internal/project/project_update.go
+++ b/backend/internal/project/project_update.go
@@ -95,6 +95,9 @@ func (s *ProjectService) UpdateProject(ctx context.Context, projectID string, na
s.refreshProjectAfterContentUpdateInternal(ctx, &proj, composeContent, overrideContent)
s.logProjectUpdateEventInternal(ctx, &proj, composeContent, envContent, overrideContent, user)
+ if composeContent != nil || envContent != nil || overrideContent != nil {
+ s.filesChanged.Publish(proj.ID)
+ }
slog.InfoContext(ctx, "project updated", "projectID", proj.ID, "name", proj.Name)
return &proj, nil
diff --git a/backend/internal/project/service_test.go b/backend/internal/project/service_test.go
index fcca9c631..54dc9a071 100644
--- a/backend/internal/project/service_test.go
+++ b/backend/internal/project/service_test.go
@@ -82,7 +82,7 @@ func setupProjectTestDB(t *testing.T) *database.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.NoError(t, err)
- require.NoError(t, db.AutoMigrate(&Project{}, &ProjectTag{}, &settings.SettingVariable{}, &imageupdate.ImageUpdateRecord{}, &event.Event{}))
+ require.NoError(t, db.AutoMigrate(&Project{}, &ProjectTag{}, &GitOpsSync{}, &settings.SettingVariable{}, &imageupdate.ImageUpdateRecord{}, &event.Event{}))
return &database.DB{DB: db}
}
diff --git a/backend/internal/project/workspace.go b/backend/internal/project/workspace.go
index 59d19740d..fefab2264 100644
--- a/backend/internal/project/workspace.go
+++ b/backend/internal/project/workspace.go
@@ -181,6 +181,7 @@ func (s *ProjectService) UpdateProjectWorkspace(ctx context.Context, projectID s
"action": "update_project_workspace",
"fileChangeCount": len(manifest.FileChanges),
}, "could not log project workspace update")
+ s.filesChanged.Publish(proj.ID)
return s.GetProjectWorkspace(ctx, projectID)
}
diff --git a/backend/pkg/gitutil/git_write.go b/backend/pkg/gitutil/git_write.go
new file mode 100644
index 000000000..82d3bb6a4
--- /dev/null
+++ b/backend/pkg/gitutil/git_write.go
@@ -0,0 +1,475 @@
+package git
+
+import (
+ "bytes"
+ "context"
+ "io"
+ "io/fs"
+ "os"
+ "path"
+ "path/filepath"
+ "sort"
+ "strings"
+ "time"
+
+ "emperror.dev/errors"
+ "github.com/go-git/go-git/v5"
+ "github.com/go-git/go-git/v5/config"
+ "github.com/go-git/go-git/v5/plumbing"
+ "github.com/go-git/go-git/v5/plumbing/format/diff"
+ "github.com/go-git/go-git/v5/plumbing/object"
+ "github.com/go-git/go-git/v5/plumbing/transport"
+ "go.getarcane.app/acfs"
+)
+
+// ErrPushRejected reports that the remote branch advanced between checkout and push.
+var ErrPushRejected = errors.Sentinel("push rejected: remote branch was updated by another writer")
+
+// WriteCheckout is a scratch clone prepared for committing to one branch.
+type WriteCheckout struct {
+ RepoPath string
+ Branch string
+ HeadCommit string
+ BranchExists bool
+ url string
+ repo *git.Repository
+}
+
+// CommitFile is one file to write into the checkout, repo-relative.
+type CommitFile struct {
+ Path string
+ Content []byte
+ Executable bool
+}
+
+// CommitRequest describes the tree mutation to commit and push.
+type CommitRequest struct {
+ Files []CommitFile
+ Remove []string
+ Message string
+ AuthorName string
+ AuthorEmail string
+}
+
+// HistoryEntry is one commit touching a directory.
+type HistoryEntry struct {
+ Hash string
+ Author string
+ Email string
+ Message string
+ Date time.Time
+ Files []string
+}
+
+// FileDiff is the unified diff of one file within a commit.
+type FileDiff struct {
+ Path string
+ Patch string
+}
+
+// CheckoutForWrite clones url at branch into a scratch directory. A branch that
+// does not exist yet is created from the remote default branch, and an empty
+// repository is initialized locally so the first push creates the branch.
+func (c *Client) CheckoutForWrite(ctx context.Context, url, branch string, auth AuthConfig) (*WriteCheckout, error) {
+ if strings.TrimSpace(branch) == "" {
+ return nil, errors.New("branch is required")
+ }
+ normalized, err := normalizeURL(url)
+ if err != nil {
+ return nil, err
+ }
+
+ repoPath, err := c.Clone(ctx, normalized, branch, auth)
+ if err == nil {
+ repo, openErr := git.PlainOpen(repoPath)
+ if openErr != nil {
+ _ = c.Cleanup(repoPath)
+ return nil, errors.WrapIf(openErr, "failed to open checkout")
+ }
+ head, headErr := repo.Head()
+ if headErr != nil {
+ _ = c.Cleanup(repoPath)
+ return nil, errors.WrapIf(headErr, "failed to resolve checkout head")
+ }
+ return &WriteCheckout{RepoPath: repoPath, Branch: branch, HeadCommit: head.Hash().String(), BranchExists: true, url: normalized, repo: repo}, nil
+ }
+
+ switch {
+ case errors.Is(err, transport.ErrEmptyRemoteRepository):
+ return c.initEmptyCheckoutInternal(normalized, branch)
+ case errors.Is(err, git.NoMatchingRefSpecError{}):
+ return c.checkoutNewBranchInternal(ctx, normalized, branch, auth)
+ default:
+ return nil, err
+ }
+}
+
+func (c *Client) initEmptyCheckoutInternal(url, branch string) (*WriteCheckout, error) {
+ workDir := c.workDir
+ if workDir == "" {
+ workDir = os.TempDir()
+ }
+ if err := os.MkdirAll(workDir, 0o755); err != nil {
+ return nil, errors.WrapIf(err, "failed to create work dir")
+ }
+ repoPath, err := os.MkdirTemp(workDir, cloneScratchPrefix+"*")
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to create temp dir")
+ }
+ repo, err := git.PlainInit(repoPath, false)
+ if err != nil {
+ _ = os.RemoveAll(repoPath)
+ return nil, errors.WrapIf(err, "failed to initialize checkout")
+ }
+ if _, err := repo.CreateRemote(&config.RemoteConfig{Name: "origin", URLs: []string{url}}); err != nil {
+ _ = os.RemoveAll(repoPath)
+ return nil, errors.WrapIf(err, "failed to configure remote")
+ }
+ if err := repo.Storer.SetReference(plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.NewBranchReferenceName(branch))); err != nil {
+ _ = os.RemoveAll(repoPath)
+ return nil, errors.WrapIf(err, "failed to select branch")
+ }
+ return &WriteCheckout{RepoPath: repoPath, Branch: branch, url: url, repo: repo}, nil
+}
+
+func (c *Client) checkoutNewBranchInternal(ctx context.Context, url, branch string, auth AuthConfig) (*WriteCheckout, error) {
+ repoPath, err := c.Clone(ctx, url, "", auth)
+ if err != nil {
+ return nil, err
+ }
+ repo, err := git.PlainOpen(repoPath)
+ if err != nil {
+ _ = c.Cleanup(repoPath)
+ return nil, errors.WrapIf(err, "failed to open checkout")
+ }
+ worktree, err := repo.Worktree()
+ if err != nil {
+ _ = c.Cleanup(repoPath)
+ return nil, errors.WrapIf(err, "failed to open worktree")
+ }
+ if err := worktree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName(branch), Create: true}); err != nil {
+ _ = c.Cleanup(repoPath)
+ return nil, errors.WrapIff(err, "failed to create branch %s", branch)
+ }
+ return &WriteCheckout{RepoPath: repoPath, Branch: branch, url: url, repo: repo}, nil
+}
+
+// CommitAndPush writes the requested files, removes the listed paths, commits
+// when the tree changed, pushes without force, and verifies the remote branch
+// points at the pushed commit. It returns the resulting head and whether a new
+// commit was created.
+func (c *Client) CommitAndPush(ctx context.Context, checkout *WriteCheckout, req CommitRequest, auth AuthConfig) (string, bool, error) {
+ if checkout == nil || checkout.repo == nil {
+ return "", false, errors.New("checkout is not prepared for writing")
+ }
+ worktree, err := checkout.repo.Worktree()
+ if err != nil {
+ return "", false, errors.WrapIf(err, "failed to open worktree")
+ }
+ if err := stageCommitFilesInternal(ctx, checkout, worktree, req); err != nil {
+ return "", false, err
+ }
+
+ signature := &object.Signature{Name: req.AuthorName, Email: req.AuthorEmail, When: time.Now()}
+ hash, err := worktree.Commit(req.Message, &git.CommitOptions{Author: signature, Committer: signature})
+ if err != nil {
+ if errors.Is(err, git.ErrEmptyCommit) {
+ return checkout.HeadCommit, false, nil
+ }
+ return "", false, errors.WrapIf(err, "failed to commit")
+ }
+ if err := c.pushBranchInternal(ctx, checkout, hash.String(), auth); err != nil {
+ return "", false, err
+ }
+ checkout.HeadCommit = hash.String()
+ checkout.BranchExists = true
+ return hash.String(), true, nil
+}
+
+// stageCommitFilesInternal writes through acfs so a symlink committed in the
+// repository can never redirect a write outside the checkout.
+func stageCommitFilesInternal(ctx context.Context, checkout *WriteCheckout, worktree *git.Worktree, req CommitRequest) error {
+ for _, file := range req.Files {
+ if err := ValidatePath(checkout.RepoPath, file.Path); err != nil {
+ return err
+ }
+ logical := path.Join("/", filepath.ToSlash(file.Path))
+ if err := acfs.MkdirAll(ctx, checkout.RepoPath, path.Dir(logical), 0o755); err != nil {
+ return errors.WrapIff(err, "failed to create directory for %s", file.Path)
+ }
+ mode := os.FileMode(0o644)
+ if file.Executable {
+ mode = 0o755
+ }
+ if _, err := acfs.WriteFrom(ctx, checkout.RepoPath, logical, bytes.NewReader(file.Content), int64(len(file.Content)), mode); err != nil {
+ return errors.WrapIff(err, "failed to write %s", file.Path)
+ }
+ if _, err := worktree.Add(file.Path); err != nil {
+ return errors.WrapIff(err, "failed to stage %s", file.Path)
+ }
+ }
+ for _, removed := range req.Remove {
+ if err := ValidatePath(checkout.RepoPath, removed); err != nil {
+ return err
+ }
+ if _, err := acfs.Stat(ctx, checkout.RepoPath, path.Join("/", filepath.ToSlash(removed)), false); err != nil {
+ if errors.Is(err, fs.ErrNotExist) {
+ continue
+ }
+ return errors.WrapIff(err, "failed to inspect %s", removed)
+ }
+ if _, err := worktree.Remove(removed); err != nil {
+ return errors.WrapIff(err, "failed to remove %s", removed)
+ }
+ }
+ return nil
+}
+
+// pushBranchInternal pushes the checkout branch without force and verifies the
+// remote now points at commit.
+func (c *Client) pushBranchInternal(ctx context.Context, checkout *WriteCheckout, commit string, auth AuthConfig) error {
+ authMethod, err := c.getAuthInternal(checkout.url, auth)
+ if err != nil {
+ return err
+ }
+ refName := plumbing.NewBranchReferenceName(checkout.Branch)
+ pushOptions := &git.PushOptions{
+ RemoteName: "origin",
+ RefSpecs: []config.RefSpec{config.RefSpec(refName.String() + ":" + refName.String())},
+ }
+ if authMethod != nil {
+ pushOptions.Auth = authMethod
+ }
+ if err := checkout.repo.PushContext(ctx, pushOptions); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
+ if isPushRejectedInternal(err) {
+ return errors.WrapIf(ErrPushRejected, err.Error())
+ }
+ return errors.WrapIf(err, "failed to push")
+ }
+
+ remoteHead, exists, err := c.RemoteBranchHead(ctx, checkout.url, checkout.Branch, auth)
+ if err != nil {
+ return errors.WrapIf(err, "failed to verify pushed commit")
+ }
+ if !exists || remoteHead != commit {
+ return errors.WrapIf(ErrPushRejected, "remote branch does not point at the pushed commit")
+ }
+ return nil
+}
+
+func isPushRejectedInternal(err error) bool {
+ if errors.Is(err, git.ErrNonFastForwardUpdate) || errors.Is(err, git.ErrForceNeeded) {
+ return true
+ }
+ message := err.Error()
+ return strings.Contains(message, "non-fast-forward") || strings.Contains(message, "command error") || strings.Contains(message, "failed to update ref")
+}
+
+// RemoteBranchHead resolves the remote head of branch without cloning.
+func (c *Client) RemoteBranchHead(ctx context.Context, url, branch string, auth AuthConfig) (string, bool, error) {
+ refs, err := c.listRemoteReferences(ctx, url, auth)
+ if err != nil {
+ if errors.Is(err, transport.ErrEmptyRemoteRepository) {
+ return "", false, nil
+ }
+ return "", false, err
+ }
+ refName := plumbing.NewBranchReferenceName(branch)
+ for _, ref := range refs {
+ if ref.Name() == refName {
+ return ref.Hash().String(), true, nil
+ }
+ }
+ return "", false, nil
+}
+
+// DirectoryHistory lists the newest commits that touched directory, newest first.
+func (c *Client) DirectoryHistory(ctx context.Context, repoPath, directory string, limit int) ([]HistoryEntry, error) {
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ repo, err := git.PlainOpen(repoPath)
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to open repository")
+ }
+ head, err := repo.Head()
+ if err != nil {
+ if errors.Is(err, plumbing.ErrReferenceNotFound) {
+ return nil, nil
+ }
+ return nil, errors.WrapIf(err, "failed to resolve head")
+ }
+ prefix := directoryPrefixInternal(directory)
+ iter, err := repo.Log(&git.LogOptions{From: head.Hash(), PathFilter: func(p string) bool { return strings.HasPrefix(p, prefix) }})
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to read history")
+ }
+ defer iter.Close()
+
+ entries := make([]HistoryEntry, 0, max(limit, 0))
+ for {
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ commit, err := iter.Next()
+ if err != nil {
+ if errors.Is(err, io.EOF) {
+ break
+ }
+ return nil, errors.WrapIf(err, "failed to iterate history")
+ }
+ files, err := commitDirectoryFilesInternal(commit, prefix)
+ if err != nil {
+ return nil, err
+ }
+ entries = append(entries, historyEntryInternal(commit, files))
+ if limit > 0 && len(entries) >= limit {
+ break
+ }
+ }
+ return entries, nil
+}
+
+// CommitDiff returns the commit and the per-file unified diffs inside directory.
+func (c *Client) CommitDiff(ctx context.Context, repoPath, commitHash, directory string) (HistoryEntry, []FileDiff, error) {
+ if err := ctx.Err(); err != nil {
+ return HistoryEntry{}, nil, err
+ }
+ repo, err := git.PlainOpen(repoPath)
+ if err != nil {
+ return HistoryEntry{}, nil, errors.WrapIf(err, "failed to open repository")
+ }
+ commit, err := repo.CommitObject(plumbing.NewHash(commitHash))
+ if err != nil {
+ return HistoryEntry{}, nil, errors.WrapIf(err, "commit not found")
+ }
+ prefix := directoryPrefixInternal(directory)
+ patch, err := commitPatchInternal(commit)
+ if err != nil {
+ return HistoryEntry{}, nil, err
+ }
+
+ var files []string
+ var diffs []FileDiff
+ for _, filePatch := range patch.FilePatches() {
+ name, relative, ok := patchPathInDirectoryInternal(filePatch, prefix)
+ if !ok {
+ continue
+ }
+ files = append(files, relative)
+ single, err := commitFilePatchInternal(commit, name)
+ if err != nil {
+ return HistoryEntry{}, nil, err
+ }
+ diffs = append(diffs, FileDiff{Path: relative, Patch: single})
+ }
+ return historyEntryInternal(commit, files), diffs, nil
+}
+
+func directoryPrefixInternal(directory string) string {
+ cleaned := strings.Trim(path.Clean(filepath.ToSlash(directory)), "/")
+ if cleaned == "" || cleaned == "." {
+ return ""
+ }
+ return cleaned + "/"
+}
+
+func historyEntryInternal(commit *object.Commit, files []string) HistoryEntry {
+ sort.Strings(files)
+ if files == nil {
+ files = []string{}
+ }
+ return HistoryEntry{
+ Hash: commit.Hash.String(),
+ Author: commit.Author.Name,
+ Email: commit.Author.Email,
+ Message: strings.TrimSpace(commit.Message),
+ Date: commit.Author.When,
+ Files: files,
+ }
+}
+
+func commitPatchInternal(commit *object.Commit) (*object.Patch, error) {
+ var parentTree *object.Tree
+ if commit.NumParents() > 0 {
+ parent, err := commit.Parent(0)
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to load parent commit")
+ }
+ parentTree, err = parent.Tree()
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to load parent tree")
+ }
+ }
+ tree, err := commit.Tree()
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to load commit tree")
+ }
+ patch, err := parentTree.Patch(tree)
+ if err != nil {
+ return nil, errors.WrapIf(err, "failed to diff commit")
+ }
+ return patch, nil
+}
+
+func commitDirectoryFilesInternal(commit *object.Commit, prefix string) ([]string, error) {
+ patch, err := commitPatchInternal(commit)
+ if err != nil {
+ return nil, err
+ }
+ var files []string
+ for _, filePatch := range patch.FilePatches() {
+ if _, relative, ok := patchPathInDirectoryInternal(filePatch, prefix); ok {
+ files = append(files, relative)
+ }
+ }
+ return files, nil
+}
+
+// patchPathInDirectoryInternal picks the side of a file patch that lives under
+// prefix, so renames into or out of the directory are still reported.
+func patchPathInDirectoryInternal(filePatch diff.FilePatch, prefix string) (string, string, bool) {
+ from, to := filePatch.Files()
+ for _, file := range []diff.File{to, from} {
+ if file == nil {
+ continue
+ }
+ if relative, ok := strings.CutPrefix(file.Path(), prefix); ok {
+ return file.Path(), relative, true
+ }
+ }
+ return "", "", false
+}
+
+func commitFilePatchInternal(commit *object.Commit, name string) (string, error) {
+ var parentTree *object.Tree
+ if commit.NumParents() > 0 {
+ parent, err := commit.Parent(0)
+ if err != nil {
+ return "", errors.WrapIf(err, "failed to load parent commit")
+ }
+ parentTree, err = parent.Tree()
+ if err != nil {
+ return "", errors.WrapIf(err, "failed to load parent tree")
+ }
+ }
+ tree, err := commit.Tree()
+ if err != nil {
+ return "", errors.WrapIf(err, "failed to load commit tree")
+ }
+ changes, err := object.DiffTree(parentTree, tree)
+ if err != nil {
+ return "", errors.WrapIf(err, "failed to diff commit")
+ }
+ for _, change := range changes {
+ if change.From.Name != name && change.To.Name != name {
+ continue
+ }
+ patch, err := change.Patch()
+ if err != nil {
+ return "", errors.WrapIff(err, "failed to build patch for %s", name)
+ }
+ return patch.String(), nil
+ }
+ return "", nil
+}
diff --git a/backend/pkg/gitutil/git_write_test.go b/backend/pkg/gitutil/git_write_test.go
new file mode 100644
index 000000000..9a877cb15
--- /dev/null
+++ b/backend/pkg/gitutil/git_write_test.go
@@ -0,0 +1,208 @@
+package git
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "sync"
+ "testing"
+ "time"
+
+ "emperror.dev/errors"
+ "github.com/go-git/go-billy/v5/osfs"
+ gogit "github.com/go-git/go-git/v5"
+ "github.com/go-git/go-git/v5/plumbing/object"
+ "github.com/go-git/go-git/v5/plumbing/transport/client"
+ "github.com/go-git/go-git/v5/plumbing/transport/server"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+var installWriteTestTransportOnceInternal sync.Once
+
+// installWriteTestTransportInternal serves bare repositories on disk over the
+// "http" scheme so the write paths run without a network or the git binary.
+func installWriteTestTransportInternal(t *testing.T) {
+ t.Helper()
+ installWriteTestTransportOnceInternal.Do(func() {
+ client.InstallProtocol("http", server.NewClient(server.NewFilesystemLoader(osfs.New("/"))))
+ })
+}
+
+func newBareRepoURLInternal(t *testing.T) string {
+ t.Helper()
+ installWriteTestTransportInternal(t)
+ bare := filepath.Join(t.TempDir(), "bare.git")
+ _, err := gogit.PlainInit(bare, true)
+ require.NoError(t, err)
+ return "http://localhost" + bare
+}
+
+func noAuthInternal() AuthConfig {
+ return AuthConfig{AuthType: "none"}
+}
+
+// pushCommitInternal commits files onto branch through a separate checkout,
+// standing in for another writer against the same remote.
+func pushCommitInternal(t *testing.T, url, branch, message string, files map[string]string) string {
+ t.Helper()
+ writer := NewClient(t.TempDir())
+ checkout, err := writer.CheckoutForWrite(t.Context(), url, branch, noAuthInternal())
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = writer.Cleanup(checkout.RepoPath) })
+
+ request := CommitRequest{Message: message, AuthorName: "Other", AuthorEmail: "other@localhost"}
+ for path, content := range files {
+ request.Files = append(request.Files, CommitFile{Path: path, Content: []byte(content)})
+ }
+ head, _, err := writer.CommitAndPush(t.Context(), checkout, request, noAuthInternal())
+ require.NoError(t, err)
+ return head
+}
+
+func TestWriteCommitAndPush_CreatesBranchInEmptyRepository(t *testing.T) {
+ ctx := t.Context()
+ url := newBareRepoURLInternal(t)
+ gitClient := NewClient(t.TempDir())
+
+ checkout, err := gitClient.CheckoutForWrite(ctx, url, "main", noAuthInternal())
+ require.NoError(t, err)
+ require.False(t, checkout.BranchExists)
+
+ request := CommitRequest{
+ Files: []CommitFile{{Path: "backups/app/compose.yaml", Content: []byte("services: {}\n")}},
+ Message: "first backup",
+ AuthorName: "Arcane",
+ AuthorEmail: "arcane@localhost",
+ }
+ head, committed, err := gitClient.CommitAndPush(ctx, checkout, request, noAuthInternal())
+ require.NoError(t, err)
+ require.True(t, committed)
+ require.NotEmpty(t, head)
+
+ remoteHead, exists, err := gitClient.RemoteBranchHead(ctx, url, "main", noAuthInternal())
+ require.NoError(t, err)
+ require.True(t, exists)
+ require.Equal(t, head, remoteHead)
+
+ second, committed, err := gitClient.CommitAndPush(ctx, checkout, request, noAuthInternal())
+ require.NoError(t, err)
+ assert.False(t, committed)
+ assert.Equal(t, head, second)
+}
+
+func TestCheckoutForWrite_CreatesMissingBranchFromDefault(t *testing.T) {
+ ctx := t.Context()
+ url := newBareRepoURLInternal(t)
+ gitClient := NewClient(t.TempDir())
+
+ pushCommitInternal(t, url, "master", "seed", map[string]string{"unrelated/keep.txt": "keep\n"})
+
+ checkout, err := gitClient.CheckoutForWrite(ctx, url, "feature", noAuthInternal())
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = gitClient.Cleanup(checkout.RepoPath) })
+ require.FileExists(t, filepath.Join(checkout.RepoPath, "unrelated", "keep.txt"))
+
+ head, committed, err := gitClient.CommitAndPush(ctx, checkout, CommitRequest{
+ Files: []CommitFile{{Path: "backups/app/compose.yaml", Content: []byte("services: {}\n")}},
+ Message: "backup on new branch",
+ AuthorName: "Arcane",
+ AuthorEmail: "arcane@localhost",
+ }, noAuthInternal())
+ require.NoError(t, err)
+ require.True(t, committed)
+
+ remoteHead, exists, err := gitClient.RemoteBranchHead(ctx, url, "feature", noAuthInternal())
+ require.NoError(t, err)
+ require.True(t, exists)
+ require.Equal(t, head, remoteHead)
+
+ verify := NewClient(t.TempDir())
+ repoPath, err := verify.Clone(ctx, url, "feature", noAuthInternal())
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = verify.Cleanup(repoPath) })
+ assert.FileExists(t, filepath.Join(repoPath, "unrelated", "keep.txt"))
+ assert.FileExists(t, filepath.Join(repoPath, "backups", "app", "compose.yaml"))
+}
+
+func TestWriteCommitAndPush_RejectsWhenRemoteAdvanced(t *testing.T) {
+ ctx := t.Context()
+ url := newBareRepoURLInternal(t)
+ gitClient := NewClient(t.TempDir())
+
+ pushCommitInternal(t, url, "main", "seed", map[string]string{"backups/app/compose.yaml": "services: {}\n"})
+
+ checkout, err := gitClient.CheckoutForWrite(ctx, url, "main", noAuthInternal())
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = gitClient.Cleanup(checkout.RepoPath) })
+
+ pushCommitInternal(t, url, "main", "other writer", map[string]string{"other/notes.txt": "notes\n"})
+
+ _, _, err = gitClient.CommitAndPush(ctx, checkout, CommitRequest{
+ Files: []CommitFile{{Path: "backups/app/compose.yaml", Content: []byte("services: {app: {}}\n")}},
+ Message: "stale backup",
+ AuthorName: "Arcane",
+ AuthorEmail: "arcane@localhost",
+ }, noAuthInternal())
+ require.Error(t, err)
+ assert.ErrorIs(t, err, ErrPushRejected)
+}
+
+func TestDirectoryHistoryAndCommitDiff(t *testing.T) {
+ ctx := t.Context()
+ repoPath := t.TempDir()
+ repo, err := gogit.PlainInit(repoPath, false)
+ require.NoError(t, err)
+ worktree, err := repo.Worktree()
+ require.NoError(t, err)
+
+ commit := func(message string, files map[string]string) string {
+ for name, content := range files {
+ target := filepath.Join(repoPath, filepath.FromSlash(name))
+ require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755))
+ require.NoError(t, os.WriteFile(target, []byte(content), 0o644))
+ _, err := worktree.Add(name)
+ require.NoError(t, err)
+ }
+ signature := &object.Signature{Name: "Arcane", Email: "arcane@localhost", When: time.Now()}
+ hash, err := worktree.Commit(message, &gogit.CommitOptions{Author: signature, Committer: signature})
+ require.NoError(t, err)
+ return hash.String()
+ }
+
+ commit("backup one", map[string]string{"backups/app/compose.yaml": "services: {}\n"})
+ commit("unrelated", map[string]string{"docs/readme.md": "docs\n"})
+ changed := commit("backup two", map[string]string{"backups/app/compose.yaml": "services: {app: {}}\n"})
+
+ gitClient := NewClient(t.TempDir())
+ entries, err := gitClient.DirectoryHistory(ctx, repoPath, "backups/app", 10)
+ require.NoError(t, err)
+ require.Len(t, entries, 2)
+ assert.Equal(t, changed, entries[0].Hash)
+ assert.Equal(t, []string{"compose.yaml"}, entries[0].Files)
+ assert.Equal(t, "backup two", entries[0].Message)
+ assert.Equal(t, "backup one", entries[1].Message)
+
+ entry, diffs, err := gitClient.CommitDiff(ctx, repoPath, changed, "backups/app")
+ require.NoError(t, err)
+ assert.Equal(t, changed, entry.Hash)
+ require.Len(t, diffs, 1)
+ assert.Equal(t, "compose.yaml", diffs[0].Path)
+ assert.Contains(t, diffs[0].Patch, "services: {app: {}}")
+}
+
+func TestDirectoryHistory_LimitsAndMissingHead(t *testing.T) {
+ ctx := t.Context()
+ repoPath := t.TempDir()
+ _, err := gogit.PlainInit(repoPath, false)
+ require.NoError(t, err)
+
+ gitClient := NewClient(t.TempDir())
+ entries, err := gitClient.DirectoryHistory(ctx, repoPath, "backups/app", 10)
+ require.NoError(t, err)
+ assert.Empty(t, entries)
+
+ _, _, err = gitClient.CommitDiff(ctx, repoPath, "0000000000000000000000000000000000000000", "backups/app")
+ require.Error(t, err)
+ assert.False(t, errors.Is(err, context.Canceled))
+}
diff --git a/backend/resources/migrations/postgres/085_add_gitops_backup_mode.sql b/backend/resources/migrations/postgres/085_add_gitops_backup_mode.sql
new file mode 100644
index 000000000..4337bba36
--- /dev/null
+++ b/backend/resources/migrations/postgres/085_add_gitops_backup_mode.sql
@@ -0,0 +1,25 @@
+-- +goose Up
+ALTER TABLE gitops_syncs ADD COLUMN mode TEXT NOT NULL DEFAULT 'deploy';
+ALTER TABLE gitops_syncs ADD COLUMN backup_directory TEXT NOT NULL DEFAULT '';
+ALTER TABLE gitops_syncs ADD COLUMN backup_paths TEXT;
+ALTER TABLE gitops_syncs ADD COLUMN backup_on_save BOOLEAN NOT NULL DEFAULT true;
+ALTER TABLE gitops_syncs ADD COLUMN backup_pending BOOLEAN NOT NULL DEFAULT false;
+ALTER TABLE gitops_syncs ADD COLUMN backup_pending_since TIMESTAMPTZ;
+ALTER TABLE gitops_syncs ADD COLUMN backup_conflict BOOLEAN NOT NULL DEFAULT false;
+ALTER TABLE gitops_syncs ADD COLUMN backup_failure_reason TEXT;
+ALTER TABLE gitops_syncs ADD COLUMN last_backup_at TIMESTAMPTZ;
+ALTER TABLE gitops_syncs ADD COLUMN last_backup_snapshot TEXT;
+CREATE UNIQUE INDEX idx_gitops_syncs_backup_project ON gitops_syncs(project_id) WHERE mode = 'backup';
+
+-- +goose Down
+DROP INDEX IF EXISTS idx_gitops_syncs_backup_project;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS last_backup_snapshot;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS last_backup_at;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS backup_failure_reason;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS backup_conflict;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS backup_pending_since;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS backup_pending;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS backup_on_save;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS backup_paths;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS backup_directory;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS mode;
diff --git a/backend/resources/migrations/sqlite/085_add_gitops_backup_mode.sql b/backend/resources/migrations/sqlite/085_add_gitops_backup_mode.sql
new file mode 100644
index 000000000..a427dc90d
--- /dev/null
+++ b/backend/resources/migrations/sqlite/085_add_gitops_backup_mode.sql
@@ -0,0 +1,25 @@
+-- +goose Up
+ALTER TABLE gitops_syncs ADD COLUMN mode TEXT NOT NULL DEFAULT 'deploy';
+ALTER TABLE gitops_syncs ADD COLUMN backup_directory TEXT NOT NULL DEFAULT '';
+ALTER TABLE gitops_syncs ADD COLUMN backup_paths TEXT;
+ALTER TABLE gitops_syncs ADD COLUMN backup_on_save BOOLEAN NOT NULL DEFAULT true;
+ALTER TABLE gitops_syncs ADD COLUMN backup_pending BOOLEAN NOT NULL DEFAULT false;
+ALTER TABLE gitops_syncs ADD COLUMN backup_pending_since DATETIME;
+ALTER TABLE gitops_syncs ADD COLUMN backup_conflict BOOLEAN NOT NULL DEFAULT false;
+ALTER TABLE gitops_syncs ADD COLUMN backup_failure_reason TEXT;
+ALTER TABLE gitops_syncs ADD COLUMN last_backup_at DATETIME;
+ALTER TABLE gitops_syncs ADD COLUMN last_backup_snapshot TEXT;
+CREATE UNIQUE INDEX idx_gitops_syncs_backup_project ON gitops_syncs(project_id) WHERE mode = 'backup';
+
+-- +goose Down
+DROP INDEX idx_gitops_syncs_backup_project;
+ALTER TABLE gitops_syncs DROP COLUMN last_backup_snapshot;
+ALTER TABLE gitops_syncs DROP COLUMN last_backup_at;
+ALTER TABLE gitops_syncs DROP COLUMN backup_failure_reason;
+ALTER TABLE gitops_syncs DROP COLUMN backup_conflict;
+ALTER TABLE gitops_syncs DROP COLUMN backup_pending_since;
+ALTER TABLE gitops_syncs DROP COLUMN backup_pending;
+ALTER TABLE gitops_syncs DROP COLUMN backup_on_save;
+ALTER TABLE gitops_syncs DROP COLUMN backup_paths;
+ALTER TABLE gitops_syncs DROP COLUMN backup_directory;
+ALTER TABLE gitops_syncs DROP COLUMN mode;
diff --git a/frontend/messages/en.json b/frontend/messages/en.json
index 7be87015c..f4ddc28ff 100644
--- a/frontend/messages/en.json
+++ b/frontend/messages/en.json
@@ -330,7 +330,7 @@
"common_name_placeholder": "Enter a name",
"common_description_placeholder": "Enter a description",
"common_enabled_description": "Enable or disable this resource",
- "common_auto_sync_description": "Automatically sync on a schedule",
+ "common_auto_sync_description": "Sync on a schedule.",
"common_select_placeholder": "Select an option",
"common_edit_description": "Update the details for this resource",
"common_add_description": "Enter the details for the new resource",
@@ -1893,13 +1893,13 @@
"git_sync_branch": "Branch",
"git_sync_compose_path": "Compose File Path",
"git_sync_sync_files": "Sync Files",
- "git_sync_sync_files_description": "Copy sibling files from the compose directory into the managed project. When disabled, Arcane only syncs the selected compose file.",
+ "git_sync_sync_files_description": "Also copy the other files next to the compose file.",
"git_sync_sync_files_locked_hint": "Locked while a pre-deploy hook is configured.",
"git_sync_pull_image_after_sync": "Pull Image After Sync",
- "git_sync_pull_image_after_sync_description": "Pull each service's image right after a sync changes the compose file, even if the project is currently stopped. Without this, images are only pulled when the project is already running (as part of an automatic redeploy) or on a manual Redeploy/Pull.",
- "git_sync_pull_image_after_sync_redundant_description": "Already covered by Redeploy After Sync, which pulls images as part of redeploying.",
+ "git_sync_pull_image_after_sync_description": "Pull images after a change, even while the project is stopped.",
+ "git_sync_pull_image_after_sync_redundant_description": "Covered by Redeploy After Sync.",
"git_sync_redeploy_after_sync": "Redeploy After Sync",
- "git_sync_redeploy_after_sync_description": "Recreate the project's containers with freshly pulled images right after a sync changes the compose file — even if the project is currently stopped, which starts it. This goes further than Pull Image After Sync: it also recreates the running containers, similar to Portainer's \"Re-pull image\" option.",
+ "git_sync_redeploy_after_sync_description": "Recreate containers with fresh images after a change, even while the project is stopped.",
"git_sync_auto_sync": "Auto Sync",
"git_sync_sync_interval": "Sync Interval",
"git_sync_sync_interval_description": "Minutes",
@@ -3553,5 +3553,79 @@
"features_vulnerability_disabled": "Vulnerability management is disabled for this environment. Saved reports and settings are retained.",
"features_unsupported": "Upgrade this environment’s Arcane agent to manage feature availability. Its existing behavior is unchanged.",
"features_unavailable": "Feature settings are unavailable. Check the environment connection and try again.",
- "features_environment_override": "This feature setting is managed by an environment variable. Change FEATURE_VULNERABILITY_MANAGEMENT_ENABLED on this environment to update it."
+ "features_environment_override": "This feature setting is managed by an environment variable. Change FEATURE_VULNERABILITY_MANAGEMENT_ENABLED on this environment to update it.",
+ "history": "History",
+ "destination": "Destination",
+ "changes": "Changes",
+ "conflicts": "Conflicts",
+ "added": "Added",
+ "modified": "Modified",
+ "removed": "Removed",
+ "resolve": "Resolve",
+ "git_backup": "Git Backup",
+ "back_up_now": "Back up now",
+ "back_up_to_git": "Back up to Git",
+ "not_backed_up_to_git": "This project is not backed up to Git",
+ "not_backed_up_yet": "Not backed up yet",
+ "pending_changes": "Pending changes",
+ "backing_up": "Backing up",
+ "backed_up": "Backed up",
+ "needs_attention": "Needs attention",
+ "last_backup": "Last backup",
+ "last_check": "Last check",
+ "backup_started": "Backup started",
+ "backup_failed": "Backup failed",
+ "backup_history": "Backup History",
+ "backup_history_empty": "No backups have been pushed to this repository yet",
+ "backup_history_load_failed": "Failed to load the backup history",
+ "backup_revision_load_failed": "Failed to load this revision",
+ "changed_files_count": "{count} files",
+ "no_file_changes": "No file changes in this revision",
+ "resolve_backup": "Resolve Backup",
+ "backup_preview_load_failed": "Failed to load the backup preview",
+ "backup_files_changed_in_repository": "Backup files changed in the repository",
+ "backup_folder_occupied": "The backup folder already contains other files",
+ "backup_up_to_date": "The repository is up to date",
+ "no_pending_changes": "No pending changes",
+ "use_arcane_files": "Use Arcane files",
+ "use_arcane_files_description": "Arcane's current files replace the backup files in the repository in a new commit. Existing history is kept.",
+ "backup_resolved": "Backup resolved",
+ "backup_resolve_failed": "Failed to resolve the backup",
+ "disconnect_backup_title": "Stop backing up \"{name}\" to Git?",
+ "disconnect_backup_message": "Arcane stops backing this project up to Git. Local files and the repository history are kept.",
+ "disconnect_backup_success": "Git backup disconnected",
+ "disconnect_backup_failed": "Failed to disconnect the Git backup",
+ "direction": "Direction",
+ "deploy_from_git": "Deploy from Git",
+ "deploy_from_git_description": "Configuration lives in the repository; Arcane pulls it in",
+ "back_up_to_git_description": "Configuration lives in Arcane; saved changes are committed to the repository",
+ "deployments": "Deployments",
+ "backups": "Backups",
+ "pull_from_git": "Pull from Git",
+ "backup_state": "Backup state",
+ "backup_completed": "Backup completed",
+ "destination_folder": "Destination folder",
+ "destination_folder_description": "Folder in the repository that receives this project's files.",
+ "destination_folder_locked": "To move the backup somewhere else, disconnect it and connect it again.",
+ "destination_folder_required": "Destination folder is required",
+ "destination_folder_invalid": "Enter a folder inside the repository, without a leading slash",
+ "included_files": "Included files",
+ "included_files_description": "The compose file is always included.",
+ "environment_file": "Environment file",
+ "environment_file_warning": "Environment files often contain secrets. Their contents will be stored in the repository history.",
+ "automatic_backup": "Automatic backup",
+ "automatic_backup_description": "Check for changes on a schedule.",
+ "back_up_on_save": "Back up on save",
+ "back_up_on_save_description": "Commit right after changes are saved in Arcane.",
+ "check_interval": "Check interval",
+ "save_and_back_up": "Save and back up",
+ "project_backup_name": "{name} backup",
+ "project_required": "Select a project",
+ "no_project_files": "No files found for this project",
+ "repository_path": "Repository Path",
+ "every": "every",
+ "minutes": "minutes",
+ "commits_files_to": "Commits {count} file(s) to",
+ "create_new_project": "Create a new project",
+ "link_existing_project_description": "Link an existing project instead of creating one. Its files are replaced from the repository; local .env values are kept."
}
diff --git a/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte b/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
index 119e1f0b6..7d78a6729 100644
--- a/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
+++ b/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
@@ -12,17 +12,29 @@
import { Input } from '#lib/components/ui/input/index.js';
import FileBrowserDialog from '#lib/components/dialogs/file-browser-dialog.svelte';
import GitopsDialogFooter from '#lib/components/dialogs/gitops-dialog-footer.svelte';
+ import { Checkbox } from '#lib/components/ui/checkbox/index.js';
+ import * as RadioGroup from '#lib/components/ui/radio-group/index.js';
+ import { RadioGroup as RadioGroupPrimitive } from 'bits-ui';
+ import { SvelteMap, SvelteSet } from 'svelte/reactivity';
import type {
FileTreeNode,
GitOpsSync,
GitOpsSyncCreateDto,
+ GitOpsSyncMode,
GitOpsSyncUpdateDto,
GitRepository,
BranchInfo
} from '#lib/types/automation.js';
+ import type { Project } from '#lib/types/swarm.js';
+ import type { SearchPaginationSortRequest } from '#lib/types/shared.js';
+ import type { WorkspaceFileEntry } from '#lib/types/workspace.js';
import { gitRepositoryService } from '#lib/services/git-repository-service.js';
import { settingsService } from '#lib/services/settings-service.js';
+ import { projectService } from '#lib/services/project-service.js';
+ import { projectWorkspaceService } from '#lib/services/project-workspace-service.js';
+ import { environmentManagementService } from '#lib/services/env-mgmt-service.js';
import { hasPermission } from '#lib/utils/auth.js';
+ import { cn } from '#lib/utils.js';
import { z } from 'zod/v4';
import { createForm, preventDefault } from '#lib/utils/settings.svelte.js';
@@ -37,6 +49,8 @@
syncToEdit: GitOpsSync | null;
environmentId: string;
targetType?: string;
+ mode?: GitOpsSyncMode;
+ projectId?: string;
onSubmit: (detail: { sync: GitOpsSyncCreateDto | GitOpsSyncUpdateDto; isEditMode: boolean }) => void;
isLoading: boolean;
};
@@ -46,10 +60,19 @@
syncToEdit = $bindable(),
environmentId,
targetType,
+ mode,
+ projectId,
onSubmit,
isLoading
}: GitOpsSyncFormProps = $props();
+ type BackupFileNode = {
+ name: string;
+ path: string;
+ isDirectory: boolean;
+ children: BackupFileNode[];
+ };
+
type GitOpsSyncTargetType = 'project' | 'swarm_stack';
let isEditMode = $derived(!!syncToEdit);
@@ -88,26 +111,54 @@
return value === 'swarm_stack' ? 'swarm_stack' : 'project';
}
- const formSchema = z.object({
- name: z.string().min(1, m.common_name_required()),
- repositoryId: z.string().min(1, m.common_required()),
- branch: z.string().min(1, m.common_required()),
- composePath: z.string().min(1, m.common_required()),
- syncDirectory: z.boolean().default(false),
- pullImageAfterSync: z.boolean().default(false),
- redeployAfterSync: z.boolean().default(false),
- maxSyncFiles: z.coerce.number().int().nonnegative(),
- maxSyncTotalSizeMb: z.coerce.number().int().nonnegative(),
- maxSyncBinarySizeMb: z.coerce.number().int().nonnegative(),
- autoSync: z.boolean().default(true),
- syncInterval: z.number().min(1).default(5),
- preDeployScriptPath: z.string().default(''),
- preDeployRunnerImage: z.string().default(''),
- preDeployTimeoutSec: z.coerce.number().int().positive().default(60),
- preDeployNetworkMode: z.string().default('none'),
- preDeployEnv: z.string().default(''),
- preDeployExtraMounts: z.string().default('')
- });
+ const formSchema = z
+ .object({
+ mode: z.enum(['deploy', 'backup']).default('deploy'),
+ name: z.string().default(''),
+ projectId: z.string().default(''),
+ repositoryId: z.string().min(1, m.common_required()),
+ branch: z.string().min(1, m.common_required()),
+ composePath: z.string().default(''),
+ backupDirectory: z.string().default(''),
+ backupPaths: z.array(z.string()).default([]),
+ backupOnSave: z.boolean().default(true),
+ syncDirectory: z.boolean().default(false),
+ pullImageAfterSync: z.boolean().default(false),
+ redeployAfterSync: z.boolean().default(false),
+ maxSyncFiles: z.coerce.number().int().nonnegative(),
+ maxSyncTotalSizeMb: z.coerce.number().int().nonnegative(),
+ maxSyncBinarySizeMb: z.coerce.number().int().nonnegative(),
+ autoSync: z.boolean().default(true),
+ syncInterval: z.number().min(1).default(5),
+ preDeployScriptPath: z.string().default(''),
+ preDeployRunnerImage: z.string().default(''),
+ preDeployTimeoutSec: z.coerce.number().int().positive().default(60),
+ preDeployNetworkMode: z.string().default('none'),
+ preDeployEnv: z.string().default(''),
+ preDeployExtraMounts: z.string().default('')
+ })
+ .check((ctx) => {
+ const value = ctx.value;
+ if (value.mode === 'backup') {
+ if (!value.projectId.trim()) {
+ ctx.issues.push({ code: 'custom', message: m.project_required(), path: ['projectId'], input: value });
+ }
+ const directory = value.backupDirectory.trim();
+ if (!directory) {
+ ctx.issues.push({ code: 'custom', message: m.destination_folder_required(), path: ['backupDirectory'], input: value });
+ } else if (directory.startsWith('/') || directory.includes('..') || /^[a-zA-Z]:/.test(directory)) {
+ ctx.issues.push({ code: 'custom', message: m.destination_folder_invalid(), path: ['backupDirectory'], input: value });
+ }
+ return;
+ }
+
+ if (!value.name.trim()) {
+ ctx.issues.push({ code: 'custom', message: m.common_name_required(), path: ['name'], input: value });
+ }
+ if (!value.composePath.trim()) {
+ ctx.issues.push({ code: 'custom', message: m.common_required(), path: ['composePath'], input: value });
+ }
+ });
const bytesPerMegabyte = 1024 * 1024;
@@ -148,10 +199,15 @@
preDeployRunnerImage = syncToEdit.preDeployRunnerImage ?? '';
}
return {
+ mode: syncToEdit?.mode ?? mode ?? 'deploy',
name: syncToEdit?.name ?? '',
+ projectId: syncToEdit?.projectId ?? projectId ?? '',
repositoryId: syncToEdit?.repositoryId ?? '',
branch: syncToEdit?.branch ?? 'main',
composePath: syncToEdit?.composePath ?? defaultComposePath,
+ backupDirectory: syncToEdit?.backupDirectory ?? '',
+ backupPaths: syncToEdit?.backupPaths ?? [],
+ backupOnSave: syncToEdit?.backupOnSave ?? true,
syncDirectory: syncToEdit?.syncDirectory ?? false,
pullImageAfterSync: syncToEdit?.pullImageAfterSync ?? false,
redeployAfterSync: syncToEdit?.redeployAfterSync ?? false,
@@ -245,18 +301,246 @@
return branches.find((branch) => branch.isDefault)?.name ?? '';
});
+ const selectedMode = $derived<GitOpsSyncMode>(inputs.mode.value as GitOpsSyncMode);
+ const isBackupMode = $derived(selectedMode === 'backup');
+
+ const directionOptions = [
+ { value: 'deploy', label: m.deploy_from_git(), description: m.deploy_from_git_description() },
+ { value: 'backup', label: m.back_up_to_git(), description: m.back_up_to_git_description() }
+ ] satisfies { value: GitOpsSyncMode; label: string; description: string }[];
+ const selectedDirection = $derived(directionOptions.find((option) => option.value === selectedMode));
+
+ const projectListOptions: SearchPaginationSortRequest = {
+ pagination: { page: 1, limit: 200 },
+ sort: { column: 'name', direction: 'asc' }
+ };
+
+ const projectsQuery = createQuery(() => ({
+ queryKey: queryKeys.projects.list(environmentId, projectListOptions),
+ queryFn: () => projectService.getProjectsForEnvironment(environmentId, projectListOptions),
+ enabled: open,
+ staleTime: 0
+ }));
+ const projects = $derived<Project[]>(projectsQuery.data?.data ?? []);
+ const selectedProjectId = $derived(inputs.projectId.value);
+ const selectedProject = $derived(projects.find((project) => project.id === selectedProjectId));
+ const newProjectOption = '__new__';
+ const projectOptions = $derived(projects.map((project) => ({ value: project.id, label: project.name })));
+ const deployProjectOptions = $derived([{ value: newProjectOption, label: m.create_new_project() }, ...projectOptions]);
+
+ const environmentQuery = createQuery(() => ({
+ queryKey: queryKeys.environments.detail(environmentId),
+ queryFn: () => environmentManagementService.get(environmentId),
+ enabled: open && isBackupMode,
+ staleTime: 0
+ }));
+
+ function slugifyInternal(value: string): string {
+ return value.trim().replace(/\s+/g, '-');
+ }
+
+ const suggestedBackupDirectory = $derived.by(() => {
+ const environmentSegment = slugifyInternal(environmentQuery.data?.name ?? '');
+ const projectSegment = slugifyInternal(selectedProject?.name ?? '');
+ if (!environmentSegment || !projectSegment) return '';
+ return `projects/${environmentSegment}/${projectSegment}`;
+ });
+
+ const effectiveBackupDirectory = $derived(inputs.backupDirectory.value.trim() || suggestedBackupDirectory);
+
+ const workspaceQuery = createQuery(() => {
+ const requestProjectId = selectedProjectId;
+ return {
+ queryKey: queryKeys.projects.workspace(environmentId, requestProjectId),
+ queryFn: () => projectWorkspaceService.getWorkspace(requestProjectId, environmentId),
+ enabled: open && isBackupMode && !!requestProjectId,
+ staleTime: 0
+ };
+ });
+ const workspaceFiles = $derived<WorkspaceFileEntry[]>(workspaceQuery.data?.files ?? []);
+ const loadingWorkspace = $derived(!!selectedProjectId && (workspaceQuery.isPending || workspaceQuery.isFetching));
+
+ const composeFileNames = ['compose.yaml', 'compose.yml', 'docker-compose.yml', 'docker-compose.yaml'];
+
+ function isComposeFileInternal(name: string): boolean {
+ const lowered = name.toLowerCase();
+ return composeFileNames.includes(lowered) || /^(docker-)?compose\..+\.ya?ml$/.test(lowered);
+ }
+
+ function isEnvFileInternal(name: string): boolean {
+ const lowered = name.toLowerCase();
+ return lowered === '.env' || lowered.startsWith('.env.') || lowered.endsWith('.env');
+ }
+
+ function isEnvPathInternal(path: string): boolean {
+ return isEnvFileInternal(path.split('/').pop() ?? '');
+ }
+
+ function sortNodesInternal(nodes: BackupFileNode[]) {
+ nodes.sort((a, b) => (a.isDirectory === b.isDirectory ? a.name.localeCompare(b.name) : a.isDirectory ? -1 : 1));
+ for (const node of nodes) sortNodesInternal(node.children);
+ }
+
+ const fileTree = $derived.by<BackupFileNode[]>(() => {
+ const roots: BackupFileNode[] = [];
+ const byPath: Record<string, BackupFileNode> = {};
+ const entries = [...workspaceFiles].sort((a, b) => a.relativePath.localeCompare(b.relativePath));
+
+ for (const entry of entries) {
+ const segments = entry.relativePath.split('/').filter(Boolean);
+ let parentPath = '';
+ segments.forEach((segment, index) => {
+ const path = parentPath ? `${parentPath}/${segment}` : segment;
+ const isLeaf = index === segments.length - 1;
+ const existing = byPath[path];
+ if (!existing) {
+ const node: BackupFileNode = { name: segment, path, isDirectory: isLeaf ? entry.isDirectory : true, children: [] };
+ byPath[path] = node;
+ const parent = parentPath ? byPath[parentPath] : undefined;
+ if (parent) parent.children.push(node);
+ else roots.push(node);
+ } else if (isLeaf && entry.isDirectory) {
+ existing.isDirectory = true;
+ }
+ parentPath = path;
+ });
+ }
+
+ sortNodesInternal(roots);
+ return roots;
+ });
+
+ const rootFiles = $derived(workspaceFiles.filter((entry) => !entry.isDirectory && !entry.relativePath.includes('/')));
+
+ const entrypointPath = $derived.by(() => {
+ for (const candidate of composeFileNames) {
+ const found = rootFiles.find((entry) => entry.name.toLowerCase() === candidate);
+ if (found) return found.relativePath;
+ }
+ return '';
+ });
+
+ const defaultBackupPaths = $derived(
+ rootFiles.filter((entry) => isComposeFileInternal(entry.name)).map((entry) => entry.relativePath)
+ );
+
+ const initialBackupPaths = $derived.by<ReadonlySet<string>>(() => {
+ const stored = syncToEdit?.projectId === selectedProjectId ? (syncToEdit?.backupPaths ?? []) : [];
+ const paths = new Set(stored.length > 0 ? stored : defaultBackupPaths);
+ if (entrypointPath) paths.add(entrypointPath);
+ return paths;
+ });
+ const selectionOverrides = new SvelteMap<string, SvelteSet<string>>();
+ const selectedPaths = $derived<ReadonlySet<string>>(selectionOverrides.get(selectedProjectId) ?? initialBackupPaths);
+
+ function editableSelectionInternal(): SvelteSet<string> {
+ const existing = selectionOverrides.get(selectedProjectId);
+ if (existing) return existing;
+ const created = new SvelteSet(initialBackupPaths);
+ selectionOverrides.set(selectedProjectId, created);
+ return created;
+ }
+
+ function ancestorSelectedInternal(path: string): boolean {
+ const segments = path.split('/');
+ for (let index = 1; index < segments.length; index++) {
+ if (selectedPaths.has(segments.slice(0, index).join('/'))) return true;
+ }
+ return false;
+ }
+
+ function isNodeCheckedInternal(node: BackupFileNode): boolean {
+ if (selectedPaths.has(node.path)) return true;
+ if (!node.isDirectory && isEnvFileInternal(node.name)) return false;
+ return ancestorSelectedInternal(node.path);
+ }
+
+ function isNodeLockedInternal(node: BackupFileNode): boolean {
+ if (node.path === entrypointPath) return true;
+ if (!node.isDirectory && isEnvFileInternal(node.name)) return false;
+ return ancestorSelectedInternal(node.path);
+ }
+
+ function toggleNodeInternal(node: BackupFileNode, checked: boolean) {
+ if (isNodeLockedInternal(node)) return;
+ const selection = editableSelectionInternal();
+ if (!checked) {
+ selection.delete(node.path);
+ return;
+ }
+ selection.add(node.path);
+ if (!node.isDirectory) return;
+ for (const path of [...selection]) {
+ if (path !== node.path && path.startsWith(`${node.path}/`) && !isEnvPathInternal(path)) {
+ selection.delete(path);
+ }
+ }
+ }
+
+ const backupPaths = $derived([...selectedPaths].sort((a, b) => a.localeCompare(b)));
+ const hasSelectedEnvFile = $derived(backupPaths.some((path) => isEnvPathInternal(path)));
+ const backupNamePlaceholder = $derived(m.project_backup_name({ name: selectedProject?.name ?? '' }));
+ const backupSummaryDestination = $derived(
+ `${selectedRepository?.label ?? ''} · ${selectedBranch} / ${effectiveBackupDirectory}`
+ );
+ const showBackupSummary = $derived(!!selectedProject && !!selectedRepository && backupPaths.length > 0);
+
+ function backupPathsChangedInternal(): boolean {
+ const existing = [...(syncToEdit?.backupPaths ?? [])].sort((a, b) => a.localeCompare(b));
+ return existing.length !== backupPaths.length || existing.some((path, index) => path !== backupPaths[index]);
+ }
+
+ function buildBackupPayloadInternal(data: z.infer<typeof formSchema>): GitOpsSyncCreateDto | GitOpsSyncUpdateDto {
+ const name = data.name.trim() || backupNamePlaceholder;
+ const repositoryId = selectedRepository?.value || data.repositoryId;
+
+ if (!isEditMode || !syncToEdit) {
+ return {
+ name,
+ mode: 'backup',
+ projectId: data.projectId,
+ repositoryId,
+ branch: data.branch,
+ backupDirectory: data.backupDirectory.trim(),
+ backupPaths,
+ backupOnSave: data.backupOnSave,
+ autoSync: data.autoSync,
+ syncInterval: data.syncInterval
+ };
+ }
+
+ const update: GitOpsSyncUpdateDto = {};
+ if (name !== syncToEdit.name) update.name = name;
+ if (repositoryId !== syncToEdit.repositoryId) update.repositoryId = repositoryId;
+ if (data.branch !== syncToEdit.branch) update.branch = data.branch;
+ if (data.autoSync !== syncToEdit.autoSync) update.autoSync = data.autoSync;
+ if (data.syncInterval !== syncToEdit.syncInterval) update.syncInterval = data.syncInterval;
+ if (data.backupOnSave !== syncToEdit.backupOnSave) update.backupOnSave = data.backupOnSave;
+ if (backupPathsChangedInternal()) update.backupPaths = backupPaths;
+ return update;
+ }
+
function handleSubmit() {
inputs.branch.value = selectedBranch;
+ inputs.backupPaths.value = backupPaths;
+ if (!isEditMode && isBackupMode && !inputs.backupDirectory.value.trim()) {
+ inputs.backupDirectory.value = suggestedBackupDirectory;
+ }
const data = form.validate();
if (!data) return;
+ if (data.mode === 'backup') {
+ onSubmit({ sync: buildBackupPayloadInternal(data), isEditMode });
+ return;
+ }
+
const payload: GitOpsSyncCreateDto | GitOpsSyncUpdateDto = {
name: data.name,
repositoryId: selectedRepository?.value || data.repositoryId,
branch: data.branch,
composePath: data.composePath,
targetType: selectedTargetType,
- projectName: data.name,
+ projectName: selectedProject?.name ?? data.name,
syncDirectory: data.syncDirectory,
pullImageAfterSync: data.pullImageAfterSync,
redeployAfterSync: data.redeployAfterSync,
@@ -267,6 +551,10 @@
syncInterval: data.syncInterval
};
+ if (!isEditMode && data.projectId) {
+ (payload as GitOpsSyncCreateDto).projectId = data.projectId;
+ }
+
if (shouldSubmitLifecycleFieldsInternal(data)) {
payload.preDeployScriptPath = data.preDeployScriptPath.trim();
payload.preDeployRunnerImage = data.preDeployRunnerImage.trim();
@@ -296,6 +584,111 @@
</Button>
{/snippet}
+{#snippet backupFileNode(node: BackupFileNode, depth: number)}
+ {@const checked = isNodeCheckedInternal(node)}
+ {@const locked = isNodeLockedInternal(node)}
+ {@const isEnvFile = !node.isDirectory && isEnvFileInternal(node.name)}
+ <li>
+ <div class="flex items-center gap-2 rounded-md py-1" style={`padding-left: ${depth * 1.25 + 0.25}rem`}>
+ <Checkbox
+ id={`backup-path-${node.path}`}
+ {checked}
+ disabled={locked}
+ onCheckedChange={(value) => toggleNodeInternal(node, value === true)}
+ />
+ <Label for={`backup-path-${node.path}`} class="mb-0 flex min-w-0 flex-1 items-center gap-2 text-sm font-normal">
+ <span class={cn('truncate', node.isDirectory && 'font-medium')}>{node.name}{node.isDirectory ? '/' : ''}</span>
+ {#if node.path === entrypointPath}
+ <span class="shrink-0 rounded bg-primary/10 px-1.5 py-0.5 text-[10px] text-primary">{m.common_entrypoint()}</span>
+ {/if}
+ {#if isEnvFile}
+ <span class="shrink-0 rounded bg-amber-500/10 px-1.5 py-0.5 text-[10px] text-amber-600 dark:text-amber-400">
+ {m.environment_file()}
+ </span>
+ {/if}
+ </Label>
+ </div>
+ {#if node.children.length > 0}
+ <ul class="space-y-0.5">
+ {#each node.children as child (child.path)}
+ {@render backupFileNode(child, depth + 1)}
+ {/each}
+ </ul>
+ {/if}
+ </li>
+{/snippet}
+
+{#snippet repositoryAndBranchFields()}
+ <div class="grid gap-3 sm:grid-cols-[minmax(0,1fr)_12rem]">
+ <div class="space-y-2">
+ <Label for="repository">{m.git_sync_repository()}</Label>
+ <Select.Root
+ type="single"
+ value={selectedRepository?.value}
+ onValueChange={(v) => {
+ if (v) {
+ const repo = repositories.find((r) => r.id === v);
+ if (repo) {
+ inputs.repositoryId.value = v;
+ }
+ }
+ }}
+ >
+ <Select.Trigger id="repository" class="w-full" aria-invalid={inputs.repositoryId.error ? 'true' : undefined}>
+ <span>{selectedRepository?.label ?? m.common_select_placeholder()}</span>
+ </Select.Trigger>
+ <Select.Content style="width: var(--bits-select-anchor-width);">
+ {#each repositories as repo (repo.id)}
+ <Select.Item value={repo.id} class="truncate">{repo.name}</Select.Item>
+ {/each}
+ </Select.Content>
+ </Select.Root>
+ {#if inputs.repositoryId.error}
+ <p class="mt-1 text-sm text-red-500">{inputs.repositoryId.error}</p>
+ {/if}
+ </div>
+
+ <div class="space-y-2">
+ <Label for="branch">{m.git_sync_branch()}</Label>
+ {#if loadingBranches}
+ <div class="flex h-10 items-center gap-2 rounded-md border px-3">
+ <Spinner class="size-4" />
+ <span class="text-sm text-muted-foreground">{m.common_loading()}</span>
+ </div>
+ {:else if branches.length > 0}
+ <Select.Root
+ type="single"
+ value={selectedBranch}
+ onValueChange={(v) => {
+ if (v) {
+ inputs.branch.value = v;
+ }
+ }}
+ >
+ <Select.Trigger id="branch" class="w-full" aria-invalid={inputs.branch.error ? 'true' : undefined}>
+ <span>{selectedBranch || m.common_select_placeholder()}</span>
+ </Select.Trigger>
+ <Select.Content style="width: var(--bits-select-anchor-width);">
+ {#each branches as branch (branch.name)}
+ <Select.Item value={branch.name} class="truncate">
+ {branch.name}
+ {#if branch.isDefault}
+ <span class="ml-2 text-xs text-muted-foreground">({m.common_default()})</span>
+ {/if}
+ </Select.Item>
+ {/each}
+ </Select.Content>
+ </Select.Root>
+ {:else}
+ <FormInput type="text" placeholder="main" bind:input={inputs.branch} />
+ {/if}
+ {#if inputs.branch.error}
+ <p class="mt-1 text-sm text-red-500">{inputs.branch.error}</p>
+ {/if}
+ </div>
+ </div>
+{/snippet}
+
<ResponsiveDialog
bind:open
title={isEditMode ? m.git_sync_edit_title() : m.git_sync_add_title()}
@@ -308,8 +701,146 @@
<Spinner class="size-6" />
</div>
{:else}
- <form id="sync-form" onsubmit={preventDefault(handleSubmit)} class="grid gap-4 py-4">
- <div class="space-y-3">
+ <form id="sync-form" onsubmit={preventDefault(handleSubmit)} class="grid gap-5 py-2">
+ {#if isEditMode}
+ <p class="text-xs text-muted-foreground">
+ <span class="font-medium text-foreground">{isBackupMode ? m.back_up_to_git() : m.deploy_from_git()}</span>
+ · {isBackupMode ? m.back_up_to_git_description() : m.deploy_from_git_description()}
+ </p>
+ {:else}
+ <div class="space-y-2">
+ <RadioGroup.Root
+ class="inline-flex max-w-full flex-wrap items-center justify-start gap-1 rounded-lg border border-border/60 bg-muted/40 p-1 text-muted-foreground"
+ value={selectedMode}
+ onValueChange={(value) => (inputs.mode.value = value as GitOpsSyncMode)}
+ aria-label={m.direction()}
+ >
+ {#each directionOptions as option (option.value)}
+ <RadioGroupPrimitive.Item
+ id={`sync-mode-${option.value}`}
+ value={option.value}
+ class="inline-flex items-center justify-center gap-1.5 rounded-md px-3 py-1.5 text-sm font-medium whitespace-nowrap text-muted-foreground ring-offset-background transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none data-[state=checked]:bg-primary/15 data-[state=checked]:text-primary data-[state=checked]:ring-1 data-[state=checked]:ring-primary/30 dark:data-[state=checked]:text-[color-mix(in_oklch,var(--primary)_55%,white)]"
+ >
+ {option.label}
+ </RadioGroupPrimitive.Item>
+ {/each}
+ </RadioGroup.Root>
+ <p class="text-xs text-muted-foreground">{selectedDirection?.description}</p>
+ </div>
+ {/if}
+
+ {#if isBackupMode}
+ <div class="grid gap-3 sm:grid-cols-2">
+ <SelectWithLabel
+ id="backupProject"
+ label={m.project()}
+ value={selectedProjectId}
+ options={projectOptions}
+ disabled={isEditMode}
+ error={inputs.projectId.error}
+ onValueChange={(value) => {
+ if (value) inputs.projectId.value = value;
+ }}
+ />
+
+ <FormInput label={m.git_sync_name()} type="text" placeholder={backupNamePlaceholder} bind:input={inputs.name} />
+ </div>
+
+ {@render repositoryAndBranchFields()}
+
+ <div class="space-y-2">
+ <Label for="backupDirectory">{m.destination_folder()}</Label>
+ {#if isEditMode}
+ <Input id="backupDirectory" type="text" value={inputs.backupDirectory.value} readonly />
+ <p class="text-xs text-muted-foreground">{m.destination_folder_locked()}</p>
+ {:else}
+ <Input
+ id="backupDirectory"
+ type="text"
+ placeholder={suggestedBackupDirectory}
+ bind:value={inputs.backupDirectory.value}
+ aria-invalid={inputs.backupDirectory.error ? 'true' : undefined}
+ />
+ <p class="text-xs text-muted-foreground">{m.destination_folder_description()}</p>
+ {/if}
+ {#if inputs.backupDirectory.error}
+ <p class="text-xs font-medium text-destructive">{inputs.backupDirectory.error}</p>
+ {/if}
+ </div>
+
+ <div class="space-y-2">
+ <Label>{m.included_files()}</Label>
+ <div class="max-h-56 overflow-y-auto rounded-md border border-border/50 p-1.5">
+ {#if !selectedProjectId}
+ <p class="px-1 py-2 text-xs text-muted-foreground">{m.project_required()}</p>
+ {:else if loadingWorkspace}
+ <div class="flex items-center gap-2 px-1 py-2">
+ <Spinner class="size-4" />
+ <span class="text-sm text-muted-foreground">{m.common_loading()}</span>
+ </div>
+ {:else if fileTree.length === 0}
+ <p class="px-1 py-2 text-xs text-muted-foreground">{m.no_project_files()}</p>
+ {:else}
+ <ul class="space-y-0.5">
+ {#each fileTree as node (node.path)}
+ {@render backupFileNode(node, 0)}
+ {/each}
+ </ul>
+ {/if}
+ </div>
+ <p class="text-xs text-muted-foreground">{m.included_files_description()}</p>
+ {#if hasSelectedEnvFile}
+ <p class="text-xs text-amber-600 dark:text-amber-400">{m.environment_file_warning()}</p>
+ {/if}
+ </div>
+
+ <div class="divide-y divide-border/50 border-y border-border/50">
+ <div class="flex items-center justify-between gap-4 py-3">
+ <div class="min-w-0">
+ <Label for="backupAutoSyncSwitch" class="mb-0 text-sm font-medium">{m.automatic_backup()}</Label>
+ <p class="text-xs text-muted-foreground">{m.automatic_backup_description()}</p>
+ </div>
+ <div class="flex shrink-0 items-center gap-3">
+ {#if inputs.autoSync.value}
+ <label class="flex items-center gap-1.5 text-xs text-muted-foreground" for="backupSyncInterval">
+ {m.every()}
+ <Input
+ id="backupSyncInterval"
+ type="number"
+ min="1"
+ class="h-8 w-16 text-center"
+ bind:value={inputs.syncInterval.value}
+ aria-invalid={inputs.syncInterval.error ? 'true' : undefined}
+ />
+ {m.minutes()}
+ </label>
+ {/if}
+ <Switch id="backupAutoSyncSwitch" bind:checked={inputs.autoSync.value} />
+ </div>
+ </div>
+ {#if inputs.syncInterval.error}
+ <p class="pb-2 text-xs font-medium text-destructive">{inputs.syncInterval.error}</p>
+ {/if}
+
+ <div class="flex items-center justify-between gap-4 py-3">
+ <div class="min-w-0">
+ <Label for="backupOnSaveSwitch" class="mb-0 text-sm font-medium">{m.back_up_on_save()}</Label>
+ <p class="text-xs text-muted-foreground">{m.back_up_on_save_description()}</p>
+ </div>
+ <Switch id="backupOnSaveSwitch" bind:checked={inputs.backupOnSave.value} disabled={!inputs.autoSync.value} />
+ </div>
+ </div>
+
+ {#if showBackupSummary}
+ <div class="space-y-1 text-xs text-muted-foreground">
+ <p>
+ {m.commits_files_to({ count: backupPaths.length })}
+ <span class="font-mono break-all text-foreground">{backupSummaryDestination}</span>
+ </p>
+ <p class="font-mono break-all">{backupPaths.join(', ')}</p>
+ </div>
+ {/if}
+ {:else}
<div class="grid gap-3 sm:grid-cols-2">
<FormInput label={m.git_sync_name()} type="text" placeholder={m.common_name_placeholder()} bind:input={inputs.name} />
@@ -321,6 +852,7 @@
onValueChange={(value) => {
const previousDefault = defaultComposePath;
selectedTargetType = value as GitOpsSyncTargetType;
+ if (selectedTargetType === 'swarm_stack') inputs.projectId.value = '';
if (inputs.composePath.value === previousDefault) {
inputs.composePath.value = defaultComposePath;
}
@@ -328,76 +860,21 @@
/>
</div>
- <div class="grid gap-3 sm:grid-cols-[minmax(0,1fr)_12rem]">
- <div class="space-y-1.5">
- <Label for="repository">{m.git_sync_repository()}</Label>
- <Select.Root
- type="single"
- value={selectedRepository?.value}
- onValueChange={(v) => {
- if (v) {
- const repo = repositories.find((r) => r.id === v);
- if (repo) {
- inputs.repositoryId.value = v;
- }
- }
- }}
- >
- <Select.Trigger id="repository" class="w-full" aria-invalid={inputs.repositoryId.error ? 'true' : undefined}>
- <span>{selectedRepository?.label ?? m.common_select_placeholder()}</span>
- </Select.Trigger>
- <Select.Content style="width: var(--bits-select-anchor-width);">
- {#each repositories as repo (repo.id)}
- <Select.Item value={repo.id} class="truncate">{repo.name}</Select.Item>
- {/each}
- </Select.Content>
- </Select.Root>
- {#if inputs.repositoryId.error}
- <p class="mt-1 text-sm text-red-500">{inputs.repositoryId.error}</p>
- {/if}
- </div>
+ {#if !isEditMode && selectedTargetType === 'project'}
+ <SelectWithLabel
+ id="deployProject"
+ label={m.project()}
+ description={m.link_existing_project_description()}
+ value={inputs.projectId.value || newProjectOption}
+ options={deployProjectOptions}
+ error={inputs.projectId.error}
+ onValueChange={(value) => (inputs.projectId.value = value === newProjectOption ? '' : value)}
+ />
+ {/if}
- <div class="space-y-1.5">
- <Label for="branch">{m.git_sync_branch()}</Label>
- {#if loadingBranches}
- <div class="flex h-10 items-center gap-2 rounded-md border px-3">
- <Spinner class="size-4" />
- <span class="text-sm text-muted-foreground">{m.common_loading()}</span>
- </div>
- {:else if branches.length > 0}
- <Select.Root
- type="single"
- value={selectedBranch}
- onValueChange={(v) => {
- if (v) {
- inputs.branch.value = v;
- }
- }}
- >
- <Select.Trigger id="branch" class="w-full" aria-invalid={inputs.branch.error ? 'true' : undefined}>
- <span>{selectedBranch || m.common_select_placeholder()}</span>
- </Select.Trigger>
- <Select.Content style="width: var(--bits-select-anchor-width);">
- {#each branches as branch (branch.name)}
- <Select.Item value={branch.name} class="truncate">
- {branch.name}
- {#if branch.isDefault}
- <span class="ml-2 text-xs text-muted-foreground">({m.common_default()})</span>
- {/if}
- </Select.Item>
- {/each}
- </Select.Content>
- </Select.Root>
- {:else}
- <FormInput type="text" placeholder="main" bind:input={inputs.branch} />
- {/if}
- {#if inputs.branch.error}
- <p class="mt-1 text-sm text-red-500">{inputs.branch.error}</p>
- {/if}
- </div>
- </div>
+ {@render repositoryAndBranchFields()}
- <div class="space-y-1.5">
+ <div class="space-y-2">
<Label for="composePath">{m.git_sync_compose_path()}</Label>
<div class="flex gap-2">
<div class="flex-1">
@@ -410,44 +887,54 @@
{@render BrowseFilesButton('compose')}
</div>
</div>
- </div>
- <div class="grid gap-3 rounded-lg border border-border/70 bg-muted/20 p-3 sm:grid-cols-[minmax(0,1fr)_8rem]">
- <div class="grid gap-3 sm:grid-cols-2">
- <div class="flex items-start gap-3">
- <Switch id="syncDirectorySwitch" bind:checked={inputs.syncDirectory.value} disabled={lockSyncDirectory} />
- <div class="space-y-1">
- <Label for="syncDirectorySwitch" class="mb-0 text-sm leading-none font-medium">{m.git_sync_sync_files()}</Label>
- <p class="text-xs text-muted-foreground">{m.git_sync_sync_files_description()}</p>
- {#if lockSyncDirectory}
- <p class="text-xs text-muted-foreground italic">{m.git_sync_sync_files_locked_hint()}</p>
+ <div class="divide-y divide-border/50 border-y border-border/50">
+ <div class="flex items-center justify-between gap-4 py-3">
+ <div class="min-w-0">
+ <Label for="autoSyncSwitch" class="mb-0 text-sm font-medium">{m.git_sync_auto_sync()}</Label>
+ <p class="text-xs text-muted-foreground">{m.common_auto_sync_description()}</p>
+ {#if inputs.autoSync.error}
+ <p class="text-xs font-medium text-destructive">{inputs.autoSync.error}</p>
{/if}
- {#if inputs.syncDirectory.error}
- <p class="text-xs font-medium text-destructive">{inputs.syncDirectory.error}</p>
+ </div>
+ <div class="flex shrink-0 items-center gap-3">
+ {#if inputs.autoSync.value}
+ <label class="flex items-center gap-1.5 text-xs text-muted-foreground" for="syncInterval">
+ {m.every()}
+ <Input
+ id="syncInterval"
+ type="number"
+ min="1"
+ class="h-8 w-16 text-center"
+ bind:value={inputs.syncInterval.value}
+ aria-invalid={inputs.syncInterval.error ? 'true' : undefined}
+ />
+ {m.minutes()}
+ </label>
{/if}
+ <Switch id="autoSyncSwitch" bind:checked={inputs.autoSync.value} />
</div>
</div>
+ {#if inputs.syncInterval.error}
+ <p class="pb-2 text-xs font-medium text-destructive">{inputs.syncInterval.error}</p>
+ {/if}
- <div class="flex items-start gap-3">
- <Switch id="autoSyncSwitch" bind:checked={inputs.autoSync.value} />
- <div class="space-y-1">
- <Label for="autoSyncSwitch" class="mb-0 text-sm leading-none font-medium">{m.git_sync_auto_sync()}</Label>
- <p class="text-xs text-muted-foreground">{m.common_auto_sync_description()}</p>
- {#if inputs.autoSync.error}
- <p class="text-xs font-medium text-destructive">{inputs.autoSync.error}</p>
+ <div class="flex items-center justify-between gap-4 py-3">
+ <div class="min-w-0">
+ <Label for="syncDirectorySwitch" class="mb-0 text-sm font-medium">{m.git_sync_sync_files()}</Label>
+ <p class="text-xs text-muted-foreground">
+ {lockSyncDirectory ? m.git_sync_sync_files_locked_hint() : m.git_sync_sync_files_description()}
+ </p>
+ {#if inputs.syncDirectory.error}
+ <p class="text-xs font-medium text-destructive">{inputs.syncDirectory.error}</p>
{/if}
</div>
+ <Switch id="syncDirectorySwitch" bind:checked={inputs.syncDirectory.value} disabled={lockSyncDirectory} />
</div>
- <div class="flex items-start gap-3">
- <Switch
- id="pullImageAfterSyncSwitch"
- bind:checked={inputs.pullImageAfterSync.value}
- disabled={inputs.redeployAfterSync.value}
- />
- <div class="space-y-1">
- <Label for="pullImageAfterSyncSwitch" class="mb-0 text-sm leading-none font-medium"
- >{m.git_sync_pull_image_after_sync()}</Label
+ <div class="flex items-center justify-between gap-4 py-3">
+ <div class="min-w-0">
+ <Label for="pullImageAfterSyncSwitch" class="mb-0 text-sm font-medium">{m.git_sync_pull_image_after_sync()}</Label
>
<p class="text-xs text-muted-foreground">
{inputs.redeployAfterSync.value
@@ -458,209 +945,178 @@
<p class="text-xs font-medium text-destructive">{inputs.pullImageAfterSync.error}</p>
{/if}
</div>
+ <Switch
+ id="pullImageAfterSyncSwitch"
+ bind:checked={inputs.pullImageAfterSync.value}
+ disabled={inputs.redeployAfterSync.value}
+ />
</div>
- <div class="flex items-start gap-3">
- <Switch id="redeployAfterSyncSwitch" bind:checked={inputs.redeployAfterSync.value} />
- <div class="space-y-1">
- <Label for="redeployAfterSyncSwitch" class="mb-0 text-sm leading-none font-medium"
- >{m.git_sync_redeploy_after_sync()}</Label
- >
+ <div class="flex items-center justify-between gap-4 py-3">
+ <div class="min-w-0">
+ <Label for="redeployAfterSyncSwitch" class="mb-0 text-sm font-medium">{m.git_sync_redeploy_after_sync()}</Label>
<p class="text-xs text-muted-foreground">{m.git_sync_redeploy_after_sync_description()}</p>
{#if inputs.redeployAfterSync.error}
<p class="text-xs font-medium text-destructive">{inputs.redeployAfterSync.error}</p>
{/if}
</div>
+ <Switch id="redeployAfterSyncSwitch" bind:checked={inputs.redeployAfterSync.value} />
</div>
</div>
- <div class="space-y-2 sm:max-w-32">
- <div class="space-y-1">
- <Label for="syncInterval" class="mb-0 text-sm leading-none font-medium">
- {m.git_sync_sync_interval()}
- </Label>
- <p class="text-xs text-muted-foreground">{m.git_sync_sync_interval_description()}</p>
- </div>
- <Input
- id="syncInterval"
- type="number"
- placeholder="5"
- bind:value={inputs.syncInterval.value}
- aria-invalid={inputs.syncInterval.error ? 'true' : undefined}
- />
- {#if inputs.syncInterval.error}
- <p class="text-xs font-medium text-destructive">{inputs.syncInterval.error}</p>
- {/if}
- </div>
- </div>
-
- <Collapsible.Root class="group/collapsible">
- <Collapsible.Trigger
- type="button"
- class="flex w-full items-start justify-between gap-3 rounded-md px-1 py-1.5 text-left text-sm text-muted-foreground transition-colors hover:text-foreground"
- >
- <span class="min-w-0">
- <span class="block font-medium">{m.git_sync_per_sync_file_limits_title()}</span>
- </span>
- <ArrowRightIcon class="mt-0.5 size-4 shrink-0 transition-transform group-data-[state=open]/collapsible:rotate-90" />
- </Collapsible.Trigger>
- <Collapsible.Content>
- <div class="mt-2 rounded-lg border border-dashed border-border/70 p-3">
- <p class="mb-3 text-xs text-muted-foreground">{m.git_sync_per_sync_file_limits_description()}</p>
- <div class="grid gap-3 sm:grid-cols-3">
- <FormInput
- label={m.git_sync_max_files_label()}
- type="number"
- placeholder="0"
- helpText={m.git_sync_max_files_per_sync_help()}
- bind:input={inputs.maxSyncFiles}
- />
- <FormInput
- label={m.git_sync_max_total_size_label()}
- type="number"
- placeholder="0"
- helpText={m.git_sync_max_total_size_per_sync_help()}
- bind:input={inputs.maxSyncTotalSizeMb}
- />
- <FormInput
- label={m.git_sync_max_binary_size_label()}
- type="number"
- placeholder="0"
- helpText={m.git_sync_max_binary_size_per_sync_help()}
- bind:input={inputs.maxSyncBinarySizeMb}
- />
- </div>
- </div>
- </Collapsible.Content>
- </Collapsible.Root>
-
- {#if lifecycleEnabled && canManageLifecycle && selectedTargetType === 'project'}
<Collapsible.Root class="group/collapsible">
<Collapsible.Trigger
type="button"
- class="flex w-full items-start justify-between gap-3 rounded-md px-1 py-1.5 text-left text-sm text-muted-foreground transition-colors hover:text-foreground"
+ class="flex w-full items-center justify-between gap-3 py-1 text-left text-sm font-medium text-muted-foreground transition-colors hover:text-foreground"
>
- <span class="min-w-0">
- <span class="block font-medium">{m.git_sync_pre_deploy_title()}</span>
- </span>
- <ArrowRightIcon class="mt-0.5 size-4 shrink-0 transition-transform group-data-[state=open]/collapsible:rotate-90" />
+ {m.git_sync_per_sync_file_limits_title()}
+ <ArrowRightIcon class="size-4 shrink-0 transition-transform group-data-[state=open]/collapsible:rotate-90" />
</Collapsible.Trigger>
<Collapsible.Content>
- <div class="mt-2 space-y-3 rounded-lg border border-dashed border-border/70 p-3">
- <Alert.Root
- class="border-amber-500/30 bg-amber-500/5 text-amber-700 dark:text-amber-300 [&>svg]:top-1/2 [&>svg]:-translate-y-1/2"
- >
- <InfoIcon class="size-4" />
- <Alert.Description class="text-xs">
- {m.git_sync_pre_deploy_acknowledgement()}
- </Alert.Description>
- </Alert.Root>
-
- <p class="text-xs text-muted-foreground">{m.git_sync_pre_deploy_description()}</p>
-
- <div class="space-y-1.5">
- <Label for="preDeployScriptPath" class="text-sm font-medium">
- {m.git_sync_pre_deploy_script_path_label()}
- </Label>
- <div class="flex gap-2">
- <div class="flex-1">
- <Input
- id="preDeployScriptPath"
- type="text"
- placeholder={m.git_sync_pre_deploy_script_path_placeholder()}
- bind:value={inputs.preDeployScriptPath.value}
- aria-invalid={inputs.preDeployScriptPath.error ? 'true' : undefined}
- />
- </div>
- {@render BrowseFilesButton('preDeployScript')}
- </div>
- <p class="text-xs text-muted-foreground">{m.git_sync_pre_deploy_script_path_help()}</p>
- {#if inputs.preDeployScriptPath.error}
- <p class="text-xs font-medium text-destructive">{inputs.preDeployScriptPath.error}</p>
- {/if}
- </div>
-
- <FormInput
- label={m.git_sync_pre_deploy_runner_image_label()}
- type="text"
- placeholder={m.git_sync_pre_deploy_runner_image_placeholder()}
- helpText={m.git_sync_pre_deploy_runner_image_help()}
- bind:input={inputs.preDeployRunnerImage}
- />
-
- <div class="grid gap-3 sm:grid-cols-2">
+ <div class="space-y-3 pt-3">
+ <p class="text-xs text-muted-foreground">{m.git_sync_per_sync_file_limits_description()}</p>
+ <div class="grid gap-3 sm:grid-cols-3">
<FormInput
- label={m.git_sync_pre_deploy_timeout_label()}
+ label={m.git_sync_max_files_label()}
type="number"
- placeholder="60"
- helpText={m.git_sync_pre_deploy_timeout_help()}
- bind:input={inputs.preDeployTimeoutSec}
+ placeholder="0"
+ helpText={m.git_sync_max_files_per_sync_help()}
+ bind:input={inputs.maxSyncFiles}
/>
<FormInput
- label={m.resource_network_cap()}
- type="text"
- placeholder={m.git_sync_pre_deploy_network_mode_placeholder()}
- helpText={m.git_sync_pre_deploy_network_mode_help()}
- bind:input={inputs.preDeployNetworkMode}
+ label={m.git_sync_max_total_size_label()}
+ type="number"
+ placeholder="0"
+ helpText={m.git_sync_max_total_size_per_sync_help()}
+ bind:input={inputs.maxSyncTotalSizeMb}
+ />
+ <FormInput
+ label={m.git_sync_max_binary_size_label()}
+ type="number"
+ placeholder="0"
+ helpText={m.git_sync_max_binary_size_per_sync_help()}
+ bind:input={inputs.maxSyncBinarySizeMb}
/>
</div>
-
- <FormInput
- type="textarea"
- label={m.git_sync_pre_deploy_env_label()}
- placeholder={m.git_sync_pre_deploy_env_placeholder()}
- helpText={m.git_sync_pre_deploy_env_help()}
- bind:input={inputs.preDeployEnv}
- class="[&_textarea]:font-mono [&_textarea]:text-xs"
- />
-
- <FormInput
- type="textarea"
- label={m.git_sync_pre_deploy_extra_mounts_label()}
- placeholder={m.git_sync_pre_deploy_extra_mounts_placeholder()}
- helpText={m.git_sync_pre_deploy_extra_mounts_help()}
- bind:input={inputs.preDeployExtraMounts}
- class="[&_textarea]:font-mono [&_textarea]:text-xs"
- />
</div>
</Collapsible.Content>
</Collapsible.Root>
- {:else if lifecycleEnabled && canManageLifecycle}
- <div
- class="flex w-full items-start justify-between gap-3 rounded-md px-1 py-1.5 text-left text-sm text-muted-foreground"
- >
- <span class="min-w-0">
- <span class="block font-medium">{m.git_sync_pre_deploy_title()}</span>
- <span class="text-xs">{m.git_sync_pre_deploy_swarm_unsupported()}</span>
- </span>
- </div>
- {:else if lifecycleEnabled}
- <div
- class="flex w-full items-start justify-between gap-3 rounded-md px-1 py-1.5 text-left text-sm text-muted-foreground"
- >
- <span class="min-w-0">
- <span class="block font-medium">{m.git_sync_pre_deploy_title()}</span>
- <span class="text-xs">{m.git_sync_pre_deploy_managed_hint()}</span>
- </span>
- <span class="mt-0.5 inline-flex shrink-0 items-center gap-1.5 text-xs">
- {#if hookConfigured}
- <CodeIcon class="size-3.5" />
- {m.common_configured()}
- {:else}
- {m.common_not_configured()}
- {/if}
- </span>
- </div>
- {/if}
- <Alert.Root class="border-border/70 bg-muted/20 py-2 text-muted-foreground [&>svg]:top-1/2 [&>svg]:-translate-y-1/2">
- <InfoIcon class="size-4" />
- <Alert.Description class="text-xs">
+ {#if lifecycleEnabled && canManageLifecycle && selectedTargetType === 'project'}
+ <Collapsible.Root class="group/collapsible">
+ <Collapsible.Trigger
+ type="button"
+ class="flex w-full items-center justify-between gap-3 py-1 text-left text-sm font-medium text-muted-foreground transition-colors hover:text-foreground"
+ >
+ {m.git_sync_pre_deploy_title()}
+ <ArrowRightIcon class="size-4 shrink-0 transition-transform group-data-[state=open]/collapsible:rotate-90" />
+ </Collapsible.Trigger>
+ <Collapsible.Content>
+ <div class="space-y-3 pt-3">
+ <Alert.Root
+ class="border-amber-500/30 bg-amber-500/5 text-amber-700 dark:text-amber-300 [&>svg]:top-1/2 [&>svg]:-translate-y-1/2"
+ >
+ <InfoIcon class="size-4" />
+ <Alert.Description class="text-xs">
+ {m.git_sync_pre_deploy_acknowledgement()}
+ </Alert.Description>
+ </Alert.Root>
+
+ <p class="text-xs text-muted-foreground">{m.git_sync_pre_deploy_description()}</p>
+
+ <div class="space-y-2">
+ <Label for="preDeployScriptPath" class="text-sm font-medium">
+ {m.git_sync_pre_deploy_script_path_label()}
+ </Label>
+ <div class="flex gap-2">
+ <div class="flex-1">
+ <Input
+ id="preDeployScriptPath"
+ type="text"
+ placeholder={m.git_sync_pre_deploy_script_path_placeholder()}
+ bind:value={inputs.preDeployScriptPath.value}
+ aria-invalid={inputs.preDeployScriptPath.error ? 'true' : undefined}
+ />
+ </div>
+ {@render BrowseFilesButton('preDeployScript')}
+ </div>
+ <p class="text-xs text-muted-foreground">{m.git_sync_pre_deploy_script_path_help()}</p>
+ {#if inputs.preDeployScriptPath.error}
+ <p class="text-xs font-medium text-destructive">{inputs.preDeployScriptPath.error}</p>
+ {/if}
+ </div>
+
+ <FormInput
+ label={m.git_sync_pre_deploy_runner_image_label()}
+ type="text"
+ placeholder={m.git_sync_pre_deploy_runner_image_placeholder()}
+ helpText={m.git_sync_pre_deploy_runner_image_help()}
+ bind:input={inputs.preDeployRunnerImage}
+ />
+
+ <div class="grid gap-3 sm:grid-cols-2">
+ <FormInput
+ label={m.git_sync_pre_deploy_timeout_label()}
+ type="number"
+ placeholder="60"
+ helpText={m.git_sync_pre_deploy_timeout_help()}
+ bind:input={inputs.preDeployTimeoutSec}
+ />
+ <FormInput
+ label={m.resource_network_cap()}
+ type="text"
+ placeholder={m.git_sync_pre_deploy_network_mode_placeholder()}
+ helpText={m.git_sync_pre_deploy_network_mode_help()}
+ bind:input={inputs.preDeployNetworkMode}
+ />
+ </div>
+
+ <FormInput
+ type="textarea"
+ label={m.git_sync_pre_deploy_env_label()}
+ placeholder={m.git_sync_pre_deploy_env_placeholder()}
+ helpText={m.git_sync_pre_deploy_env_help()}
+ bind:input={inputs.preDeployEnv}
+ class="[&_textarea]:font-mono [&_textarea]:text-xs"
+ />
+
+ <FormInput
+ type="textarea"
+ label={m.git_sync_pre_deploy_extra_mounts_label()}
+ placeholder={m.git_sync_pre_deploy_extra_mounts_placeholder()}
+ helpText={m.git_sync_pre_deploy_extra_mounts_help()}
+ bind:input={inputs.preDeployExtraMounts}
+ class="[&_textarea]:font-mono [&_textarea]:text-xs"
+ />
+ </div>
+ </Collapsible.Content>
+ </Collapsible.Root>
+ {:else if lifecycleEnabled && canManageLifecycle}
+ <p class="text-xs text-muted-foreground">
+ <span class="font-medium">{m.git_sync_pre_deploy_title()}</span> · {m.git_sync_pre_deploy_swarm_unsupported()}
+ </p>
+ {:else if lifecycleEnabled}
+ <p class="flex items-center justify-between gap-3 text-xs text-muted-foreground">
+ <span
+ ><span class="font-medium">{m.git_sync_pre_deploy_title()}</span> · {m.git_sync_pre_deploy_managed_hint()}</span
+ >
+ <span class="inline-flex shrink-0 items-center gap-1.5">
+ {#if hookConfigured}
+ <CodeIcon class="size-3.5" />
+ {m.common_configured()}
+ {:else}
+ {m.common_not_configured()}
+ {/if}
+ </span>
+ </p>
+ {/if}
+
+ <p class="text-xs text-muted-foreground">
{m.webhook_hint_description()}
<a href="/settings/webhooks" class="text-foreground underline">{m.webhook_page_title()}</a>
{m.git_sync_webhook_hint_suffix()}
- </Alert.Description>
- </Alert.Root>
+ </p>
+ {/if}
</form>
{/if}
{/snippet}
@@ -672,7 +1128,13 @@
{#if isLoading}
<Spinner class="mr-2 size-4" />
{/if}
- {isEditMode ? m.common_save_changes() : m.common_add_button({ resource: m.resource_sync_cap() })}
+ {isEditMode
+ ? isBackupMode
+ ? m.common_save()
+ : m.common_save_changes()
+ : isBackupMode
+ ? m.save_and_back_up()
+ : m.common_add_button({ resource: m.resource_sync_cap() })}
</Button>
{/snippet}
</GitopsDialogFooter>
diff --git a/frontend/src/lib/components/gitops/backup-history-dialog.svelte b/frontend/src/lib/components/gitops/backup-history-dialog.svelte
new file mode 100644
index 000000000..f67309787
--- /dev/null
+++ b/frontend/src/lib/components/gitops/backup-history-dialog.svelte
@@ -0,0 +1,116 @@
+<script lang="ts">
+ import ResponsiveDialog from '#lib/components/ui/responsive-dialog/responsive-dialog.svelte';
+ import { ArcaneButton } from '#lib/components/arcane-button/index.js';
+ import { Spinner } from '#lib/components/ui/spinner/index.js';
+ import { createQuery } from '@tanstack/svelte-query';
+ import { queryKeys } from '#lib/query/query-keys.js';
+ import { gitOpsSyncService } from '#lib/services/gitops-sync-service.js';
+ import { formatDateTimeShort } from '#lib/utils/formatting.js';
+ import { m } from '#lib/paraglide/messages.js';
+ import type { GitOpsBackupHistoryEntry, GitOpsSync } from '#lib/types/automation.js';
+
+ let {
+ open = $bindable(false),
+ environmentId,
+ sync
+ }: {
+ open?: boolean;
+ environmentId: string;
+ sync: GitOpsSync | null;
+ } = $props();
+
+ let selectedCommit = $state<string | null>(null);
+
+ const history = createQuery(() => ({
+ queryKey: queryKeys.gitOpsSyncs.backupHistory(environmentId, sync?.id ?? ''),
+ queryFn: () => gitOpsSyncService.getBackupHistory(environmentId, sync!.id),
+ enabled: open && !!sync
+ }));
+
+ const revision = createQuery(() => ({
+ queryKey: queryKeys.gitOpsSyncs.backupRevision(environmentId, sync?.id ?? '', selectedCommit ?? ''),
+ queryFn: () => gitOpsSyncService.getBackupRevision(environmentId, sync!.id, selectedCommit!),
+ enabled: open && !!sync && !!selectedCommit
+ }));
+
+ const entries = $derived<GitOpsBackupHistoryEntry[]>(history.data?.entries ?? []);
+
+ function firstLine(message: string): string {
+ return message.split('\n')[0] ?? message;
+ }
+
+ function handleOpenChange(next: boolean) {
+ if (!next) selectedCommit = null;
+ }
+</script>
+
+<ResponsiveDialog
+ bind:open
+ onOpenChange={handleOpenChange}
+ title={m.backup_history()}
+ description={sync?.name}
+ contentClass="sm:max-w-3xl"
+ class="pb-6"
+>
+ {#if history.isPending}
+ <div class="flex items-center gap-2 py-8 text-sm text-muted-foreground">
+ <Spinner class="size-4" />
+ {m.common_loading()}
+ </div>
+ {:else if history.isError}
+ <p class="py-8 text-sm text-destructive">{m.backup_history_load_failed()}</p>
+ {:else if entries.length === 0}
+ <p class="py-8 text-sm text-muted-foreground">{m.backup_history_empty()}</p>
+ {:else}
+ <ul class="divide-y divide-border/50 rounded-lg border border-border/50">
+ {#each entries as entry (entry.commit)}
+ {@const isSelected = selectedCommit === entry.commit}
+ <li>
+ <button
+ type="button"
+ class="flex w-full flex-col gap-1 px-3 py-2.5 text-left hover:bg-muted/40 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring"
+ aria-expanded={isSelected}
+ onclick={() => (selectedCommit = isSelected ? null : entry.commit)}
+ >
+ <span class="flex flex-wrap items-center gap-x-3 gap-y-1 text-xs text-muted-foreground">
+ <code class="rounded bg-muted px-1.5 py-0.5 font-mono">{entry.commit.slice(0, 7)}</code>
+ <span>{formatDateTimeShort(entry.date)}</span>
+ <span>{entry.author}</span>
+ <span>{m.changed_files_count({ count: entry.files.length })}</span>
+ </span>
+ <span class="text-sm font-medium break-words">{firstLine(entry.message)}</span>
+ </button>
+
+ {#if isSelected}
+ <div class="border-t border-border/50 bg-muted/20 px-3 py-3">
+ {#if revision.isPending}
+ <div class="flex items-center gap-2 text-sm text-muted-foreground">
+ <Spinner class="size-4" />
+ {m.common_loading()}
+ </div>
+ {:else if revision.isError}
+ <p class="text-sm text-destructive">{m.backup_revision_load_failed()}</p>
+ {:else if (revision.data?.diffs ?? []).length === 0}
+ <p class="text-sm text-muted-foreground">{m.no_file_changes()}</p>
+ {:else}
+ <div class="space-y-3">
+ {#each revision.data?.diffs ?? [] as diff (diff.path)}
+ <div class="space-y-1">
+ <p class="font-mono text-xs font-medium break-all">{diff.path}</p>
+ <pre
+ class="overflow-x-auto rounded-md border border-border/50 bg-background p-3 font-mono text-xs leading-relaxed whitespace-pre">{diff.patch}</pre>
+ </div>
+ {/each}
+ </div>
+ {/if}
+ </div>
+ {/if}
+ </li>
+ {/each}
+ </ul>
+ {/if}
+
+ {#snippet footer()}
+ <ArcaneButton action="base" tone="outline" customLabel={m.common_close()} icon={null} onclick={() => (open = false)} />
+ {/snippet}
+</ResponsiveDialog>
diff --git a/frontend/src/lib/components/gitops/backup-resolve-dialog.svelte b/frontend/src/lib/components/gitops/backup-resolve-dialog.svelte
new file mode 100644
index 000000000..4cd1ef55d
--- /dev/null
+++ b/frontend/src/lib/components/gitops/backup-resolve-dialog.svelte
@@ -0,0 +1,132 @@
+<script lang="ts">
+ import ResponsiveDialog from '#lib/components/ui/responsive-dialog/responsive-dialog.svelte';
+ import { ArcaneButton } from '#lib/components/arcane-button/index.js';
+ import { Badge } from '#lib/components/ui/badge/index.js';
+ import { Spinner } from '#lib/components/ui/spinner/index.js';
+ import { createQuery, useQueryClient } from '@tanstack/svelte-query';
+ import { queryKeys } from '#lib/query/query-keys.js';
+ import { gitOpsSyncService } from '#lib/services/gitops-sync-service.js';
+ import { handleApiResultWithCallbacks } from '#lib/utils/api.js';
+ import { tryCatch } from '#lib/utils/try-catch.js';
+ import { m } from '#lib/paraglide/messages.js';
+ import { toast } from 'svelte-sonner';
+ import type { GitOpsBackupFileChange, GitOpsSync } from '#lib/types/automation.js';
+
+ let {
+ open = $bindable(false),
+ environmentId,
+ sync,
+ onResolved
+ }: {
+ open?: boolean;
+ environmentId: string;
+ sync: GitOpsSync | null;
+ onResolved?: () => void;
+ } = $props();
+
+ const queryClient = useQueryClient();
+
+ let resolving = $state(false);
+
+ const preview = createQuery(() => ({
+ queryKey: queryKeys.gitOpsSyncs.backupPreview(environmentId, sync?.id ?? ''),
+ queryFn: () => gitOpsSyncService.previewBackup(environmentId, sync!.id),
+ enabled: open && !!sync
+ }));
+
+ const stateLabel = $derived.by(() => {
+ switch (preview.data?.state) {
+ case 'conflict':
+ return m.backup_files_changed_in_repository();
+ case 'destination_occupied':
+ return m.backup_folder_occupied();
+ case 'changes':
+ return m.pending_changes();
+ default:
+ return m.backup_up_to_date();
+ }
+ });
+
+ function changeLabel(change: GitOpsBackupFileChange['change']): string {
+ if (change === 'added') return m.added();
+ if (change === 'removed') return m.removed();
+ return m.modified();
+ }
+
+ function changeVariant(change: GitOpsBackupFileChange['change']): 'green' | 'red' | 'amber' {
+ if (change === 'added') return 'green';
+ if (change === 'removed') return 'red';
+ return 'amber';
+ }
+
+ async function useArcaneFiles() {
+ if (!sync) return;
+ resolving = true;
+ const result = await tryCatch(gitOpsSyncService.resolveBackupConflict(environmentId, sync.id, { strategy: 'use_arcane' }));
+ await handleApiResultWithCallbacks({
+ result,
+ message: m.backup_resolve_failed(),
+ setLoadingState: (value) => (resolving = value),
+ onSuccess: async () => {
+ toast.success(m.backup_resolved());
+ await queryClient.invalidateQueries({ queryKey: queryKeys.gitOpsSyncs.all });
+ open = false;
+ onResolved?.();
+ }
+ });
+ resolving = false;
+ }
+</script>
+
+{#snippet fileList(title: string, items: GitOpsBackupFileChange[])}
+ <div class="space-y-2">
+ <h4 class="text-xs font-semibold tracking-wide text-muted-foreground uppercase">{title}</h4>
+ <ul class="divide-y divide-border/50 rounded-lg border border-border/50">
+ {#each items as item (item.path)}
+ <li class="flex items-center justify-between gap-3 px-3 py-2">
+ <span class="font-mono text-xs break-all">{item.path}</span>
+ <Badge variant={changeVariant(item.change)} size="sm">{changeLabel(item.change)}</Badge>
+ </li>
+ {/each}
+ </ul>
+ </div>
+{/snippet}
+
+<ResponsiveDialog bind:open title={m.resolve_backup()} description={sync?.name} contentClass="sm:max-w-xl" class="pb-6">
+ {#if preview.isPending}
+ <div class="flex items-center gap-2 py-8 text-sm text-muted-foreground">
+ <Spinner class="size-4" />
+ {m.common_loading()}
+ </div>
+ {:else if preview.isError}
+ <p class="py-8 text-sm text-destructive">{m.backup_preview_load_failed()}</p>
+ {:else}
+ <div class="space-y-4">
+ <p class="text-sm font-medium">{stateLabel}</p>
+ <p class="text-sm text-muted-foreground">{m.use_arcane_files_description()}</p>
+
+ {#if (preview.data?.conflicts ?? []).length > 0}
+ {@render fileList(m.conflicts(), preview.data?.conflicts ?? [])}
+ {/if}
+
+ {#if (preview.data?.changes ?? []).length > 0}
+ {@render fileList(m.changes(), preview.data?.changes ?? [])}
+ {:else}
+ <p class="text-sm text-muted-foreground">{m.no_pending_changes()}</p>
+ {/if}
+ </div>
+ {/if}
+
+ {#snippet footer()}
+ <ArcaneButton action="base" tone="outline" customLabel={m.common_cancel()} icon={null} onclick={() => (open = false)} />
+ <ArcaneButton
+ action="base"
+ tone="outline-primary"
+ customLabel={m.use_arcane_files()}
+ icon={null}
+ loading={resolving}
+ disabled={resolving || !sync}
+ onclick={useArcaneFiles}
+ />
+ {/snippet}
+</ResponsiveDialog>
diff --git a/frontend/src/lib/components/gitops/backup-state-badge.svelte b/frontend/src/lib/components/gitops/backup-state-badge.svelte
new file mode 100644
index 000000000..196278dc1
--- /dev/null
+++ b/frontend/src/lib/components/gitops/backup-state-badge.svelte
@@ -0,0 +1,48 @@
+<script lang="ts">
+ import { Badge } from '#lib/components/ui/badge/index.js';
+ import type { BadgeVariant } from '#lib/components/ui/badge/badge.svelte';
+ import type { GitOpsBackupState, GitOpsSync } from '#lib/types/automation.js';
+ import { m } from '#lib/paraglide/messages.js';
+
+ let { sync }: { sync: GitOpsSync } = $props();
+
+ const state = $derived<GitOpsBackupState>(sync.backupState ?? 'never');
+
+ const label = $derived.by(() => {
+ switch (state) {
+ case 'pending':
+ return m.pending_changes();
+ case 'backing_up':
+ return m.backing_up();
+ case 'backed_up':
+ return m.backed_up();
+ case 'paused':
+ return m.paused();
+ case 'failed':
+ return m.common_failed();
+ case 'needs_attention':
+ return m.needs_attention();
+ default:
+ return m.not_backed_up_yet();
+ }
+ });
+
+ const variant = $derived.by<BadgeVariant>(() => {
+ switch (state) {
+ case 'pending':
+ return 'amber';
+ case 'backing_up':
+ return 'blue';
+ case 'backed_up':
+ return 'green';
+ case 'failed':
+ return 'red';
+ case 'needs_attention':
+ return 'orange';
+ default:
+ return 'gray';
+ }
+ });
+</script>
+
+<Badge {variant} size="sm">{label}</Badge>
diff --git a/frontend/src/lib/components/gitops/project-git-backup-summary.svelte b/frontend/src/lib/components/gitops/project-git-backup-summary.svelte
new file mode 100644
index 000000000..396a9a34b
--- /dev/null
+++ b/frontend/src/lib/components/gitops/project-git-backup-summary.svelte
@@ -0,0 +1,180 @@
+<script lang="ts">
+ import { goto } from '$app/navigation';
+ import { createQuery, useQueryClient } from '@tanstack/svelte-query';
+ import { toast } from 'svelte-sonner';
+ import * as DropdownMenu from '#lib/components/ui/dropdown-menu/index.js';
+ import RowActionsMenu from '#lib/components/arcane-table/row-actions-menu.svelte';
+ import { ArcaneButton } from '#lib/components/arcane-button/index.js';
+ import { DetailMetaStrip, DetailSection, type DetailMetaItem } from '#lib/components/resource-detail/index.js';
+ import BackupStateBadge from './backup-state-badge.svelte';
+ import BackupHistoryDialog from './backup-history-dialog.svelte';
+ import BackupResolveDialog from './backup-resolve-dialog.svelte';
+ import { queryKeys } from '#lib/query/query-keys.js';
+ import { gitOpsSyncService } from '#lib/services/gitops-sync-service.js';
+ import { handleApiResultWithCallbacks } from '#lib/utils/api.js';
+ import { tryCatch } from '#lib/utils/try-catch.js';
+ import { confirmAndRun } from '#lib/utils/bulk-actions.js';
+ import { hasPermission } from '#lib/utils/auth.js';
+ import { formatDateTimeShort } from '#lib/utils/formatting.js';
+ import { m } from '#lib/paraglide/messages.js';
+ import { ClockIcon, GitBranchIcon, SettingsIcon, TrashIcon, UploadIcon, ShieldAlertIcon } from '#lib/icons/index.js';
+
+ let {
+ environmentId,
+ projectId,
+ projectName
+ }: {
+ environmentId: string;
+ projectId: string;
+ projectName: string;
+ } = $props();
+
+ const queryClient = useQueryClient();
+
+ let historyOpen = $state(false);
+ let resolveOpen = $state(false);
+ let backingUp = $state(false);
+ let disconnecting = $state(false);
+
+ const backups = createQuery(() => ({
+ queryKey: queryKeys.gitOpsSyncs.projectBackup(environmentId, projectId),
+ queryFn: () =>
+ gitOpsSyncService.getSyncs(environmentId, {
+ pagination: { page: 1, limit: 1 },
+ filters: { projectId, mode: 'backup' }
+ }),
+ enabled: !!environmentId && !!projectId
+ }));
+
+ const sync = $derived(backups.data?.data?.[0] ?? null);
+ const canSync = $derived(hasPermission('gitops:sync', environmentId));
+ const canDelete = $derived(hasPermission('gitops:delete', environmentId));
+ const needsAttention = $derived(sync?.backupState === 'needs_attention');
+ const showError = $derived(!!sync?.lastSyncError && (sync?.backupState === 'failed' || needsAttention));
+ const createHref = $derived(`/environments/${environmentId}/gitops?action=create&mode=backup&projectId=${projectId}`);
+
+ const metaItems = $derived.by<DetailMetaItem[]>(() => {
+ if (!sync) return [];
+ const repository = sync.repository?.name ?? sync.repositoryId;
+ const directory = sync.backupDirectory ? ` / ${sync.backupDirectory}` : '';
+ return [
+ {
+ icon: GitBranchIcon,
+ label: m.destination(),
+ value: `${repository} · ${sync.branch}${directory}`,
+ mono: true
+ },
+ {
+ icon: UploadIcon,
+ label: m.last_backup(),
+ value: sync.lastBackupAt ? formatDateTimeShort(sync.lastBackupAt) : m.common_never()
+ },
+ {
+ icon: ClockIcon,
+ label: m.last_check(),
+ value: sync.lastSyncAt ? formatDateTimeShort(sync.lastSyncAt) : m.common_never()
+ }
+ ];
+ });
+
+ async function invalidate() {
+ await queryClient.invalidateQueries({ queryKey: queryKeys.gitOpsSyncs.all });
+ await queryClient.invalidateQueries({ queryKey: queryKeys.gitOpsSyncs.projectBackup(environmentId, projectId) });
+ }
+
+ async function backUpNow() {
+ if (!sync) return;
+ backingUp = true;
+ const result = await tryCatch(gitOpsSyncService.performSync(environmentId, sync.id));
+ await handleApiResultWithCallbacks({
+ result,
+ message: m.backup_failed(),
+ setLoadingState: (value) => (backingUp = value),
+ onSuccess: async () => {
+ toast.success(m.backup_started());
+ await invalidate();
+ }
+ });
+ backingUp = false;
+ }
+
+ function disconnect() {
+ if (!sync) return;
+ const syncId = sync.id;
+ confirmAndRun({
+ title: m.disconnect_backup_title({ name: projectName }),
+ message: m.disconnect_backup_message(),
+ confirmLabel: m.common_disconnect(),
+ destructive: true,
+ setLoading: (value) => (disconnecting = value),
+ run: () => gitOpsSyncService.deleteSync(environmentId, syncId),
+ failureMessage: m.disconnect_backup_failed(),
+ onSuccess: async () => {
+ toast.success(m.disconnect_backup_success());
+ await invalidate();
+ }
+ });
+ }
+</script>
+
+<DetailSection title={m.git_backup()} icon={GitBranchIcon} class="mb-4 space-y-3 border-t-0 pt-0">
+ {#if !sync}
+ <div class="flex flex-wrap items-center justify-between gap-3 rounded-lg border border-border/50 px-4 py-3">
+ <p class="text-sm text-muted-foreground">{m.not_backed_up_to_git()}</p>
+ <ArcaneButton action="base" tone="outline-primary" href={createHref} icon={UploadIcon} customLabel={m.back_up_to_git()} />
+ </div>
+ {:else}
+ <DetailMetaStrip items={metaItems}>
+ <BackupStateBadge {sync} />
+ </DetailMetaStrip>
+
+ {#if showError}
+ <p class="text-sm break-words text-destructive">{sync.lastSyncError}</p>
+ {/if}
+
+ <div class="flex flex-wrap items-center gap-2">
+ {#if canSync}
+ <ArcaneButton
+ action="base"
+ tone="outline-primary"
+ icon={UploadIcon}
+ customLabel={m.back_up_now()}
+ loading={backingUp}
+ disabled={backingUp || disconnecting}
+ onclick={backUpNow}
+ />
+ {/if}
+
+ <RowActionsMenu>
+ <DropdownMenu.Item onclick={() => (historyOpen = true)}>
+ <ClockIcon class="size-4" />
+ {m.history()}
+ </DropdownMenu.Item>
+
+ <DropdownMenu.Item onclick={() => goto(`/environments/${environmentId}/gitops?action=edit&syncId=${sync.id}`)}>
+ <SettingsIcon class="size-4" />
+ {m.settings()}
+ </DropdownMenu.Item>
+
+ {#if needsAttention && canSync}
+ <DropdownMenu.Item onclick={() => (resolveOpen = true)}>
+ <ShieldAlertIcon class="size-4" />
+ {m.resolve()}
+ </DropdownMenu.Item>
+ {/if}
+
+ {#if canDelete}
+ <DropdownMenu.Separator />
+
+ <DropdownMenu.Item variant="destructive" disabled={disconnecting} onclick={disconnect}>
+ <TrashIcon class="size-4" />
+ {m.common_disconnect()}
+ </DropdownMenu.Item>
+ {/if}
+ </RowActionsMenu>
+ </div>
+ {/if}
+</DetailSection>
+
+<BackupHistoryDialog bind:open={historyOpen} {environmentId} {sync} />
+<BackupResolveDialog bind:open={resolveOpen} {environmentId} {sync} onResolved={invalidate} />
diff --git a/frontend/src/lib/components/ui/command/command-item.svelte b/frontend/src/lib/components/ui/command/command-item.svelte
index 872124fd1..d790ab2c1 100644
--- a/frontend/src/lib/components/ui/command/command-item.svelte
+++ b/frontend/src/lib/components/ui/command/command-item.svelte
@@ -9,7 +9,7 @@
bind:ref
data-slot="command-item"
class={cn(
- "relative flex cursor-default items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-hidden select-none aria-selected:bg-accent aria-selected:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground",
+ "relative flex cursor-default items-center gap-2 rounded-lg px-2 py-1.5 text-sm outline-hidden select-none aria-selected:bg-accent aria-selected:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground",
className
)}
{...restProps}
diff --git a/frontend/src/lib/components/ui/select/select-item.svelte b/frontend/src/lib/components/ui/select/select-item.svelte
index 3931b9170..45817b8ef 100644
--- a/frontend/src/lib/components/ui/select/select-item.svelte
+++ b/frontend/src/lib/components/ui/select/select-item.svelte
@@ -18,7 +18,7 @@
{value}
data-slot="select-item"
class={cn(
- "relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
+ "relative flex w-full cursor-default items-center gap-2 rounded-lg py-1.5 pr-8 pl-2 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='text-'])]:text-muted-foreground *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
className
)}
{...restProps}
diff --git a/frontend/src/lib/query/query-keys.ts b/frontend/src/lib/query/query-keys.ts
index 51bc52e0f..2802b981c 100644
--- a/frontend/src/lib/query/query-keys.ts
+++ b/frontend/src/lib/query/query-keys.ts
@@ -159,7 +159,13 @@ export const queryKeys = {
all: ['gitops-syncs'] as const,
list: (environmentId: string, options: SearchPaginationSortRequest) =>
['gitops-syncs', environmentId, stableSerialize(options)] as const,
- detail: (environmentId: string, syncId: string) => ['gitops-syncs', environmentId, syncId] as const
+ detail: (environmentId: string, syncId: string) => ['gitops-syncs', environmentId, syncId] as const,
+ projectBackup: (environmentId: string, projectId: string) =>
+ ['gitops-syncs', environmentId, 'project-backup', projectId] as const,
+ backupPreview: (environmentId: string, syncId: string) => ['gitops-syncs', environmentId, syncId, 'backup-preview'] as const,
+ backupHistory: (environmentId: string, syncId: string) => ['gitops-syncs', environmentId, syncId, 'backup-history'] as const,
+ backupRevision: (environmentId: string, syncId: string, commit: string) =>
+ ['gitops-syncs', environmentId, syncId, 'backup-revision', commit] as const
},
volumes: {
sizes: (environmentId: string) => ['volume-sizes', environmentId] as const,
diff --git a/frontend/src/lib/services/gitops-sync-service.ts b/frontend/src/lib/services/gitops-sync-service.ts
index 3faab8370..f52e9c9cd 100644
--- a/frontend/src/lib/services/gitops-sync-service.ts
+++ b/frontend/src/lib/services/gitops-sync-service.ts
@@ -8,7 +8,11 @@ import type {
SyncStatus,
BrowseResponse,
ImportGitOpsSyncRequest,
- ImportGitOpsSyncResponse
+ ImportGitOpsSyncResponse,
+ GitOpsBackupPreview,
+ GitOpsBackupHistoryResponse,
+ GitOpsBackupRevision,
+ ResolveGitOpsBackupConflictRequest
} from '#lib/types/automation.js';
import type { Paginated, SearchPaginationSortRequest } from '#lib/types/shared.js';
import { transformPaginationParams } from '#lib/utils/tables.js';
@@ -52,6 +56,30 @@ class GitOpsSyncService extends BaseAPIService {
async importSyncs(environmentId: string, syncs: ImportGitOpsSyncRequest[]): Promise<ImportGitOpsSyncResponse> {
return this.handleResponse(this.api.post(`/environments/${environmentId}/gitops-syncs/import`, syncs));
}
+
+ async previewBackup(environmentId: string, syncId: string): Promise<GitOpsBackupPreview> {
+ return this.handleResponse(this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}/backup/preview`));
+ }
+
+ async getBackupHistory(environmentId: string, syncId: string, limit = 20): Promise<GitOpsBackupHistoryResponse> {
+ return this.handleResponse(
+ this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}/backup/history`, { params: { limit } })
+ );
+ }
+
+ async getBackupRevision(environmentId: string, syncId: string, commit: string): Promise<GitOpsBackupRevision> {
+ return this.handleResponse(
+ this.api.get(`/environments/${environmentId}/gitops-syncs/${syncId}/backup/history/${encodeURIComponent(commit)}`)
+ );
+ }
+
+ async resolveBackupConflict(
+ environmentId: string,
+ syncId: string,
+ request: ResolveGitOpsBackupConflictRequest
+ ): Promise<SyncResult> {
+ return this.handleResponse(this.api.post(`/environments/${environmentId}/gitops-syncs/${syncId}/backup/resolve`, request));
+ }
}
export const gitOpsSyncService = new GitOpsSyncService();
diff --git a/frontend/src/lib/types/automation.ts b/frontend/src/lib/types/automation.ts
index 617c48198..564552325 100644
--- a/frontend/src/lib/types/automation.ts
+++ b/frontend/src/lib/types/automation.ts
@@ -117,11 +117,31 @@ export interface GitRepository {
updatedAt: string;
}
+export type GitOpsSyncMode = 'deploy' | 'backup';
+
+export type GitOpsBackupState = 'never' | 'pending' | 'backing_up' | 'backed_up' | 'paused' | 'failed' | 'needs_attention';
+
+export type GitOpsBackupFailureReason =
+ | 'repository'
+ | 'auth'
+ | 'project_missing'
+ | 'snapshot'
+ | 'unreadable_files'
+ | 'limits'
+ | 'conflict'
+ | 'destination_occupied'
+ | 'push_rejected';
+
export interface GitOpsSyncCreateDto {
name: string;
repositoryId: string;
branch: string;
- composePath: string;
+ composePath?: string;
+ mode?: GitOpsSyncMode;
+ projectId?: string;
+ backupDirectory?: string;
+ backupPaths?: string[];
+ backupOnSave?: boolean;
targetType?: string;
projectName?: string;
autoSync?: boolean;
@@ -147,6 +167,8 @@ export interface GitOpsSyncUpdateDto {
composePath?: string;
targetType?: string;
projectName?: string;
+ backupPaths?: string[];
+ backupOnSave?: boolean;
autoSync?: boolean;
syncInterval?: number;
syncDirectory?: boolean;
@@ -172,6 +194,14 @@ export interface GitOpsSync {
branch: string;
composePath: string;
targetType?: string;
+ mode: GitOpsSyncMode;
+ backupDirectory?: string;
+ backupPaths?: string[];
+ backupOnSave: boolean;
+ backupPending: boolean;
+ backupState?: GitOpsBackupState;
+ backupFailureReason?: GitOpsBackupFailureReason;
+ lastBackupAt?: string;
projectName: string;
projectId?: string;
autoSync: boolean;
@@ -204,6 +234,8 @@ export interface GitOpsSyncCounts {
totalSyncs: number;
activeSyncs: number;
successfulSyncs: number;
+ deploySyncs: number;
+ backupSyncs: number;
}
export interface SyncResult {
@@ -234,6 +266,52 @@ export interface SyncStatus {
lastSyncStatus?: string;
lastSyncError?: string;
lastSyncCommit?: string;
+ mode: GitOpsSyncMode;
+ backupState?: GitOpsBackupState;
+ backupPending: boolean;
+ backupFailureReason?: GitOpsBackupFailureReason;
+ lastBackupAt?: string;
+}
+
+export type GitOpsBackupPreviewState = 'clean' | 'changes' | 'conflict' | 'destination_occupied';
+
+export interface GitOpsBackupFileChange {
+ path: string;
+ change: 'added' | 'modified' | 'removed';
+}
+
+export interface GitOpsBackupPreview {
+ state: GitOpsBackupPreviewState;
+ remoteCommit?: string;
+ changes: GitOpsBackupFileChange[];
+ conflicts: GitOpsBackupFileChange[];
+ files: string[];
+}
+
+export interface GitOpsBackupHistoryEntry {
+ commit: string;
+ author: string;
+ message: string;
+ date: string;
+ files: string[];
+}
+
+export interface GitOpsBackupHistoryResponse {
+ entries: GitOpsBackupHistoryEntry[];
+}
+
+export interface GitOpsBackupFileDiff {
+ path: string;
+ patch: string;
+}
+
+export interface GitOpsBackupRevision {
+ entry: GitOpsBackupHistoryEntry;
+ diffs: GitOpsBackupFileDiff[];
+}
+
+export interface ResolveGitOpsBackupConflictRequest {
+ strategy: 'use_arcane';
}
export interface GitRepositoryTestResponse {
diff --git a/frontend/src/routes/(app)/environments/[id]/gitops/+page.svelte b/frontend/src/routes/(app)/environments/[id]/gitops/+page.svelte
index b67a89a6b..9b7163ef3 100644
--- a/frontend/src/routes/(app)/environments/[id]/gitops/+page.svelte
+++ b/frontend/src/routes/(app)/environments/[id]/gitops/+page.svelte
@@ -4,6 +4,7 @@
GitOpsSync,
GitOpsSyncCounts,
GitOpsSyncCreateDto,
+ GitOpsSyncMode,
GitOpsSyncUpdateDto,
ImportGitOpsSyncRequest
} from '#lib/types/automation.js';
@@ -40,18 +41,40 @@
const syncCountsFallback: GitOpsSyncCounts = {
totalSyncs: 0,
activeSyncs: 0,
- successfulSyncs: 0
+ successfulSyncs: 0,
+ deploySyncs: 0,
+ backupSyncs: 0
};
const syncCounts = $derived(syncs?.counts ?? syncCountsFallback);
let dialogTargetType = $state<string | undefined>(undefined);
+ let dialogMode = $state<GitOpsSyncMode | undefined>(undefined);
+ let dialogProjectId = $state<string | undefined>(undefined);
+
+ function parseSyncModeInternal(value: string | null): GitOpsSyncMode | undefined {
+ return value === 'deploy' || value === 'backup' ? value : undefined;
+ }
afterNavigate(() => {
- if (page.url.searchParams.get('action') !== 'create') return;
- openCreateSyncDialog(page.url.searchParams.get('targetType') ?? undefined);
+ const action = page.url.searchParams.get('action');
+ if (action === 'create') {
+ openCreateSyncDialog(
+ page.url.searchParams.get('targetType') ?? undefined,
+ parseSyncModeInternal(page.url.searchParams.get('mode')),
+ page.url.searchParams.get('projectId') ?? undefined
+ );
+ } else if (action === 'edit') {
+ const syncId = page.url.searchParams.get('syncId');
+ if (!syncId) return;
+ void openEditSyncDialogById(syncId);
+ } else {
+ return;
+ }
+
const newUrl = new URL(page.url.href);
- newUrl.searchParams.delete('action');
- newUrl.searchParams.delete('targetType');
+ for (const param of ['action', 'targetType', 'mode', 'projectId', 'syncId']) {
+ newUrl.searchParams.delete(param);
+ }
void goto(newUrl.toString(), { replaceState: true, reset: false });
});
@@ -68,9 +91,11 @@
});
}
- function openCreateSyncDialog(targetType?: string | Event) {
+ function openCreateSyncDialog(targetType?: string | Event, mode?: GitOpsSyncMode, projectId?: string) {
syncToEdit = null;
dialogTargetType = typeof targetType === 'string' ? targetType : undefined;
+ dialogMode = mode;
+ dialogProjectId = projectId;
syncSession += 1;
isSyncDialogOpen = true;
}
@@ -78,10 +103,27 @@
function openEditSyncDialog(sync: GitOpsSync) {
syncToEdit = sync;
dialogTargetType = undefined;
+ dialogMode = undefined;
+ dialogProjectId = undefined;
syncSession += 1;
isSyncDialogOpen = true;
}
+ async function openEditSyncDialogById(syncId: string) {
+ const loaded = syncs?.data.find((item) => item.id === syncId);
+ if (loaded) {
+ openEditSyncDialog(loaded);
+ return;
+ }
+
+ await handleApiResultWithCallbacks({
+ result: await tryCatch(gitOpsSyncService.getSync(environmentId, syncId)),
+ message: m.common_not_found_title({ resource: m.resource_sync_cap() }),
+ setLoadingState: (value) => (isLoading.edit = value),
+ onSuccess: (sync) => openEditSyncDialog(sync)
+ });
+ }
+
async function handleSyncDialogSubmit(detail: { sync: GitOpsSyncCreateDto | GitOpsSyncUpdateDto; isEditMode: boolean }) {
const { sync, isEditMode } = detail;
const loadingKey = isEditMode ? 'edit' : 'create';
@@ -232,6 +274,8 @@
bind:syncToEdit
{environmentId}
targetType={dialogTargetType}
+ mode={dialogMode}
+ projectId={dialogProjectId}
onSubmit={handleSyncDialogSubmit}
isLoading={isLoading.create || isLoading.edit}
/>
diff --git a/frontend/src/routes/(app)/environments/[id]/gitops/sync-table.svelte b/frontend/src/routes/(app)/environments/[id]/gitops/sync-table.svelte
index 886c66d32..983ea61e5 100644
--- a/frontend/src/routes/(app)/environments/[id]/gitops/sync-table.svelte
+++ b/frontend/src/routes/(app)/environments/[id]/gitops/sync-table.svelte
@@ -5,10 +5,14 @@
import * as DropdownMenu from '#lib/components/ui/dropdown-menu/index.js';
import RowActionsMenu from '#lib/components/arcane-table/row-actions-menu.svelte';
import RemoveMenuItem from '#lib/components/arcane-table/cells/remove-menu-item.svelte';
+ import BackupStateBadge from '#lib/components/gitops/backup-state-badge.svelte';
+ import BackupHistoryDialog from '#lib/components/gitops/backup-history-dialog.svelte';
+ import BackupResolveDialog from '#lib/components/gitops/backup-resolve-dialog.svelte';
import { toast } from 'svelte-sonner';
import { handleApiResultWithCallbacks } from '#lib/utils/api.js';
import { tryCatch } from '#lib/utils/try-catch.js';
- import type { Paginated, SearchPaginationSortRequest } from '#lib/types/shared.js';
+ import { hasPermission } from '#lib/utils/auth.js';
+ import type { FilterMap, Paginated, SearchPaginationSortRequest } from '#lib/types/shared.js';
import type { GitOpsSync } from '#lib/types/automation.js';
import type { ColumnSpec, BulkAction, ArcaneRow } from '#lib/components/arcane-table/index.js';
import { UniversalMobileCard } from '#lib/components/arcane-table/index.js';
@@ -16,6 +20,7 @@
import { m } from '#lib/paraglide/messages.js';
import { gitOpsSyncService } from '#lib/services/gitops-sync-service.js';
import { toGitRouteUrl } from '#lib/utils/navigation.js';
+ import { cn } from '#lib/utils.js';
import {
EditIcon as PencilIcon,
StartIcon as PlayIcon,
@@ -23,7 +28,12 @@
RefreshIcon as RefreshCwIcon,
GitBranchIcon,
ProjectsIcon as FolderIcon,
- HashIcon
+ HashIcon,
+ UploadIcon,
+ DownloadIcon,
+ ClockIcon,
+ SettingsIcon,
+ AlertTriangleIcon
} from '#lib/icons/index.js';
import { bulkConfirmAndRun, confirmAndRun } from '#lib/utils/bulk-actions.js';
@@ -43,11 +53,49 @@
onEditSync: (sync: GitOpsSync) => void;
} = $props();
+ const canSync = $derived(hasPermission('gitops:sync', environmentId));
+ const canDelete = $derived(hasPermission('gitops:delete', environmentId));
+
let isLoading = $state({
removing: false,
syncing: false
});
let mobileFieldVisibility = $state<Record<string, boolean>>({});
+ let backupHistoryOpen = $state(false);
+ let backupResolveOpen = $state(false);
+ let backupDialogSync = $state<GitOpsSync | null>(null);
+
+ const modeOptions = [
+ { value: '', label: m.common_all() },
+ { value: 'deploy', label: m.deployments() },
+ { value: 'backup', label: m.backups() }
+ ];
+
+ const activeMode = $derived(String(requestOptions?.filters?.['mode'] ?? ''));
+
+ async function reloadSyncs() {
+ await handleApiResultWithCallbacks({
+ result: await tryCatch(gitOpsSyncService.getSyncs(environmentId, requestOptions)),
+ message: m.common_refresh_failed({ resource: m.git_syncs_title() }),
+ setLoadingState: () => {},
+ onSuccess: (next) => {
+ syncs = next;
+ }
+ });
+ }
+
+ async function selectMode(mode: string) {
+ const filters: FilterMap = { ...(requestOptions?.filters ?? {}) };
+ if (mode) filters['mode'] = mode;
+ else delete filters['mode'];
+
+ requestOptions = {
+ ...requestOptions,
+ filters: Object.keys(filters).length > 0 ? filters : undefined,
+ pagination: { page: 1, limit: requestOptions?.pagination?.limit ?? 20 }
+ };
+ await reloadSyncs();
+ }
function getProjectDetailsUrl(projectId: string): string {
const params = new URLSearchParams({
@@ -58,6 +106,20 @@
return `/projects/${projectId}?${params.toString()}`;
}
+ function isBackup(sync: GitOpsSync): boolean {
+ return sync.mode === 'backup';
+ }
+
+ function openBackupHistory(sync: GitOpsSync) {
+ backupDialogSync = sync;
+ backupHistoryOpen = true;
+ }
+
+ function openBackupResolve(sync: GitOpsSync) {
+ backupDialogSync = sync;
+ backupResolveOpen = true;
+ }
+
async function handleDeleteSelected(ids: string[]) {
bulkConfirmAndRun({
ids,
@@ -103,18 +165,32 @@
});
}
- async function handlePerformSync(id: string, _name: string) {
+ async function handleDisconnectBackup(sync: GitOpsSync) {
+ confirmAndRun({
+ title: m.disconnect_backup_title({ name: sync.projectName || sync.name }),
+ message: m.disconnect_backup_message(),
+ confirmLabel: m.common_disconnect(),
+ destructive: true,
+ setLoading: (loading) => (isLoading.removing = loading),
+ run: () => gitOpsSyncService.deleteSync(environmentId, sync.id),
+ failureMessage: m.disconnect_backup_failed(),
+ onSuccess: async () => {
+ toast.success(m.disconnect_backup_success());
+ await reloadSyncs();
+ }
+ });
+ }
+
+ async function handlePerformSync(sync: GitOpsSync) {
isLoading.syncing = true;
- const result = await tryCatch(gitOpsSyncService.performSync(environmentId, id));
+ const result = await tryCatch(gitOpsSyncService.performSync(environmentId, sync.id));
await handleApiResultWithCallbacks({
result,
- message: m.git_sync_failed(),
+ message: isBackup(sync) ? m.backup_failed() : m.git_sync_failed(),
setLoadingState: () => {},
- onSuccess: () => {
- toast.success(m.git_sync_success());
- gitOpsSyncService.getSyncs(environmentId, requestOptions).then((newSyncs) => {
- syncs = newSyncs;
- });
+ onSuccess: async () => {
+ toast.success(isBackup(sync) ? m.backup_completed() : m.git_sync_success());
+ await reloadSyncs();
}
});
isLoading.syncing = false;
@@ -128,6 +204,12 @@
sortable: true,
cell: NameCell
},
+ {
+ accessorKey: 'mode',
+ title: m.direction(),
+ sortable: true,
+ cell: DirectionCell
+ },
{
accessorKey: 'branch',
title: m.git_sync_branch(),
@@ -136,7 +218,7 @@
},
{
accessorKey: 'composePath',
- title: m.git_sync_compose_path(),
+ title: m.repository_path(),
sortable: true,
cell: PathCell
},
@@ -169,10 +251,12 @@
const mobileFields = [
{ id: 'id', label: m.common_id(), defaultVisible: false },
{ id: 'name', label: m.git_sync_name(), defaultVisible: true },
+ { id: 'mode', label: m.direction(), defaultVisible: true },
{ id: 'branch', label: m.git_sync_branch(), defaultVisible: true },
- { id: 'composePath', label: m.git_sync_compose_path(), defaultVisible: true },
+ { id: 'composePath', label: m.repository_path(), defaultVisible: true },
{ id: 'autoSync', label: m.git_sync_auto_sync(), defaultVisible: true },
{ id: 'lastSyncStatus', label: m.git_sync_status(), defaultVisible: true },
+ { id: 'backupState', label: m.backup_state(), defaultVisible: true },
{ id: 'lastSyncCommit', label: m.commit(), defaultVisible: false },
{ id: 'lastSyncAt', label: m.git_sync_last_sync(), defaultVisible: true }
];
@@ -203,6 +287,18 @@
</span>
{/snippet}
+{#snippet DirectionCell({ item }: { value: any; item: GitOpsSync; row: ArcaneRow<GitOpsSync> })}
+ <span class="inline-flex items-center gap-1.5 text-sm">
+ {#if isBackup(item)}
+ <UploadIcon class="size-3.5 text-muted-foreground" />
+ {m.back_up_to_git()}
+ {:else}
+ <DownloadIcon class="size-3.5 text-muted-foreground" />
+ {m.deploy_from_git()}
+ {/if}
+ </span>
+{/snippet}
+
{#snippet BranchCell({ value }: { value: any; item: GitOpsSync; row: ArcaneRow<GitOpsSync> })}
<div class="flex items-center gap-1.5">
<GitBranchIcon class="size-3.5 text-muted-foreground" />
@@ -211,20 +307,24 @@
{/snippet}
{#snippet PathCell({ value, item }: { value: any; item: GitOpsSync; row: ArcaneRow<GitOpsSync> })}
- {@const fileUrl = item.repository?.url ? toGitRouteUrl(item.repository.url, 'blob', item.branch, String(value)) : null}
+ {@const path = isBackup(item) ? (item.backupDirectory ?? '') : String(value ?? '')}
+ {@const route = isBackup(item) ? 'tree' : 'blob'}
+ {@const fileUrl = item.repository?.url && path ? toGitRouteUrl(item.repository.url, route, item.branch, path) : null}
<div class="flex items-center gap-1.5">
<FolderIcon class="size-3.5 text-muted-foreground" />
- {#if fileUrl}
+ {#if !path}
+ <span class="text-sm text-muted-foreground">{m.common_na()}</span>
+ {:else if fileUrl}
<a
href={fileUrl}
target="_blank"
rel="noopener noreferrer"
class="rounded bg-muted px-2 py-0.5 font-mono text-xs text-muted-foreground transition-colors hover:text-primary"
>
- {value}
+ {path}
</a>
{:else}
- <code class="rounded bg-muted px-2 py-0.5 text-xs text-muted-foreground">{value}</code>
+ <code class="rounded bg-muted px-2 py-0.5 text-xs text-muted-foreground">{path}</code>
{/if}
</div>
{/snippet}
@@ -233,8 +333,10 @@
<Badge variant={value ? 'blue' : 'gray'} minWidth="20">{value ? m.common_enabled() : m.common_disabled()}</Badge>
{/snippet}
-{#snippet StatusCell({ value }: { value: any; item: GitOpsSync; row: ArcaneRow<GitOpsSync> })}
- {#if value === 'success'}
+{#snippet StatusCell({ value, item }: { value: any; item: GitOpsSync; row: ArcaneRow<GitOpsSync> })}
+ {#if isBackup(item)}
+ <BackupStateBadge sync={item} />
+ {:else if value === 'success'}
<Badge variant="green" minWidth="20">{m.common_success()}</Badge>
{:else if value === 'failed'}
<Badge variant="red" minWidth="20">{m.common_failed()}</Badge>
@@ -269,8 +371,35 @@
{/if}
{/snippet}
-{#snippet LastSyncCell({ value }: { value: any; item: GitOpsSync; row: ArcaneRow<GitOpsSync> })}
- <span class="text-sm">{value ? formatDateTimeShort(value) : m.common_never()}</span>
+{#snippet LastSyncCell({ value, item }: { value: any; item: GitOpsSync; row: ArcaneRow<GitOpsSync> })}
+ {@const timestamp = isBackup(item) ? item.lastBackupAt : value}
+ <span class="text-sm">{timestamp ? formatDateTimeShort(timestamp) : m.common_never()}</span>
+{/snippet}
+
+{#snippet ModeFilter()}
+ <div
+ class="inline-flex items-center gap-0.5 rounded-lg border border-border/60 bg-muted/40 p-0.5"
+ role="group"
+ aria-label={m.direction()}
+ >
+ {#each modeOptions as option (option.value)}
+ <button
+ type="button"
+ aria-pressed={activeMode === option.value}
+ class={cn(
+ 'inline-flex h-7 items-center justify-center rounded-md px-2.5 text-xs font-medium text-muted-foreground transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none',
+ activeMode === option.value && 'bg-primary/15 text-primary ring-1 ring-primary/30'
+ )}
+ onclick={() => selectMode(option.value)}
+ >
+ {option.label}
+ </button>
+ {/each}
+ </div>
+{/snippet}
+
+{#snippet BackupStateField(value: GitOpsSync)}
+ <BackupStateBadge sync={value} />
{/snippet}
{#snippet SyncMobileCardSnippet({ item, mobileFieldVisibility }: { item: GitOpsSync; mobileFieldVisibility: FieldVisibility })}
@@ -281,6 +410,13 @@
subtitle={(item) => ((mobileFieldVisibility['id'] ?? false) ? item.id : item.branch)}
badges={[{ variant: 'purple' as const, text: m.resource_sync_cap() }]}
fields={[
+ {
+ label: m.direction(),
+ getValue: (item: GitOpsSync) => (isBackup(item) ? m.back_up_to_git() : m.deploy_from_git()),
+ icon: isBackup(item) ? UploadIcon : DownloadIcon,
+ iconVariant: 'gray' as const,
+ show: mobileFieldVisibility['mode'] ?? true
+ },
{
label: m.git_sync_branch(),
getValue: (item: GitOpsSync) => item.branch,
@@ -289,11 +425,20 @@
show: mobileFieldVisibility['branch'] ?? true
},
{
- label: m.git_sync_compose_path(),
- getValue: (item: GitOpsSync) => item.composePath,
+ label: m.repository_path(),
+ getValue: (item: GitOpsSync) => (isBackup(item) ? (item.backupDirectory ?? m.common_na()) : item.composePath),
icon: FolderIcon,
iconVariant: 'gray' as const,
show: mobileFieldVisibility['composePath'] ?? true
+ },
+ {
+ label: m.backup_state(),
+ getValue: (item: GitOpsSync) => item,
+ type: 'component' as const,
+ component: BackupStateField,
+ icon: UploadIcon,
+ iconVariant: 'gray' as const,
+ show: isBackup(item) && (mobileFieldVisibility['backupState'] ?? true)
}
]}
rowActions={RowActions}
@@ -302,17 +447,51 @@
{#snippet RowActions({ item }: { item: GitOpsSync })}
<RowActionsMenu>
- <DropdownMenu.Item onclick={() => handlePerformSync(item.id, item.name)} disabled={isLoading.syncing}>
- <PlayIcon class="size-4" />
- {m.git_sync_perform()}
- </DropdownMenu.Item>
+ {#if isBackup(item)}
+ <DropdownMenu.Item onclick={() => handlePerformSync(item)} disabled={isLoading.syncing || !canSync}>
+ <UploadIcon class="size-4" />
+ {m.back_up_now()}
+ </DropdownMenu.Item>
- <DropdownMenu.Item onclick={() => onEditSync(item)}>
- <PencilIcon class="size-4" />
- {m.common_edit()}
- </DropdownMenu.Item>
+ <DropdownMenu.Item onclick={() => openBackupHistory(item)}>
+ <ClockIcon class="size-4" />
+ {m.history()}
+ </DropdownMenu.Item>
- <RemoveMenuItem onclick={() => handleDeleteOne(item.id, item.name)} disabled={isLoading.removing} />
+ {#if item.backupState === 'needs_attention' && canSync}
+ <DropdownMenu.Item onclick={() => openBackupResolve(item)}>
+ <AlertTriangleIcon class="size-4" />
+ {m.resolve()}
+ </DropdownMenu.Item>
+ {/if}
+
+ <DropdownMenu.Item onclick={() => onEditSync(item)}>
+ <SettingsIcon class="size-4" />
+ {m.settings()}
+ </DropdownMenu.Item>
+
+ {#if canDelete}
+ <RemoveMenuItem
+ onclick={() => handleDisconnectBackup(item)}
+ disabled={isLoading.removing}
+ label={m.common_disconnect()}
+ />
+ {/if}
+ {:else}
+ <DropdownMenu.Item onclick={() => handlePerformSync(item)} disabled={isLoading.syncing}>
+ <PlayIcon class="size-4" />
+ {m.pull_from_git()}
+ </DropdownMenu.Item>
+
+ <DropdownMenu.Item onclick={() => onEditSync(item)}>
+ <PencilIcon class="size-4" />
+ {m.common_edit()}
+ </DropdownMenu.Item>
+
+ {#if canDelete}
+ <RemoveMenuItem onclick={() => handleDeleteOne(item.id, item.name)} disabled={isLoading.removing} />
+ {/if}
+ {/if}
</RowActionsMenu>
{/snippet}
@@ -326,6 +505,11 @@
onRefresh={async (options) => (syncs = await gitOpsSyncService.getSyncs(environmentId, options))}
{columns}
{mobileFields}
+ customToolbarActions={ModeFilter}
rowActions={RowActions}
mobileCard={SyncMobileCardSnippet}
/>
+
+<BackupHistoryDialog bind:open={backupHistoryOpen} {environmentId} sync={backupDialogSync} />
+
+<BackupResolveDialog bind:open={backupResolveOpen} {environmentId} sync={backupDialogSync} onResolved={reloadSyncs} />
diff --git a/frontend/src/routes/(app)/projects/[projectId]/+page.svelte b/frontend/src/routes/(app)/projects/[projectId]/+page.svelte
index 0329a7eda..66739ac26 100644
--- a/frontend/src/routes/(app)/projects/[projectId]/+page.svelte
+++ b/frontend/src/routes/(app)/projects/[projectId]/+page.svelte
@@ -24,6 +24,7 @@
import { type TabItem } from '#lib/components/tab-bar/index.js';
import TabbedPageLayout from '#lib/layouts/tabbed-page-layout.svelte';
import ActionButtons from '#lib/components/action-buttons.svelte';
+ import ProjectGitBackupSummary from '#lib/components/gitops/project-git-backup-summary.svelte';
import { Badge } from '#lib/components/ui/badge/index.js';
import * as ArcaneTooltip from '#lib/components/arcane-tooltip/index.js';
import { getStatusVariant, getThemedIconUrl } from '#lib/utils/docker.js';
@@ -1472,6 +1473,9 @@
<div class="flex h-full min-h-0 flex-col">
{@render configurationErrorNotice()}
{@render gitSourceNotice()}
+ {#if !isGitOpsManaged}
+ <ProjectGitBackupSummary environmentId={envId} projectId={project.id} projectName={project.name} />
+ {/if}
{@render composeFilesNotice()}
<div class="mb-2 flex shrink-0 items-center justify-end gap-2">
<label for="layout-mode-toggle" class="cursor-pointer text-xs text-muted-foreground" title={m.project_view_description()}>
diff --git a/types/gitops/gitops.go b/types/gitops/gitops.go
index 8af222577..939998dc5 100644
--- a/types/gitops/gitops.go
+++ b/types/gitops/gitops.go
@@ -2,6 +2,43 @@ package gitops
import "time"
+const (
+ // SyncModeDeploy pulls configuration from the repository into Arcane.
+ SyncModeDeploy = "deploy"
+ // SyncModeBackup commits saved project configuration to the repository.
+ SyncModeBackup = "backup"
+)
+
+const (
+ BackupStateNever = "never"
+ BackupStatePending = "pending"
+ BackupStateBackingUp = "backing_up"
+ BackupStateBackedUp = "backed_up"
+ BackupStatePaused = "paused"
+ BackupStateFailed = "failed"
+ BackupStateNeedsAttention = "needs_attention"
+)
+
+const (
+ BackupFailureRepository = "repository"
+ BackupFailureAuth = "auth"
+ BackupFailureProjectMissing = "project_missing"
+ BackupFailureSnapshot = "snapshot"
+ BackupFailureUnreadableFiles = "unreadable_files"
+ BackupFailureLimits = "limits"
+ BackupFailureConflict = "conflict"
+ BackupFailureDestinationOccupied = "destination_occupied"
+ BackupFailurePushRejected = "push_rejected"
+)
+
+const (
+ // BackupConflictUseArcane replaces the remote backup files with Arcane's current files.
+ BackupConflictUseArcane = "use_arcane"
+)
+
+// BackupManifestFileName is the manifest written beside backed-up files.
+const BackupManifestFileName = ".arcane-backup.json"
+
// GitRepository represents a reusable Git repository with credentials.
type GitRepository struct {
// ID of the git repository.
@@ -201,6 +238,36 @@ type GitOpsSync struct {
// Required: true
TargetType string `json:"targetType"`
+ // Mode is the direction of the sync: "deploy" pulls from Git, "backup" commits to Git.
+ //
+ // Required: true
+ Mode string `json:"mode"`
+
+ // BackupDirectory is the repository directory that receives backed-up files.
+ //
+ // Required: false
+ BackupDirectory string `json:"backupDirectory,omitempty"`
+
+ // BackupPaths lists the project-relative files and directories included in backups.
+ //
+ // Required: false
+ BackupPaths []string `json:"backupPaths,omitempty"`
+
+ // BackupState summarizes the backup lifecycle (never, pending, backing_up, backed_up, paused, failed, needs_attention).
+ //
+ // Required: false
+ BackupState string `json:"backupState,omitempty"`
+
+ // BackupFailureReason is the typed reason for the last backup failure.
+ //
+ // Required: false
+ BackupFailureReason *string `json:"backupFailureReason,omitempty"`
+
+ // LastBackupAt is the time the remote last received a new backup commit.
+ //
+ // Required: false
+ LastBackupAt *time.Time `json:"lastBackupAt,omitempty"`
+
// PreDeployTimeoutSec bounds the script execution. Capped by the
// lifecycleMaxTimeoutSec global setting at run time.
//
@@ -255,6 +322,16 @@ type GitOpsSync struct {
//
// Required: true
RedeployAfterSync bool `json:"redeployAfterSync"`
+
+ // BackupOnSave indicates whether saved project changes trigger a backup.
+ //
+ // Required: true
+ BackupOnSave bool `json:"backupOnSave"`
+
+ // BackupPending indicates that saved changes have not reached the repository yet.
+ //
+ // Required: true
+ BackupPending bool `json:"backupPending"`
}
// SyncCounts contains counts of syncs by status within the current filtered set.
@@ -273,6 +350,16 @@ type SyncCounts struct {
//
// Required: true
SuccessfulSyncs int `json:"successfulSyncs"`
+
+ // DeploySyncs is the number of "deploy" mode syncs in the current filtered set.
+ //
+ // Required: true
+ DeploySyncs int `json:"deploySyncs"`
+
+ // BackupSyncs is the number of "backup" mode syncs in the current filtered set.
+ //
+ // Required: true
+ BackupSyncs int `json:"backupSyncs"`
}
// CreateRepositoryRequest represents the request to create a git repository.
@@ -392,9 +479,39 @@ type CreateSyncRequest struct {
Branch string `json:"branch" binding:"required"`
// ComposePath is the path to the docker-compose file in the repository.
+ // Required for "deploy" mode; derived from the backup directory in "backup" mode.
//
- // Required: true
- ComposePath string `json:"composePath" binding:"required"`
+ // Required: false
+ ComposePath string `json:"composePath,omitempty"`
+
+ // Mode selects the sync direction: "deploy" (default) or "backup".
+ //
+ // Required: false
+ Mode string `json:"mode,omitempty" binding:"omitempty,oneof=deploy backup"`
+
+ // ProjectID links an existing project. Required in "backup" mode; optional in
+ // "deploy" mode, where the sync adopts the project instead of creating one.
+ //
+ // Required: false
+ ProjectID string `json:"projectId,omitempty"`
+
+ // BackupDirectory is the repository directory that receives backed-up files.
+ // Required in "backup" mode.
+ //
+ // Required: false
+ BackupDirectory string `json:"backupDirectory,omitempty"`
+
+ // BackupPaths lists project-relative files and directories to back up. Defaults
+ // to the project's compose files when omitted. Environment files are only
+ // included when listed explicitly.
+ //
+ // Required: false
+ BackupPaths []string `json:"backupPaths,omitempty"`
+
+ // BackupOnSave triggers a backup after project changes are saved. Default: true.
+ //
+ // Required: false
+ BackupOnSave *bool `json:"backupOnSave,omitempty"`
// TargetType specifies if this sync targets a "project" or "swarm_stack".
//
@@ -534,6 +651,16 @@ type UpdateSyncRequest struct {
// Required: false
ProjectName *string `json:"projectName,omitzero"`
+ // BackupPaths replaces the backed-up file selection. Omitted or empty leaves it unchanged.
+ //
+ // Required: false
+ BackupPaths []string `json:"backupPaths,omitzero"`
+
+ // BackupOnSave toggles backing up after project changes are saved.
+ //
+ // Required: false
+ BackupOnSave *bool `json:"backupOnSave,omitzero"`
+
// AutoSync indicates if the sync should run automatically.
//
// Required: false
@@ -650,6 +777,40 @@ func (r UpdateSyncRequest) HasPreDeployConfig() bool {
r.PreDeployTimeoutSec != nil
}
+// HasDeploymentOptions reports whether the request sets deployment-only
+// options that a backup sync must reject.
+func (r CreateSyncRequest) HasDeploymentOptions() bool {
+ return (r.TargetType != "" && r.TargetType != "project") ||
+ (r.SyncDirectory != nil && *r.SyncDirectory) ||
+ (r.PullImageAfterSync != nil && *r.PullImageAfterSync) ||
+ (r.RedeployAfterSync != nil && *r.RedeployAfterSync) ||
+ r.HasPreDeployConfig()
+}
+
+// HasDeploymentOptions reports whether the request sets deployment-only
+// options that a backup sync must reject.
+func (r UpdateSyncRequest) HasDeploymentOptions() bool {
+ return (r.TargetType != nil && *r.TargetType != "" && *r.TargetType != "project") ||
+ r.ComposePath != nil ||
+ r.ProjectName != nil ||
+ (r.SyncDirectory != nil && *r.SyncDirectory) ||
+ (r.PullImageAfterSync != nil && *r.PullImageAfterSync) ||
+ (r.RedeployAfterSync != nil && *r.RedeployAfterSync) ||
+ r.HasPreDeployConfig()
+}
+
+// HasBackupOptions reports whether the request sets backup-only options that
+// a deploy sync must reject.
+func (r CreateSyncRequest) HasBackupOptions() bool {
+ return r.BackupDirectory != "" || len(r.BackupPaths) > 0 || r.BackupOnSave != nil
+}
+
+// HasBackupOptions reports whether the request sets backup-only options that
+// a deploy sync must reject.
+func (r UpdateSyncRequest) HasBackupOptions() bool {
+ return len(r.BackupPaths) > 0 || r.BackupOnSave != nil
+}
+
// SyncResult represents the result of a sync operation.
type SyncResult struct {
// Success indicates if the sync was successful.
@@ -860,6 +1021,31 @@ type SyncStatus struct {
//
// Required: false
LastSyncCommit *string `json:"lastSyncCommit,omitempty"`
+
+ // Mode is the sync direction ("deploy" or "backup").
+ //
+ // Required: true
+ Mode string `json:"mode"`
+
+ // BackupState summarizes the backup lifecycle for "backup" syncs.
+ //
+ // Required: false
+ BackupState string `json:"backupState,omitempty"`
+
+ // BackupPending indicates saved changes have not reached the repository yet.
+ //
+ // Required: true
+ BackupPending bool `json:"backupPending"`
+
+ // BackupFailureReason is the typed reason for the last backup failure.
+ //
+ // Required: false
+ BackupFailureReason *string `json:"backupFailureReason,omitempty"`
+
+ // LastBackupAt is the time the remote last received a new backup commit.
+ //
+ // Required: false
+ LastBackupAt *time.Time `json:"lastBackupAt,omitempty"`
}
// ImportGitOpsSyncRequest represents the request to import gitops syncs.
@@ -936,3 +1122,153 @@ type ImportGitOpsSyncResponse struct {
// Required: true
Errors []string `json:"errors"`
}
+
+// BackupManifest is written beside backed-up files so a backup can be
+// recognized and recovered without Arcane's database.
+type BackupManifest struct {
+ // Version of the manifest format.
+ //
+ // Required: true
+ Version int `json:"version"`
+
+ // SyncID is the Arcane sync that produced the backup.
+ //
+ // Required: true
+ SyncID string `json:"syncId"`
+
+ // ProjectName is the name of the backed-up project.
+ //
+ // Required: true
+ ProjectName string `json:"projectName"`
+
+ // EnvironmentID is the Arcane environment that owns the project.
+ //
+ // Required: true
+ EnvironmentID string `json:"environmentId"`
+
+ // GeneratedAt is when the snapshot was taken.
+ //
+ // Required: true
+ GeneratedAt time.Time `json:"generatedAt"`
+
+ // ComposeFiles lists the backed-up Compose entrypoints and overrides.
+ //
+ // Required: true
+ ComposeFiles []string `json:"composeFiles"`
+
+ // Files maps each backed-up path to its SHA-256 content hash.
+ //
+ // Required: true
+ Files map[string]string `json:"files"`
+}
+
+// BackupFileChange describes one file difference between Arcane and the repository.
+type BackupFileChange struct {
+ // Path relative to the backup directory.
+ //
+ // Required: true
+ Path string `json:"path"`
+
+ // Change is "added", "modified", or "removed" from the repository's point of view.
+ //
+ // Required: true
+ Change string `json:"change"`
+}
+
+// BackupPreview describes what the next backup would commit.
+type BackupPreview struct {
+ // State is "clean", "changes", "conflict", or "destination_occupied".
+ //
+ // Required: true
+ State string `json:"state"`
+
+ // RemoteCommit is the current head of the backup branch, when it exists.
+ //
+ // Required: false
+ RemoteCommit string `json:"remoteCommit,omitempty"`
+
+ // Changes lists files the next backup would add, modify, or remove.
+ //
+ // Required: true
+ Changes []BackupFileChange `json:"changes"`
+
+ // Conflicts lists backup-owned files changed in the repository since the last successful backup.
+ //
+ // Required: true
+ Conflicts []BackupFileChange `json:"conflicts"`
+
+ // Files lists every path the snapshot includes.
+ //
+ // Required: true
+ Files []string `json:"files"`
+}
+
+// BackupHistoryEntry is one revision affecting a backup directory.
+type BackupHistoryEntry struct {
+ // Commit hash.
+ //
+ // Required: true
+ Commit string `json:"commit"`
+
+ // Author name of the commit.
+ //
+ // Required: true
+ Author string `json:"author"`
+
+ // Message of the commit.
+ //
+ // Required: true
+ Message string `json:"message"`
+
+ // Date of the commit.
+ //
+ // Required: true
+ Date time.Time `json:"date"`
+
+ // Files changed inside the backup directory, relative to it.
+ //
+ // Required: true
+ Files []string `json:"files"`
+}
+
+// BackupHistoryResponse lists revisions affecting a backup.
+type BackupHistoryResponse struct {
+ // Entries newest first.
+ //
+ // Required: true
+ Entries []BackupHistoryEntry `json:"entries"`
+}
+
+// BackupFileDiff is the unified diff of one file in a revision.
+type BackupFileDiff struct {
+ // Path relative to the backup directory.
+ //
+ // Required: true
+ Path string `json:"path"`
+
+ // Patch is the unified diff text.
+ //
+ // Required: true
+ Patch string `json:"patch"`
+}
+
+// BackupRevision is a revision with its per-file diffs.
+type BackupRevision struct {
+ // Entry describes the commit.
+ //
+ // Required: true
+ Entry BackupHistoryEntry `json:"entry"`
+
+ // Diffs contains one entry per changed file inside the backup directory.
+ //
+ // Required: true
+ Diffs []BackupFileDiff `json:"diffs"`
+}
+
+// ResolveBackupConflictRequest chooses how to resolve a backup that needs attention.
+type ResolveBackupConflictRequest struct {
+ // Strategy is "use_arcane" to replace the repository's backup files with Arcane's.
+ //
+ // Required: true
+ Strategy string `json:"strategy" binding:"required,oneof=use_arcane"`
+}