-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
201 lines (177 loc) · 4.79 KB
/
main.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"fmt"
"strings"
"github.com/aweris/gale/common/log"
)
func New(
// Context of the operation.
ctx context.Context,
// Directory containing the repository source. If source is provided, rest of the options are ignored.
// +optional=true
source *Directory,
// Name of the repository. Format: owner/name.
// +optional=true
repo string,
// Name to check out. Only one of branch or tag can be used. Precedence is as follows: tag, branch.
// +optional=true
tag string,
// Branch name to check out. Only one of branch or tag can be used. Precedence is as follows: tag, branch.
// +optional=true
branch string,
// Path to the workflows' directory.
// +optional=true
// +default=.github/workflows
workflowsDir string,
) (*Gale, error) {
info, err := NewRepoInfo(ctx, source, repo, tag, branch)
if err != nil {
return nil, err
}
return &Gale{Repo: info, Workflows: info.workflows(workflowsDir)}, nil
}
type Gale struct {
// Repository information
Repo *RepoInfo
// Workflows in the repository
Workflows *Workflows
}
// List returns a list of workflows and their jobs.
func (g *Gale) List(ctx context.Context) (string, error) {
workflows, err := g.Workflows.List(ctx)
if err != nil {
return "", err
}
sb := &strings.Builder{}
var (
indentation = " "
newline = "\n"
)
for _, workflow := range workflows {
sb.WriteString("- Workflow: ")
if workflow.Name != "" {
sb.WriteString(fmt.Sprintf("%s (path: %s)", workflow.Name, workflow.Path))
} else {
sb.WriteString(workflow.Path)
}
sb.WriteString(newline)
sb.WriteString(indentation)
sb.WriteString("Jobs:")
sb.WriteString(newline)
for _, job := range workflow.Jobs {
sb.WriteString(indentation)
sb.WriteString(fmt.Sprintf(" - %s", job.JobID))
sb.WriteString(newline)
}
sb.WriteString("\n") // extra empty line
}
return sb.String(), nil
}
func (g *Gale) Run(
// Context to use for the operation
ctx context.Context,
// External workflow file to run.
// +optional=true
workflowFile *File,
// Name of the workflow to run.
// +optional=true
workflow string,
// Name of the job to run. If empty, all jobs will be run.
// +optional=true
job string,
// Name of the event that triggered the workflow. e.g. push
// +optional=true
// +default=push
event string,
// File with the complete webhook event payload.
// +optional=true
eventFile *File,
// Container to use for the runner(default: ghcr.io/catthehacker/ubuntu:act-latest).
// +optional=true
container *Container,
// Enables debug mode.
// +optional=true
// +default=false
runnerDebug bool,
// Enables native Docker support, allowing direct execution of Docker commands in the workflow.
// +optional=true
// +default=true
useNativeDocker bool,
// Sets DOCKER_HOST to use for the native docker support.
// +optional=true
// +default=unix:///var/run/docker.sock
dockerHost string,
// Enables docker-in-dagger support to be able to run docker commands isolated from the host. Enabling DinD may lead to longer execution times.
// +optional=true
// +default=false
useDind bool,
// GitHub token to use for authentication.
// +optional=true
token *Secret,
) (*WorkflowRun, error) {
if eventFile == nil {
eventFile = dag.Directory().WithNewFile("event.json", "{}").File("event.json")
}
if container == nil {
container = dag.Container().From("ghcr.io/catthehacker/ubuntu:act-latest")
}
if useNativeDocker && useDind {
useNativeDocker = false
log.Warnf("Both enableDocker and useDind are enabled. Using DinD to run docker commands.")
}
if useNativeDocker {
log.Warnf("Using native docker support. Please ensure that DOCKER_HOST is set correctly.", "DOCKER_HOST", dockerHost)
}
if useDind {
log.Warnf("Enabling DinD may lead to longer execution times.")
}
planner := NewWorkflowExecutionPlanner(
g.Repo,
g.Workflows,
&WorkflowRunOpts{
WorkflowFile: workflowFile,
Workflow: workflow,
Job: job,
},
&RunnerOpts{
Ctr: container,
Debug: runnerDebug,
UseNativeDocker: useNativeDocker,
DockerHost: dockerHost,
UseDind: useDind,
},
&EventOpts{
Name: event,
File: eventFile,
},
&SecretOpts{
Token: token,
},
)
executor, err := planner.Plan(ctx)
if err != nil {
return nil, err
}
return executor.Execute(ctx)
}
func (g *Gale) Action(
// ID of the step. Defaults to the step index in the job.
// +optional=true
stepID string,
// External workflow file to run.
// +optional=false
uses string,
// Environment variables for the action. Format: name=value.
// +optional=true
env []string,
// Input parameters for the action. Format: name=value.
// +optional=true
with []string,
) (*Actions, error) {
actions := &Actions{
Repo: g.Repo,
Workflows: g.Workflows,
}
return actions.Action(stepID, uses, env, with)
}