Skip to content

Commit 34d36ef

Browse files
committed
add workflow to correct branch
1 parent 84d6ace commit 34d36ef

File tree

4 files changed

+72
-11
lines changed

4 files changed

+72
-11
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ winget:
197197
owner: skssmd # ← CHANGED: Your fork, not microsoft
198198
name: winget-pkgs
199199
branch: "{{.ProjectName}}-{{.Version}}-{{.ShortCommit}}"
200-
token: "{{ .Env.GITHUB_TOKEN }}"
200+
token: "{{ .Env.WINGET_GITHUB_TOKEN }}"
201201
pull_request:
202202
enabled: true
203203
draft: false

cmd/graft/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func main() {
2121
if len(args) > 0 {
2222
arg := args[0]
2323
if arg == "-v" || arg == "--version" {
24-
fmt.Println("v2.3.1")
24+
fmt.Println("v2.3.2")
2525
return
2626
}
2727
if arg == "--help" {

internal/deploy/project.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/skssmd/graft/internal/config"
10+
"github.com/skssmd/graft/internal/git"
1011
"gopkg.in/yaml.v3"
1112
)
1213

@@ -374,6 +375,35 @@ func GenerateWorkflows(p *Project,env string, remoteURL string, mode string, web
374375
gitBranch = meta.GitBranch
375376
}
376377

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+
377407
// Extract owner and repo from remote URL
378408
ownerRepo := ""
379409
if strings.HasPrefix(remoteURL, "https://") {
@@ -405,11 +435,8 @@ func GenerateWorkflows(p *Project,env string, remoteURL string, mode string, web
405435
- completed`
406436
condition = "if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}"
407437
} else {
408-
// Use the selected branch for push triggers
438+
// Use ONLY the configured branch for push triggers
409439
triggerBranches := "[ " + gitBranch + " ]"
410-
if gitBranch == "main" || gitBranch == "master" || gitBranch == "develop" {
411-
triggerBranches = "[ main, develop ]"
412-
}
413440

414441
triggers = fmt.Sprintf(` push:
415442
branches: %s`, triggerBranches)
@@ -471,13 +498,9 @@ jobs:
471498
}
472499
}
473500

474-
// 2. Generate CI Workflow (only for git-images)
475501
if mode == "git-images" {
476-
// Use the selected branch for push triggers
502+
// Use ONLY the configured branch for push triggers
477503
triggerBranches := "[ " + gitBranch + " ]"
478-
if gitBranch == "main" || gitBranch == "master" || gitBranch == "develop" {
479-
triggerBranches = "[ main, develop ]"
480-
}
481504

482505
ciTemplate := fmt.Sprintf("name: CI/CD Pipeline ("+env+")\n\n"+`on:
483506
push:

internal/git/git.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,44 @@ func GetLatestCommit(dir, branch string) (string, error) {
6060
return strings.TrimSpace(string(output)), nil
6161
}
6262

63+
// IsDirty checks if there are any uncommitted changes in the repository
64+
func IsDirty(dir string) (bool, error) {
65+
cmd := exec.Command("git", "status", "--porcelain")
66+
cmd.Dir = dir
67+
output, err := cmd.Output()
68+
if err != nil {
69+
return false, fmt.Errorf("failed to check git status: %v", err)
70+
}
71+
return len(strings.TrimSpace(string(output))) > 0, nil
72+
}
73+
74+
// BranchExists checks if a branch exists locally
75+
func BranchExists(dir, branch string) (bool, error) {
76+
cmd := exec.Command("git", "show-ref", "--verify", "--quiet", "refs/heads/"+branch)
77+
cmd.Dir = dir
78+
err := cmd.Run()
79+
if err == nil {
80+
return true, nil
81+
}
82+
83+
// Also check remotes
84+
cmd = exec.Command("git", "show-ref", "--verify", "--quiet", "refs/remotes/origin/"+branch)
85+
cmd.Dir = dir
86+
err = cmd.Run()
87+
return err == nil, nil
88+
}
89+
90+
// CheckoutBranch switches to the specified branch
91+
func CheckoutBranch(dir, branch string) error {
92+
cmd := exec.Command("git", "checkout", branch)
93+
cmd.Dir = dir
94+
output, err := cmd.CombinedOutput()
95+
if err != nil {
96+
return fmt.Errorf("failed to checkout branch %s: %v\nOutput: %s", branch, err, string(output))
97+
}
98+
return nil
99+
}
100+
63101
// CreateArchive exports a git commit to a tarball, optionally filtering to specific paths
64102
// paths: optional list of paths to include (e.g., ["frontend/"] for service filtering)
65103
func CreateArchive(dir, commit, outputPath string, paths []string) error {

0 commit comments

Comments
 (0)