Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
feat(gitops): sync .env files from git repositories (#1632)
Ryan Wilson7f05d1797992
diff --git a/backend/internal/services/gitops_sync_service.go b/backend/internal/services/gitops_sync_service.goindex 01ce57156..85e08207c 100644--- a/backend/internal/services/gitops_sync_service.go+++ b/backend/internal/services/gitops_sync_service.go@@ -5,6 +5,7 @@ import ("errors""fmt""log/slog"+ "path/filepath""strings""time"@@ -334,8 +335,20 @@ func (s *GitOpsSyncService) PerformSync(ctx context.Context, environmentID, id sreturn result, s.failSync(syncCtx, id, result, sync, "Failed to read compose file", err.Error())}+ // Try to read .env file from the same directory as the compose file+ var envContent *string+ envPath := filepath.Join(filepath.Dir(sync.ComposePath), ".env")+ if s.repoService.gitClient.FileExists(syncCtx, repoPath, envPath) {+ content, err := s.repoService.gitClient.ReadFile(syncCtx, repoPath, envPath)+ if err != nil {+ slog.WarnContext(syncCtx, "Failed to read .env file", "path", envPath, "error", err)+ } else {+ envContent = &content+ }+ }+// Get or create project- project, err := s.getOrCreateProject(syncCtx, sync, id, composeContent, result)+ project, err := s.getOrCreateProjectInternal(syncCtx, sync, id, composeContent, envContent, result)if err != nil {return result, err}@@ -547,8 +560,8 @@ func (s *GitOpsSyncService) failSync(ctx context.Context, id string, result *gitreturn fmt.Errorf("%s", errMsg)}-func (s *GitOpsSyncService) createProjectForSync(ctx context.Context, sync *models.GitOpsSync, id string, composeContent string, result *gitops.SyncResult) (*models.Project, error) {- project, err := s.projectService.CreateProject(ctx, sync.ProjectName, composeContent, nil, systemUser)+func (s *GitOpsSyncService) createProjectForSyncInternal(ctx context.Context, sync *models.GitOpsSync, id string, composeContent string, envContent *string, result *gitops.SyncResult) (*models.Project, error) {+ project, err := s.projectService.CreateProject(ctx, sync.ProjectName, composeContent, envContent, systemUser)if err != nil {return nil, s.failSync(ctx, id, result, sync, "Failed to create project", err.Error())}@@ -576,7 +589,7 @@ func (s *GitOpsSyncService) createProjectForSync(ctx context.Context, sync *modereturn project, nil}-func (s *GitOpsSyncService) getOrCreateProject(ctx context.Context, sync *models.GitOpsSync, id string, composeContent string, result *gitops.SyncResult) (*models.Project, error) {+func (s *GitOpsSyncService) getOrCreateProjectInternal(ctx context.Context, sync *models.GitOpsSync, id string, composeContent string, envContent *string, result *gitops.SyncResult) (*models.Project, error) {var project *models.Projectvar err error@@ -589,26 +602,33 @@ func (s *GitOpsSyncService) getOrCreateProject(ctx context.Context, sync *models}if project == nil {- return s.createProjectForSync(ctx, sync, id, composeContent, result)+ return s.createProjectForSyncInternal(ctx, sync, id, composeContent, envContent, result)}- if err := s.updateProjectForSync(ctx, sync, id, project, composeContent, result); err != nil {+ if err := s.updateProjectForSyncInternal(ctx, sync, id, project, composeContent, envContent, result); err != nil {return nil, err}return project, nil}-func (s *GitOpsSyncService) updateProjectForSync(ctx context.Context, sync *models.GitOpsSync, id string, project *models.Project, composeContent string, result *gitops.SyncResult) error {+func (s *GitOpsSyncService) updateProjectForSyncInternal(ctx context.Context, sync *models.GitOpsSync, id string, project *models.Project, composeContent string, envContent *string, result *gitops.SyncResult) error {// Get current content to see if it changed- oldCompose, _, _ := s.projectService.GetProjectContent(ctx, project.ID)+ oldCompose, oldEnv, _ := s.projectService.GetProjectContent(ctx, project.ID)contentChanged := oldCompose != composeContent+ if envContent != nil {+ if oldEnv != *envContent {+ contentChanged = true+ }+ } else if oldEnv != "" {+ contentChanged = true+ }- // Update existing project's compose file- _, err := s.projectService.UpdateProject(ctx, project.ID, nil, &composeContent, nil)+ // Update existing project's compose and env files+ _, err := s.projectService.UpdateProject(ctx, project.ID, nil, &composeContent, envContent)if err != nil {- return s.failSync(ctx, id, result, sync, "Failed to update project compose file", err.Error())+ return s.failSync(ctx, id, result, sync, "Failed to update project files", err.Error())}- slog.InfoContext(ctx, "Updated project compose file", "projectName", project.Name, "projectId", project.ID)+ slog.InfoContext(ctx, "Updated project files", "projectName", project.Name, "projectId", project.ID)// If content changed and project is running, redeployif contentChanged {diff --git a/docker/Dockerfile b/docker/Dockerfileindex 6d3114ac1..f29147d01 100644--- a/docker/Dockerfile+++ b/docker/Dockerfile@@ -21,6 +21,7 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store \pnpm install -r --offlineENV BUILD_PATH=dist+ENV NODE_OPTIONS="--max-old-space-size=4096"RUN pnpm -C frontend build# Stage 2: Build Backenddiff --git a/frontend/src/routes/(app)/projects/[projectId]/+page.svelte b/frontend/src/routes/(app)/projects/[projectId]/+page.svelteindex cccb78443..b3ed7d7fb 100644--- a/frontend/src/routes/(app)/projects/[projectId]/+page.svelte+++ b/frontend/src/routes/(app)/projects/[projectId]/+page.svelte@@ -90,6 +90,7 @@!isGitOpsManaged && !isLoading.saving && project?.status !== 'running' && project?.status !== 'partially running');let canEditCompose = $derived(!isGitOpsManaged);+ let canEditEnv = $derived(!isGitOpsManaged);let autoScrollStackLogs = $state(true);@@ -550,6 +551,7 @@language="env"bind:value={$inputs.envContent.value}error={$inputs.envContent.error ?? undefined}+ readOnly={!canEditEnv}/>{:else}{@const includeFile = project?.includeFiles?.find((f) => f.relativePath === selectedFile)}@@ -644,6 +646,7 @@language="env"bind:value={$inputs.envContent.value}error={$inputs.envContent.error ?? undefined}+ readOnly={!canEditEnv}/>{:else}{@const includeFile = project?.includeFiles?.find((f) => f.relativePath === selectedFile)}@@ -708,6 +711,7 @@language="env"bind:value={$inputs.envContent.value}error={$inputs.envContent.error ?? undefined}+ readOnly={!canEditEnv}/></div>@@ -744,6 +748,7 @@language="env"bind:value={$inputs.envContent.value}error={$inputs.envContent.error ?? undefined}+ readOnly={!canEditEnv}/></div>{/snippet}diff --git a/tests/playwright.config.ts b/tests/playwright.config.tsindex f0d3f2684..dd75000be 100644--- a/tests/playwright.config.ts+++ b/tests/playwright.config.ts@@ -21,13 +21,19 @@ export default defineConfig({},projects: [{- name: 'setup',- testMatch: /setup\/.*\.setup\.ts/,+ name: 'auth-setup',+ testMatch: /setup\/auth\.setup\.ts/,+ },+ {+ name: 'gitops-setup',+ testMatch: /setup\/gitops\.setup\.ts/,+ use: { storageState: '.auth/login.json' },+ dependencies: ['auth-setup'],},{name: 'chromium',use: { ...devices['Desktop Chrome'], storageState: '.auth/login.json' },- dependencies: ['setup'],+ dependencies: ['auth-setup', 'gitops-setup'],testMatch: /spec\/.*\.spec\.ts/,},],diff --git a/tests/setup/gitops-test-repo/compose.yaml b/tests/setup/gitops-test-repo/compose.yamlnew file mode 100644index 000000000..a4e6e3ca9--- /dev/null+++ b/tests/setup/gitops-test-repo/compose.yaml@@ -0,0 +1,11 @@+services:+ nginx:+ image: nginx:alpine+ container_name: gitops-test-nginx+ ports:+ - "8888:80"+ volumes:+ - nginx_data:/usr/share/nginx/html++volumes:+ nginx_data:diff --git a/tests/setup/gitops.setup.ts b/tests/setup/gitops.setup.tsnew file mode 100644index 000000000..4e9175193--- /dev/null+++ b/tests/setup/gitops.setup.ts@@ -0,0 +1,183 @@+import { test as setup, expect } from '@playwright/test';++/**+ * GitOps Test Setup+ *+ * This setup:+ * 1. Registers a Git repository in Arcane (GitHub)+ * 2. Configures Arcane to sync from this repository+ */+const GITOPS_REPO_NAME = 'gitsyncs-test-repo';+const GITOPS_REPO_URL = 'https://github.com/getarcaneapp/gitsyncs.git';+const GITOPS_REPO_BRANCH = 'main';+const GITOPS_COMPOSE_PATH = 'compose-test-repo/compose.yaml';+const GITOPS_SYNC_NAME = 'gitops-test-sync';++setup('create gitops sync in arcane', async ({ page }) => {+ console.log('Creating GitOps sync configuration in Arcane...');++ // Step 1: Create a Git Repository in Arcane pointing to GitHub+ await page.goto('/customize/git-repositories');+ await page.waitForLoadState('networkidle');++ // Check if test repo already exists+ const existingRepo = page.getByRole('cell', { name: GITOPS_REPO_NAME });+ if ((await existingRepo.count()) === 0) {+ console.log('Creating Git repository in Arcane...');++ // Click Add Repository button+ const addRepoButton = page.getByRole('button', { name: /Add.*Repository/i });+ await addRepoButton.click();++ // Wait for dialog+ const dialog = page.getByRole('dialog');+ await expect(dialog).toBeVisible();++ // Fill repository form - use specific labels to avoid ambiguity+ await dialog.getByRole('textbox', { name: /Repository Name/i }).fill(GITOPS_REPO_NAME);+ await dialog.getByRole('textbox', { name: /Repository URL/i }).fill(GITOPS_REPO_URL);++ // Select "None" for auth type (public repo) - click the auth type dropdown+ const authTrigger = dialog.locator('#authType');+ if ((await authTrigger.count()) > 0) {+ await authTrigger.click();+ await page.waitForTimeout(300);+ const noneOption = page.getByRole('option', { name: /None|No Auth/i });+ if ((await noneOption.count()) > 0) {+ await noneOption.click();+ } else {+ await page.keyboard.press('Escape');+ }+ }++ // Submit the form+ const submitButton = dialog.getByRole('button', { name: /Add Repository/i });+ await submitButton.click();++ // Wait for success or error+ await page.waitForTimeout(2000);+ const successToast = page.getByText(/created|success/i);+ if ((await successToast.count()) > 0) {+ console.log('Git repository created successfully in Arcane');+ } else {+ console.log('Repository may already exist or creation had issues, continuing...');+ }+ } else {+ console.log('Git repository already exists in Arcane');+ }++ // Step 2: Create GitOps Sync+ await page.goto('/environments/0/gitops');+ await page.waitForLoadState('networkidle');++ // Check if sync already exists+ const existingSync = page.getByRole('cell', { name: GITOPS_SYNC_NAME });+ if ((await existingSync.count()) === 0) {+ console.log('Creating GitOps sync...');++ // Click Add Sync button+ const addSyncButton = page.getByRole('button', { name: /Add.*Sync/i });+ await addSyncButton.click();++ // Wait for dialog+ const dialog = page.getByRole('dialog');+ await expect(dialog).toBeVisible();++ // Wait for dialog content to load+ await page.waitForTimeout(1000);++ // Fill sync form - Name field (use specific selector)+ const nameInput = dialog.getByRole('textbox', { name: /Sync Name/i });+ await nameInput.fill(GITOPS_SYNC_NAME);++ // Select repository from dropdown+ const repoTrigger = dialog.locator('#repository, [id*="repository"]').first();+ if ((await repoTrigger.count()) > 0) {+ await repoTrigger.click();+ await page.waitForTimeout(300);+ const repoOption = page.getByRole('option', { name: GITOPS_REPO_NAME });+ if ((await repoOption.count()) > 0) {+ await repoOption.click();+ }+ }++ // Wait for branches to load+ await page.waitForTimeout(2000);++ // Select or enter branch+ const branchTrigger = dialog.locator('#branch, [id*="branch"]').first();+ if ((await branchTrigger.count()) > 0) {+ const isSelect = (await branchTrigger.getAttribute('role')) === 'combobox';+ if (isSelect) {+ await branchTrigger.click();+ await page.waitForTimeout(300);+ const mainOption = page.getByRole('option', { name: new RegExp(GITOPS_REPO_BRANCH, 'i') });+ if ((await mainOption.count()) > 0) {+ await mainOption.click();+ } else {+ await page.keyboard.press('Escape');+ }+ }+ }++ // Enter compose path+ const composePathInput = dialog.getByPlaceholder(/docker-compose|compose/i);+ if ((await composePathInput.count()) > 0) {+ await composePathInput.fill(GITOPS_COMPOSE_PATH);+ }++ // Disable auto-sync for tests+ const autoSyncSwitch = dialog.getByRole('switch');+ if ((await autoSyncSwitch.count()) > 0) {+ const isChecked = await autoSyncSwitch.getAttribute('data-state');+ if (isChecked === 'checked') {+ await autoSyncSwitch.click();+ }+ }++ // Submit the form+ const submitButton = dialog.getByRole('button', { name: /Add.*Sync|Create/i }).filter({ hasNotText: /Cancel/ });+ await submitButton.click();++ // Wait for result+ await page.waitForTimeout(3000);+ console.log('GitOps sync configuration created');+ } else {+ console.log('GitOps sync already exists');+ }++ // Step 3: Trigger initial sync to create the managed project+ console.log('Triggering initial sync...');+ await page.reload();+ await page.waitForLoadState('networkidle');++ // Find the sync row and trigger sync+ const syncRow = page.locator('tr').filter({ hasText: GITOPS_SYNC_NAME });+ if ((await syncRow.count()) > 0) {+ // Look for sync button or menu+ const syncButton = syncRow.getByRole('button', { name: /Sync|Sync Now/i });+ const menuButton = syncRow.getByRole('button', { name: /menu|actions|Open menu/i });++ if ((await syncButton.count()) > 0) {+ await syncButton.click();+ } else if ((await menuButton.count()) > 0) {+ await menuButton.click();+ await page.waitForTimeout(300);+ const syncMenuItem = page.getByRole('menuitem', { name: /Sync/i });+ if ((await syncMenuItem.count()) > 0) {+ await syncMenuItem.click();+ }+ }++ // Wait for sync to complete+ await page.waitForTimeout(5000);+ console.log('Initial sync triggered');+ }++ // Verify project was created+ await page.goto('/projects');+ await page.waitForLoadState('networkidle');+ await page.waitForTimeout(2000);++ console.log('GitOps test setup complete!');+});diff --git a/tests/spec/project.spec.ts b/tests/spec/project.spec.tsindex 36613b589..74dce6066 100644--- a/tests/spec/project.spec.ts+++ b/tests/spec/project.spec.ts@@ -54,7 +54,12 @@ test.describe('Projects Page', () => {await menuButton.click();await expect(page.getByRole('menuitem', { name: 'Edit' })).toBeVisible();- await expect(page.getByRole('menuitem', { name: /Up|Down|Restart/ })).toBeVisible();+ // Check for at least one of the state action buttons (Up/Down/Restart)+ const upItem = page.getByRole('menuitem', { name: 'Up', exact: true });+ const downItem = page.getByRole('menuitem', { name: 'Down', exact: true });+ const restartItem = page.getByRole('menuitem', { name: 'Restart', exact: true });+ const hasStateAction = (await upItem.count()) > 0 || (await downItem.count()) > 0 || (await restartItem.count()) > 0;+ expect(hasStateAction).toBe(true);await expect(page.getByRole('menuitem', { name: 'Pull & Redeploy' })).toBeVisible();await expect(page.getByRole('menuitem', { name: 'Destroy' })).toBeVisible();});@@ -63,12 +68,13 @@ test.describe('Projects Page', () => {test.skip(!realProjects.length, 'No projects available for navigation test');await page.waitForLoadState('networkidle');- const firstProjectLink = page.locator('tbody tr').first().getByRole('link');+ // Get the first project link that points to /projects/ (not the "Git" indicator link)+ const firstProjectLink = page.locator('tbody tr').first().getByRole('link').filter({ hasText: /^(?!Git$)/ }).first();const projectName = await firstProjectLink.textContent();await firstProjectLink.click();await expect(page).toHaveURL(/\/projects\/.+/);- await expect(page.getByRole('heading', { name: new RegExp(`.*${projectName}`) })).toBeVisible();+ await expect(page.getByRole('button', { name: new RegExp(`${projectName}`) })).toBeVisible();});test('should allow searching/filtering projects', async ({ page }) => {@@ -307,6 +313,165 @@ test.describe('New Compose Project Page', () => {});});+test.describe('GitOps Managed Project', () => {+ test('should show read-only alert when project is GitOps managed', async ({ page }) => {+ const gitOpsProject = realProjects.find((p) => p.gitOpsManagedBy);+ test.skip(!gitOpsProject, 'No GitOps-managed projects found');++ await page.goto(`/projects/${gitOpsProject!.id}`);+ await page.waitForLoadState('networkidle');++ // Navigate to Configuration tab+ const configTab = page.getByRole('tab', { name: /Configuration|Config/i });+ await configTab.click();+ await page.waitForLoadState('networkidle');++ // Verify the GitOps read-only alert is visible (title contains "Git" and "Read-only")+ await expect(page.getByText('Git Read-only')).toBeVisible();+ await expect(page.getByText(/managed by Git/i)).toBeVisible();+ });++ test('should display Sync from Git button when GitOps managed', async ({ page }) => {+ const gitOpsProject = realProjects.find((p) => p.gitOpsManagedBy);+ test.skip(!gitOpsProject, 'No GitOps-managed projects found');++ await page.goto(`/projects/${gitOpsProject!.id}`);+ await page.waitForLoadState('networkidle');++ const configTab = page.getByRole('tab', { name: /Configuration|Config/i });+ await configTab.click();+ await page.waitForLoadState('networkidle');++ // Verify the Sync from Git button is present+ await expect(page.getByRole('button', { name: 'Sync from Git' })).toBeVisible();+ });++ test('should show last sync commit when GitOps managed', async ({ page }) => {+ const gitOpsProject = realProjects.find((p) => p.gitOpsManagedBy && p.lastSyncCommit);+ test.skip(!gitOpsProject, 'No GitOps-managed projects with sync commit found');++ await page.goto(`/projects/${gitOpsProject!.id}`);+ await page.waitForLoadState('networkidle');++ // The commit hash should be visible somewhere on the page+ const commitHash = gitOpsProject!.lastSyncCommit!.substring(0, 7);+ await expect(page.getByText(new RegExp(commitHash))).toBeVisible();+ });++ test('should disable name editing when GitOps managed', async ({ page }) => {+ const gitOpsProject = realProjects.find((p) => p.gitOpsManagedBy);+ test.skip(!gitOpsProject, 'No GitOps-managed projects found');++ await page.goto(`/projects/${gitOpsProject!.id}`);+ await page.waitForLoadState('networkidle');++ // The name button should be disabled for GitOps-managed projects+ const nameButton = page.getByRole('button', { name: gitOpsProject!.name });+ await expect(nameButton).toBeDisabled();+ });++ test('should have compose editor in read-only mode when GitOps managed', async ({ page }) => {+ const gitOpsProject = realProjects.find((p) => p.gitOpsManagedBy);+ test.skip(!gitOpsProject, 'No GitOps-managed projects found');++ await page.goto(`/projects/${gitOpsProject!.id}`);+ await page.waitForLoadState('networkidle');++ const configTab = page.getByRole('tab', { name: /Configuration|Config/i });+ await configTab.click();+ await page.waitForLoadState('networkidle');++ // Wait for Monaco editor to load+ await page.waitForTimeout(1000);++ // Check that the Monaco editor instance has readOnly option set+ const isReadOnly = await page.evaluate(() => {+ const editors = (window as any).monaco?.editor?.getEditors() ?? [];+ // Find the YAML editor (compose file)+ const yamlEditor = editors.find((e: any) => {+ const model = e.getModel();+ return model && model.getLanguageId() === 'yaml';+ });+ if (yamlEditor) {+ return yamlEditor.getOption((window as any).monaco.editor.EditorOption.readOnly);+ }+ return null;+ });++ expect(isReadOnly).toBe(true);+ });++ test('should have env editor in read-only mode when GitOps managed', async ({ page }) => {+ const gitOpsProject = realProjects.find((p) => p.gitOpsManagedBy);+ test.skip(!gitOpsProject, 'No GitOps-managed projects found');++ await page.goto(`/projects/${gitOpsProject!.id}`);+ await page.waitForLoadState('networkidle');++ const configTab = page.getByRole('tab', { name: /Configuration|Config/i });+ await configTab.click();+ await page.waitForLoadState('networkidle');++ // Wait for Monaco editor to load+ await page.waitForTimeout(1000);++ // Check that the Monaco editor instance has readOnly option set+ const isReadOnly = await page.evaluate(() => {+ const editors = (window as any).monaco?.editor?.getEditors() ?? [];+ // Find the env/ini editor+ const envEditor = editors.find((e: any) => {+ const model = e.getModel();+ return model && model.getLanguageId() === 'ini';+ });+ if (envEditor) {+ return envEditor.getOption((window as any).monaco.editor.EditorOption.readOnly);+ }+ return null;+ });++ expect(isReadOnly).toBe(true);+ });++ test('should allow editing for non-GitOps managed projects', async ({ page }) => {+ const regularProject = realProjects.find((p) => !p.gitOpsManagedBy && p.status === 'stopped');+ test.skip(!regularProject, 'No regular (non-GitOps) stopped projects found');++ await page.goto(`/projects/${regularProject!.id}`);+ await page.waitForLoadState('networkidle');++ // The name button should be enabled for regular projects that are stopped+ const nameButton = page.getByRole('button', { name: regularProject!.name });+ await expect(nameButton).toBeEnabled();++ // Navigate to Configuration tab+ const configTab = page.getByRole('tab', { name: /Configuration|Config/i });+ await configTab.click();+ await page.waitForLoadState('networkidle');++ // GitOps alert should NOT be visible+ await expect(page.getByText('Git Read-only')).not.toBeVisible();++ // Sync from Git button should NOT be visible+ await expect(page.getByRole('button', { name: 'Sync from Git' })).not.toBeVisible();+ });++ test('should not show GitOps alert on Configuration tab for regular projects', async ({ page }) => {+ const regularProject = realProjects.find((p) => !p.gitOpsManagedBy);+ test.skip(!regularProject, 'No regular (non-GitOps) projects found');++ await page.goto(`/projects/${regularProject!.id}`);+ await page.waitForLoadState('networkidle');++ const configTab = page.getByRole('tab', { name: /Configuration|Config/i });+ await configTab.click();+ await page.waitForLoadState('networkidle');++ // Verify no GitOps-related UI elements+ await expect(page.getByText(/managed by Git\./i)).not.toBeVisible();+ await expect(page.getByRole('button', { name: 'Sync from Git' })).not.toBeVisible();+ });+});+test.describe('Project Detail Page', () => {test('should display project details for existing project', async ({ page }) => {test.skip(!realProjects.length, 'No projects available for detail page test');