|
7 | 7 | "strings" |
8 | 8 |
|
9 | 9 | "github.com/skssmd/graft/internal/config" |
| 10 | + "github.com/skssmd/graft/internal/git" |
10 | 11 | "gopkg.in/yaml.v3" |
11 | 12 | ) |
12 | 13 |
|
@@ -374,6 +375,35 @@ func GenerateWorkflows(p *Project,env string, remoteURL string, mode string, web |
374 | 375 | gitBranch = meta.GitBranch |
375 | 376 | } |
376 | 377 |
|
| 378 | + // Branch management: Switch to the target branch if necessary |
| 379 | + originalBranch, _ := git.GetCurrentBranch(".") |
| 380 | + switchedBranch := false |
| 381 | + if gitBranch != "" && originalBranch != gitBranch { |
| 382 | + dirty, _ := git.IsDirty(".") |
| 383 | + if dirty { |
| 384 | + fmt.Printf("⚠️ Warning: Repository is dirty. Cannot switch to branch '%s' for workflow generation. Files will be placed in '%s'.\n", gitBranch, originalBranch) |
| 385 | + } else { |
| 386 | + exists, _ := git.BranchExists(".", gitBranch) |
| 387 | + if exists { |
| 388 | + fmt.Printf("🌿 Switching to branch '%s' to place workflows...\n", gitBranch) |
| 389 | + if err := git.CheckoutBranch(".", gitBranch); err == nil { |
| 390 | + switchedBranch = true |
| 391 | + defer func() { |
| 392 | + if switchedBranch { |
| 393 | + fmt.Printf("🌿 Switching back to original branch '%s'...\n", originalBranch) |
| 394 | + git.CheckoutBranch(".", originalBranch) |
| 395 | + } |
| 396 | + }() |
| 397 | + } else { |
| 398 | + fmt.Printf("⚠️ Warning: Failed to switch to branch '%s': %v\n", gitBranch, err) |
| 399 | + } |
| 400 | + } else { |
| 401 | + fmt.Printf("⚠️ Warning: Target branch '%s' does not exist. Files will be placed in '%s'.\n", gitBranch, originalBranch) |
| 402 | + } |
| 403 | + } |
| 404 | + } |
| 405 | + |
| 406 | + |
377 | 407 | // Extract owner and repo from remote URL |
378 | 408 | ownerRepo := "" |
379 | 409 | if strings.HasPrefix(remoteURL, "https://") { |
@@ -405,11 +435,8 @@ func GenerateWorkflows(p *Project,env string, remoteURL string, mode string, web |
405 | 435 | - completed` |
406 | 436 | condition = "if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}" |
407 | 437 | } else { |
408 | | - // Use the selected branch for push triggers |
| 438 | + // Use ONLY the configured branch for push triggers |
409 | 439 | triggerBranches := "[ " + gitBranch + " ]" |
410 | | - if gitBranch == "main" || gitBranch == "master" || gitBranch == "develop" { |
411 | | - triggerBranches = "[ main, develop ]" |
412 | | - } |
413 | 440 |
|
414 | 441 | triggers = fmt.Sprintf(` push: |
415 | 442 | branches: %s`, triggerBranches) |
@@ -471,13 +498,9 @@ jobs: |
471 | 498 | } |
472 | 499 | } |
473 | 500 |
|
474 | | - // 2. Generate CI Workflow (only for git-images) |
475 | 501 | if mode == "git-images" { |
476 | | - // Use the selected branch for push triggers |
| 502 | + // Use ONLY the configured branch for push triggers |
477 | 503 | triggerBranches := "[ " + gitBranch + " ]" |
478 | | - if gitBranch == "main" || gitBranch == "master" || gitBranch == "develop" { |
479 | | - triggerBranches = "[ main, develop ]" |
480 | | - } |
481 | 504 |
|
482 | 505 | ciTemplate := fmt.Sprintf("name: CI/CD Pipeline ("+env+")\n\n"+`on: |
483 | 506 | push: |
|
0 commit comments