-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.go
More file actions
120 lines (107 loc) · 2.87 KB
/
deploy.go
File metadata and controls
120 lines (107 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package greengo
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
)
const (
defaultBranch = "main"
defaultWorkspace = "greengo-workspace"
defaultComposeCommand = "docker"
)
// DeployConfig describes the default Git to Docker Compose deployment flow.
type DeployConfig struct {
RepoURL string
Branch string
Workspace string
ComposeFiles []string
ComposeCommand string
Build bool
Detach bool
}
// Normalize fills safe defaults for optional deployment settings.
func (c DeployConfig) Normalize() DeployConfig {
if c.Branch == "" {
c.Branch = defaultBranch
}
if c.Workspace == "" {
c.Workspace = defaultWorkspace
}
if c.ComposeCommand == "" {
c.ComposeCommand = defaultComposeCommand
}
return c
}
// Validate checks the required deployment settings.
func (c DeployConfig) Validate() error {
if c.RepoURL == "" {
return errors.New("repository URL is required")
}
return nil
}
// NewDockerComposePipeline returns a deployment pipeline that clones the target
// repository and runs Docker Compose from the cloned project.
func NewDockerComposePipeline(config DeployConfig, opts ...Option) (*Pipeline, error) {
config = config.Normalize()
if err := config.Validate(); err != nil {
return nil, err
}
pipeline := NewPipeline("docker-compose-deploy", opts...)
pipeline.AddStage("prepare workspace", func(ctx context.Context, run RunContext) error {
workspace := run.Workspace
if workspace == "" {
workspace = config.Workspace
}
return EnsureCleanDir(workspace)
})
pipeline.AddStage("clone repository", func(ctx context.Context, run RunContext) error {
workspace := run.Workspace
if workspace == "" {
workspace = config.Workspace
}
parent := filepath.Dir(workspace)
if err := os.MkdirAll(parent, 0o755); err != nil {
return err
}
_, err := run.Runner.Run(ctx, Command{
Name: "git",
Args: []string{"clone", "--branch", config.Branch, "--single-branch", config.RepoURL, workspace},
})
return err
})
pipeline.AddStage("docker compose up", func(ctx context.Context, run RunContext) error {
workspace := run.Workspace
if workspace == "" {
workspace = config.Workspace
}
args := composeBaseArgs(config.ComposeCommand)
for _, file := range config.ComposeFiles {
args = append(args, "-f", file)
}
args = append(args, "up")
if config.Build {
args = append(args, "--build")
}
if config.Detach {
args = append(args, "-d")
}
_, err := run.Runner.Run(ctx, Command{
Name: config.ComposeCommand,
Args: args,
Dir: workspace,
})
if err != nil && config.ComposeCommand == defaultComposeCommand {
return fmt.Errorf("%w; install Docker Compose v2 or set ComposeCommand to docker-compose", err)
}
return err
})
return pipeline, nil
}
func composeBaseArgs(command string) []string {
if command == "docker-compose" {
return nil
}
return []string{"compose"}
}