diff --git a/README.md b/README.md index d020934..a73363c 100644 --- a/README.md +++ b/README.md @@ -81,14 +81,6 @@ This will: - Find changed stacks - Deploy only changed stacks assigned to this host -### Dry Run - -To see what would be deployed without actually deploying: - -```bash -./compose-sync -dry-run -``` - ### Custom Config Path ```bash diff --git a/config.yml.example b/config.yml.example index a14c4a8..1a01926 100644 --- a/config.yml.example +++ b/config.yml.example @@ -1,5 +1,6 @@ # Configuration file for compose-sync repo_url: "git@github.com:user/compose-repo.git" # Optional: URL for cloning (not used if repo_path already exists) repo_path: "/path/to/local/repo" # Required: Local path to the git repository +branch: "main" # Optional: Git branch to sync (default: current branch or main) concurrency: 3 # Optional: Number of concurrent deployments (default: 3) diff --git a/main.go b/main.go index 9285e30..f19d873 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,6 @@ import ( func main() { configPath := flag.String("config", "config.yml", "Path to configuration file") - dryRun := flag.Bool("dry-run", false, "Show what would be deployed without actually deploying") flag.Parse() cfg, err := loadConfig(*configPath) @@ -38,8 +37,8 @@ func main() { } fmt.Printf("Detected host: %s\n", currentHost) - fmt.Println("Pulling git repository...") - changedStacks, err := pullAndDetectChanges(cfg.RepoPath) + fmt.Printf("Pulling git repository (branch: %s)...\n", cfg.Branch) + changedStacks, err := pullAndDetectChanges(cfg.RepoPath, cfg.Branch) if err != nil { log.Fatalf("Failed to pull or detect changes: %v", err) } @@ -72,14 +71,6 @@ func main() { fmt.Printf("Stacks to deploy: %v\n", stacksToDeploy) - if *dryRun { - fmt.Println("DRY RUN: Would deploy the following stacks:") - for _, stack := range stacksToDeploy { - fmt.Printf(" - %s\n", stack) - } - return - } - deployStacks(cfg.RepoPath, stacksToDeploy, cfg.Concurrency) } diff --git a/sync.go b/sync.go index fa7483c..63883c9 100644 --- a/sync.go +++ b/sync.go @@ -15,6 +15,7 @@ import ( type config struct { RepoURL string `yaml:"repo_url"` RepoPath string `yaml:"repo_path"` + Branch string `yaml:"branch"` Concurrency int `yaml:"concurrency"` } @@ -33,6 +34,13 @@ func loadConfig(path string) (*config, error) { return nil, fmt.Errorf("repo_path is required in config") } + if cfg.Branch == "" { + cfg.Branch = detectCurrentBranch(cfg.RepoPath) + if cfg.Branch == "" { + cfg.Branch = "main" + } + } + if cfg.Concurrency <= 0 { cfg.Concurrency = 3 // Safe default for Docker } @@ -104,7 +112,17 @@ func getAssignedStacks(repoPath, hostname string) ([]string, error) { return stacks, nil } -func pullAndDetectChanges(repoPath string) ([]string, error) { +func detectCurrentBranch(repoPath string) string { + cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") + cmd.Dir = repoPath + output, err := cmd.Output() + if err != nil { + return "" + } + return strings.TrimSpace(string(output)) +} + +func pullAndDetectChanges(repoPath, branch string) ([]string, error) { if _, err := os.Stat(repoPath); os.IsNotExist(err) { return nil, fmt.Errorf("repository path does not exist: %s", repoPath) } @@ -118,7 +136,7 @@ func pullAndDetectChanges(repoPath string) ([]string, error) { return nil, fmt.Errorf("failed to get previous HEAD: %w", err) } - if err := gitPull(repoPath); err != nil { + if err := gitFetchPull(repoPath, branch); err != nil { return nil, fmt.Errorf("failed to pull: %w", err) } @@ -140,8 +158,25 @@ func pullAndDetectChanges(repoPath string) ([]string, error) { return changedStacks, nil } -func gitPull(repoPath string) error { - cmd := exec.Command("git", "pull") +func gitFetchPull(repoPath, branch string) error { + cmd := exec.Command("git", "fetch", "origin", branch) + cmd.Dir = repoPath + if err := cmd.Run(); err != nil { + return fmt.Errorf("git fetch failed: %w", err) + } + + cmd = exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") + cmd.Dir = repoPath + currentBranch, _ := cmd.Output() + if strings.TrimSpace(string(currentBranch)) != branch { + cmd = exec.Command("git", "checkout", branch) + cmd.Dir = repoPath + if err := cmd.Run(); err != nil { + return fmt.Errorf("git checkout failed: %w", err) + } + } + + cmd = exec.Command("git", "pull", "origin", branch) cmd.Dir = repoPath output, err := cmd.CombinedOutput() if err != nil {