Skip to content

Modern Docker Management, Designed for Everyone

feat: pull and redeploy images after GitOps sync for stopped projects (#3698)

Ogilf17e84412ae4
diff --git a/backend/internal/gitops/gitops_sync.go b/backend/internal/gitops/gitops_sync.go
index 2567f4c9a..af64068de 100644
--- a/backend/internal/gitops/gitops_sync.go
+++ b/backend/internal/gitops/gitops_sync.go
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json/v2"
"fmt"
+ "io"
"io/fs"
"log/slog"
"maps"
@@ -632,6 +633,12 @@ func (s *GitOpsSyncService) CreateSync(ctx context.Context, environmentID string
if req.SyncDirectory != nil {
syncRecord.SyncDirectory = *req.SyncDirectory
}
+ if req.PullImageAfterSync != nil {
+ syncRecord.PullImageAfterSync = *req.PullImageAfterSync
+ }
+ if req.RedeployAfterSync != nil {
+ syncRecord.RedeployAfterSync = *req.RedeployAfterSync
+ }
if err := validateSyncLimits(req.MaxSyncFiles, req.MaxSyncTotalSize, req.MaxSyncBinarySize); err != nil {
return nil, err
}
@@ -747,6 +754,12 @@ func (s *GitOpsSyncService) UpdateSync(ctx context.Context, environmentID, id st
if req.SyncDirectory != nil {
updates["sync_directory"] = *req.SyncDirectory
}
+ if req.PullImageAfterSync != nil {
+ updates["pull_image_after_sync"] = *req.PullImageAfterSync
+ }
+ if req.RedeployAfterSync != nil {
+ updates["redeploy_after_sync"] = *req.RedeployAfterSync
+ }
if err := validateSyncLimits(req.MaxSyncFiles, req.MaxSyncTotalSize, req.MaxSyncBinarySize); err != nil {
return nil, err
}
@@ -1022,7 +1035,7 @@ func (s *GitOpsSyncService) performDirectorySync(ctx context.Context, sync *proj
}
if contentsChanged {
- if redeployErr := s.redeployIfRunningAfterSync(ctx, project, actor, "directory"); redeployErr != nil {
+ if redeployErr := s.redeployIfRunningAfterSync(ctx, sync, project, actor, "directory"); redeployErr != nil {
if errors.Is(redeployErr, common.ErrRedeployAfterSyncFailed) {
s.markSyncRedeployFailedInternal(ctx, sync, id, source.commitHash, syncedFiles, redeployErr, actor, result)
}
@@ -1150,20 +1163,29 @@ func (s *GitOpsSyncService) performSwarmStackSyncInternal(ctx context.Context, s
return result, nil
}
-// redeployIfRunningAfterSync redeploys a project only when it is already
-// running and the latest sync actually changed managed content. Returns a
-// common.ErrRedeployAfterSyncFailed when the redeploy itself fails; callers
-// surface that on the sync row's LastSyncError.
-func (s *GitOpsSyncService) redeployIfRunningAfterSync(ctx context.Context, project *projectpkg.Project, actor common.User, syncMode string) error {
+// redeployIfRunningAfterSync redeploys a project when it is already running,
+// or when the sync has RedeployAfterSync enabled (in which case a stopped
+// project is redeployed too, mirroring Portainer's "Re-pull image" stack
+// option). Returns a common.ErrRedeployAfterSyncFailed when the redeploy
+// itself fails; callers surface that on the sync row's LastSyncError.
+//
+// When neither condition holds — project stopped and RedeployAfterSync off —
+// RedeployProject is never called (Arcane does not start a stopped project on
+// its behalf by default), which also means the image pull that RedeployProject
+// performs as a side effect never happens. Left alone, that can leave a
+// stopped container referencing an image tag that was since re-pointed or
+// pruned upstream, with no local copy to fall back on at next start. If the
+// sync opted into PullImageAfterSync, pull the image here instead so it stays
+// available even while the project is stopped.
+func (s *GitOpsSyncService) redeployIfRunningAfterSync(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project, actor common.User, syncMode string) error {
details, err := s.projectService.GetProjectDetails(ctx, project.ID, projecttypes.DetailsOptions{})
- if err != nil {
- return nil //nolint:nilerr // best-effort: skip post-sync redeploy when project state can't be determined
- }
- if details.Status != string(projectpkg.ProjectStatusRunning) && details.Status != string(projectpkg.ProjectStatusPartiallyRunning) {
- return nil
+ running := err == nil && (details.Status == string(projectpkg.ProjectStatusRunning) || details.Status == string(projectpkg.ProjectStatusPartiallyRunning))
+
+ if !running && !sync.RedeployAfterSync {
+ return s.pullImageAfterSyncIfConfiguredInternal(ctx, sync, project, actor)
}
- slog.InfoContext(ctx, "Redeploying project due to content change from Git sync", "syncMode", syncMode, "projectName", project.Name, "projectId", project.ID)
+ slog.InfoContext(ctx, "Redeploying project due to content change from Git sync", "syncMode", syncMode, "projectName", project.Name, "projectId", project.ID, "wasRunning", running)
if err := s.projectService.RedeployProject(ctx, project.ID, actor, nil); err != nil {
slog.ErrorContext(ctx, "Failed to redeploy project after Git sync", "syncMode", syncMode, "error", err, "projectId", project.ID)
return common.Classify(common.ErrRedeployAfterSyncFailed, errors.WrapIf(err, "redeploy failed"))
@@ -1171,6 +1193,28 @@ func (s *GitOpsSyncService) redeployIfRunningAfterSync(ctx context.Context, proj
return nil
}
+// pullImageAfterSyncIfConfiguredInternal pulls each service's image for a
+// project that Git sync changed but did not redeploy (because it wasn't
+// running), when the sync has PullImageAfterSync enabled. Best-effort: a pull
+// failure is logged but never fails the sync itself, mirroring how
+// RedeployProject treats its own pre-deploy pull.
+func (s *GitOpsSyncService) pullImageAfterSyncIfConfiguredInternal(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project, actor common.User) error {
+ if !sync.PullImageAfterSync {
+ return nil
+ }
+
+ credentials, cerr := s.projectService.ResolveRegistryCredentials(ctx)
+ if cerr != nil {
+ slog.WarnContext(ctx, "failed to resolve registry credentials for post-sync pull", "error", cerr, "projectId", project.ID)
+ }
+ slog.InfoContext(ctx, "Pulling project images after Git sync (project not running)", "projectName", project.Name, "projectId", project.ID)
+ if err := s.projectService.PullProjectImages(ctx, project.ID, io.Discard, actor, credentials); err != nil {
+ slog.ErrorContext(ctx, "failed to pull project images after Git sync", "error", err, "projectId", project.ID)
+ return common.Classify(common.ErrRedeployAfterSyncFailed, errors.WrapIf(err, "post-sync image pull failed"))
+ }
+ return nil
+}
+
// logSyncSuccess records the Git sync completion event once the filesystem and
// sync-status updates have already succeeded.
func (s *GitOpsSyncService) logSyncSuccess(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project, actor common.User) {
@@ -1516,6 +1560,20 @@ func (s *GitOpsSyncService) createProjectForSyncInternal(ctx context.Context, sy
slog.InfoContext(ctx, "Created project for GitOps sync", "projectName", sync.ProjectName, "projectId", project.ID)
+ // A freshly created project has no containers yet, so it's always
+ // "stopped" from RedeployProject's point of view — mirror
+ // updateProjectForSyncInternal's post-sync behavior here instead of
+ // silently leaving the new project undeployed with no image pulled.
+ if sync.RedeployAfterSync {
+ slog.InfoContext(ctx, "Redeploying newly created project from Git sync", "projectName", project.Name, "projectId", project.ID)
+ if err := s.projectService.RedeployProject(ctx, project.ID, actor, nil); err != nil {
+ slog.ErrorContext(ctx, "Failed to redeploy newly created project after Git sync", "error", err, "projectId", project.ID)
+ return nil, common.Classify(common.ErrRedeployAfterSyncFailed, errors.WrapIf(err, "redeploy failed"))
+ }
+ } else if err := s.pullImageAfterSyncIfConfiguredInternal(ctx, sync, project, actor); err != nil {
+ return nil, err
+ }
+
return project, nil
}
@@ -1566,15 +1624,27 @@ func (s *GitOpsSyncService) updateProjectForSyncInternal(ctx context.Context, sy
// can reflect it on the sync row's LastSyncError.
if contentChanged {
details, err := s.projectService.GetProjectDetails(ctx, project.ID, projecttypes.DetailsOptions{})
- if err == nil && (details.Status == string(projectpkg.ProjectStatusRunning) || details.Status == string(projectpkg.ProjectStatusPartiallyRunning)) {
- slog.InfoContext(ctx, "Redeploying project due to content change from Git sync", "projectName", project.Name, "projectId", project.ID)
+ running := err == nil && (details.Status == string(projectpkg.ProjectStatusRunning) || details.Status == string(projectpkg.ProjectStatusPartiallyRunning))
+ if running || sync.RedeployAfterSync {
+ slog.InfoContext(ctx, "Redeploying project due to content change from Git sync", "projectName", project.Name, "projectId", project.ID, "wasRunning", running)
if err := s.projectService.RedeployProject(ctx, project.ID, actor, nil); err != nil {
slog.ErrorContext(ctx, "Failed to redeploy project after Git sync", "error", err, "projectId", project.ID)
return common.Classify(common.ErrRedeployAfterSyncFailed, errors.WrapIf(err, "redeploy failed"))
}
+ } else {
+ // Project isn't running and RedeployAfterSync is off, so
+ // RedeployProject (and its pull side effect) never runs. Pull
+ // explicitly if this sync opted into PullImageAfterSync, so a
+ // stopped container isn't left referencing an image tag that's
+ // since been re-pointed or pruned upstream. A pull failure is
+ // returned rather than swallowed, so it surfaces on the sync
+ // row's LastSyncError instead of the sync silently reporting
+ // success with a stale image.
+ if err := s.pullImageAfterSyncIfConfiguredInternal(ctx, sync, project, actor); err != nil {
+ return err
+ }
}
}
-
return nil
}
diff --git a/backend/internal/project/model.go b/backend/internal/project/model.go
index d34cb31e0..e51bcd4ad 100644
--- a/backend/internal/project/model.go
+++ b/backend/internal/project/model.go
@@ -98,6 +98,8 @@ type GitOpsSync struct {
PreDeployTimeoutSec int `json:"preDeployTimeoutSec" gorm:"column:pre_deploy_timeout_sec;default:60"`
AutoSync bool `json:"autoSync" sortable:"true" search:"auto,automatic,sync,continuous,scheduled"`
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"`
}
func (GitOpsSync) TableName() string {
diff --git a/backend/resources/migrations/postgres/074_add_gitops_sync_pull_redeploy.sql b/backend/resources/migrations/postgres/074_add_gitops_sync_pull_redeploy.sql
new file mode 100644
index 000000000..2dab54209
--- /dev/null
+++ b/backend/resources/migrations/postgres/074_add_gitops_sync_pull_redeploy.sql
@@ -0,0 +1,7 @@
+-- +goose Up
+ALTER TABLE gitops_syncs ADD COLUMN pull_image_after_sync BOOLEAN NOT NULL DEFAULT false;
+ALTER TABLE gitops_syncs ADD COLUMN redeploy_after_sync BOOLEAN NOT NULL DEFAULT false;
+
+-- +goose Down
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS redeploy_after_sync;
+ALTER TABLE gitops_syncs DROP COLUMN IF EXISTS pull_image_after_sync;
diff --git a/backend/resources/migrations/sqlite/074_add_gitops_sync_pull_redeploy.sql b/backend/resources/migrations/sqlite/074_add_gitops_sync_pull_redeploy.sql
new file mode 100644
index 000000000..b80a5f264
--- /dev/null
+++ b/backend/resources/migrations/sqlite/074_add_gitops_sync_pull_redeploy.sql
@@ -0,0 +1,7 @@
+-- +goose Up
+ALTER TABLE gitops_syncs ADD COLUMN pull_image_after_sync BOOLEAN NOT NULL DEFAULT false;
+ALTER TABLE gitops_syncs ADD COLUMN redeploy_after_sync BOOLEAN NOT NULL DEFAULT false;
+
+-- +goose Down
+ALTER TABLE gitops_syncs DROP COLUMN redeploy_after_sync;
+ALTER TABLE gitops_syncs DROP COLUMN pull_image_after_sync;
diff --git a/frontend/messages/en.json b/frontend/messages/en.json
index a1844ee65..78635c364 100644
--- a/frontend/messages/en.json
+++ b/frontend/messages/en.json
@@ -1834,6 +1834,11 @@
"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_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_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_auto_sync": "Auto Sync",
"git_sync_sync_interval": "Sync Interval",
"git_sync_sync_interval_description": "Minutes",
diff --git a/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte b/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
index 6b06efcc6..0364d8d60 100644
--- a/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
+++ b/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
@@ -87,6 +87,8 @@
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(),
@@ -128,6 +130,8 @@
composePath:
open && syncToEdit ? syncToEdit.composePath : selectedTargetType === 'swarm_stack' ? 'compose.yml' : 'docker-compose.yml',
syncDirectory: open && syncToEdit ? (syncToEdit.syncDirectory ?? false) : false,
+ pullImageAfterSync: open && syncToEdit ? (syncToEdit.pullImageAfterSync ?? false) : false,
+ redeployAfterSync: open && syncToEdit ? (syncToEdit.redeployAfterSync ?? false) : false,
maxSyncFiles: open && syncToEdit ? (syncToEdit.maxSyncFiles ?? 0) : (settingsQuery.data?.gitSyncMaxFiles ?? 0),
maxSyncTotalSizeMb:
open && syncToEdit
@@ -249,6 +253,8 @@
targetType: selectedTargetType,
projectName: data.name,
syncDirectory: data.syncDirectory,
+ pullImageAfterSync: data.pullImageAfterSync,
+ redeployAfterSync: data.redeployAfterSync,
maxSyncFiles: data.maxSyncFiles,
maxSyncTotalSize: megabytesToBytesInternal(data.maxSyncTotalSizeMb),
maxSyncBinarySize: megabytesToBytesInternal(data.maxSyncBinarySizeMb),
@@ -427,6 +433,40 @@
{/if}
</div>
</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
+ >
+ <p class="text-xs text-muted-foreground">
+ {$inputs.redeployAfterSync.value
+ ? m.git_sync_pull_image_after_sync_redundant_description()
+ : m.git_sync_pull_image_after_sync_description()}
+ </p>
+ {#if $inputs.pullImageAfterSync.error}
+ <p class="text-xs font-medium text-destructive">{$inputs.pullImageAfterSync.error}</p>
+ {/if}
+ </div>
+ </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
+ >
+ <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>
+ </div>
</div>
<div class="space-y-2 sm:max-w-32">
diff --git a/frontend/src/lib/types/automation.ts b/frontend/src/lib/types/automation.ts
index efb9a8e42..6bd5081ae 100644
--- a/frontend/src/lib/types/automation.ts
+++ b/frontend/src/lib/types/automation.ts
@@ -125,6 +125,8 @@ export interface GitOpsSyncCreateDto {
autoSync?: boolean;
syncInterval?: number;
syncDirectory?: boolean;
+ pullImageAfterSync?: boolean;
+ redeployAfterSync?: boolean;
maxSyncFiles?: number;
maxSyncTotalSize?: number;
maxSyncBinarySize?: number;
@@ -146,6 +148,8 @@ export interface GitOpsSyncUpdateDto {
autoSync?: boolean;
syncInterval?: number;
syncDirectory?: boolean;
+ pullImageAfterSync?: boolean;
+ redeployAfterSync?: boolean;
maxSyncFiles?: number;
maxSyncTotalSize?: number;
maxSyncBinarySize?: number;
@@ -171,6 +175,8 @@ export interface GitOpsSync {
autoSync: boolean;
syncInterval: number;
syncDirectory: boolean;
+ pullImageAfterSync: boolean;
+ redeployAfterSync: boolean;
syncedFiles?: string;
maxSyncFiles: number;
maxSyncTotalSize: number;
diff --git a/types/gitops/gitops.go b/types/gitops/gitops.go
index e6e3558d3..96fd2d11b 100644
--- a/types/gitops/gitops.go
+++ b/types/gitops/gitops.go
@@ -235,6 +235,20 @@ type GitOpsSync struct {
//
// Required: true
SyncDirectory bool `json:"syncDirectory"`
+
+ // PullImageAfterSync indicates whether each service's image should be pulled
+ // right after a sync that changes managed content, even if the project is
+ // currently stopped.
+ //
+ // Required: true
+ PullImageAfterSync bool `json:"pullImageAfterSync"`
+
+ // RedeployAfterSync indicates whether the project should be redeployed
+ // (recreated with freshly pulled images) right after a sync that changes
+ // managed content, regardless of whether it's currently running or stopped.
+ //
+ // Required: true
+ RedeployAfterSync bool `json:"redeployAfterSync"`
}
// SyncCounts contains counts of syncs by status within the current filtered set.
@@ -405,6 +419,21 @@ type CreateSyncRequest struct {
// Required: false
SyncDirectory *bool `json:"syncDirectory,omitempty"`
+ // PullImageAfterSync indicates whether each service's image should be pulled
+ // right after a sync that changes managed content, even if the project is
+ // currently stopped. Default: false.
+ //
+ // Required: false
+ PullImageAfterSync *bool `json:"pullImageAfterSync,omitempty"`
+
+ // RedeployAfterSync indicates whether the project should be redeployed
+ // (recreated with freshly pulled images) right after a sync that changes
+ // managed content, regardless of whether it's currently running or stopped.
+ // Default: false.
+ //
+ // Required: false
+ RedeployAfterSync *bool `json:"redeployAfterSync,omitempty"`
+
// MaxSyncFiles is the maximum number of files to sync.
// 0 means unlimited; env var overrides take precedence.
// Default: 0
@@ -516,6 +545,21 @@ type UpdateSyncRequest struct {
// Required: false
SyncDirectory *bool `json:"syncDirectory,omitempty"`
+ // PullImageAfterSync indicates whether each service's image should be pulled
+ // right after a sync that changes managed content, even if the project is
+ // currently stopped. Default: false.
+ //
+ // Required: false
+ PullImageAfterSync *bool `json:"pullImageAfterSync,omitempty"`
+
+ // RedeployAfterSync indicates whether the project should be redeployed
+ // (recreated with freshly pulled images) right after a sync that changes
+ // managed content, regardless of whether it's currently running or stopped.
+ // Default: false.
+ //
+ // Required: false
+ RedeployAfterSync *bool `json:"redeployAfterSync,omitempty"`
+
// MaxSyncFiles is the maximum number of files to sync.
// 0 means unlimited; env var overrides take precedence.
//