-
Notifications
You must be signed in to change notification settings - Fork 18
/
run.go
216 lines (180 loc) · 7.7 KB
/
run.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//go:build linux || darwin || windows
// +build linux darwin windows
package commands
import (
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/internal/commands/helpers"
"github.com/cirruslabs/cirrus-cli/internal/commands/logs"
"github.com/cirruslabs/cirrus-cli/internal/executor"
eenvironment "github.com/cirruslabs/cirrus-cli/internal/executor/environment"
"github.com/cirruslabs/cirrus-cli/internal/executor/instance/containerbackend"
"github.com/cirruslabs/cirrus-cli/internal/executor/options"
"github.com/cirruslabs/cirrus-cli/internal/executor/taskfilter"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs/local"
"github.com/cirruslabs/cirrus-cli/pkg/parser"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parsererror"
"github.com/spf13/cobra"
"os"
"strings"
)
var ErrRun = errors.New("run failed")
// General flags.
var artifactsDir string
var dirty bool
var output string
var environment []string
var affectedFiles []string
var affectedFilesGitRevision string
var affectedFilesGitCachedRevision string
var verbose bool
// Common instance-related flags.
var lazyPull bool
// Container-related flags.
var containerBackendType string
var containerLazyPull bool
// Container-related flags: Dockerfile as CI environment[1] feature.
// [1]: https://cirrus-ci.org/guide/docker-builder-vm/#dockerfile-as-a-ci-environment
var dockerfileImageTemplate string
var dockerfileImagePush bool
// Tart-related flags.
var tartLazyPull bool
// Flags useful for debugging.
var debugNoCleanup bool
func run(cmd *cobra.Command, args []string) error {
// https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true
projectDir := "."
baseEnvironment := eenvironment.Merge(
eenvironment.Static(),
eenvironment.BuildID(),
eenvironment.ProjectSpecific(projectDir),
)
userSpecifiedEnvironment := helpers.EnvArgsToMap(environment)
// Retrieve the combined YAML configuration
combinedYAML, err := helpers.ReadCombinedConfig(cmd.Context(),
eenvironment.Merge(baseEnvironment, userSpecifiedEnvironment))
if err != nil {
return err
}
if affectedFilesGitRevision != "" {
affectedFilesFromGit, err := helpers.GitDiff(projectDir, affectedFilesGitRevision, false)
if err != nil {
return err
}
affectedFiles = append(affectedFiles, affectedFilesFromGit...)
}
if affectedFilesGitCachedRevision != "" {
affectedFilesFromGit, err := helpers.GitDiff(projectDir, affectedFilesGitCachedRevision, true)
if err != nil {
return err
}
affectedFiles = append(affectedFiles, affectedFilesFromGit...)
}
// Parse
p := parser.New(
parser.WithEnvironment(eenvironment.Merge(eenvironment.Static(), userSpecifiedEnvironment)),
parser.WithMissingInstancesAllowed(),
parser.WithAffectedFiles(affectedFiles),
parser.WithFileSystem(local.New(projectDir)),
)
result, err := p.Parse(cmd.Context(), combinedYAML)
if err != nil {
if re, ok := err.(*parsererror.Rich); ok {
fmt.Print(re.ContextLines())
}
return err
}
var executorOpts []executor.Option
// Enable logging
logger, cancel := logs.GetLogger(output, verbose, cmd.OutOrStdout(), os.Stdout)
defer cancel()
executorOpts = append(executorOpts, executor.WithLogger(logger))
// Enable a task filter if the task name is specified
if len(args) == 1 {
taskFilter := taskfilter.MatchExactTask(args[0])
executorOpts = append(executorOpts, executor.WithTaskFilter(taskFilter))
}
// Artifacts directory
if artifactsDir != "" {
executorOpts = append(executorOpts, executor.WithArtifactsDir(artifactsDir))
}
// Dirty mode
if dirty {
executorOpts = append(executorOpts, executor.WithDirtyMode())
}
// Container-related options
executorOpts = append(executorOpts, executor.WithContainerOptions(options.ContainerOptions{
LazyPull: lazyPull || containerLazyPull,
NoCleanup: debugNoCleanup,
DockerfileImageTemplate: dockerfileImageTemplate,
DockerfileImagePush: dockerfileImagePush,
}))
// Tart-related options
executorOpts = append(executorOpts, executor.WithTartOptions(options.TartOptions{
LazyPull: lazyPull || tartLazyPull,
}))
// Environment
executorOpts = append(executorOpts,
executor.WithBaseEnvironmentOverride(baseEnvironment),
executor.WithUserSpecifiedEnvironment(userSpecifiedEnvironment),
)
// Container backend
executorOpts = append(executorOpts, executor.WithContainerBackendType(containerBackendType))
// Run
e, err := executor.New(projectDir, result.Tasks, executorOpts...)
if err != nil {
return err
}
return e.Run(cmd.Context())
}
func newRunCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "run [flags] [task]",
Short: "Execute Cirrus CI tasks locally",
RunE: run,
Args: cobra.MaximumNArgs(1),
}
// General flags
cmd.PersistentFlags().StringVar(&artifactsDir, "artifacts-dir", "",
"directory in which to save the artifacts")
cmd.PersistentFlags().BoolVar(&dirty, "dirty", false, "if set the project directory will be mounted"+
"in read-write mode, otherwise the project directory files are copied, taking .gitignore into account")
cmd.PersistentFlags().StringArrayVarP(&environment, "environment", "e", []string{},
"set (-e A=B) or pass-through (-e A) an environment variable")
cmd.PersistentFlags().StringSliceVar(&affectedFiles, "affected-files", []string{},
"comma-separated list of files to add to the list of affected files (used in changesInclude and "+
"changesIncludeOnly functions)")
cmd.PersistentFlags().StringVar(&affectedFilesGitRevision, "affected-files-git", "",
"Git revision (e.g. HEAD, v0.1.0 or commit SHA) to compare unstaged changes against and "+
"add changed files to the list of affected files (similarly to git diff)")
cmd.PersistentFlags().StringVar(&affectedFilesGitCachedRevision, "affected-files-git-cached", "",
"Git revision (e.g. HEAD, v0.1.0 or commit SHA) to compare staged changes against and "+
"add changed files to the list of affected files (similarly to git diff --cached)")
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "")
cmd.PersistentFlags().StringVarP(&output, "output", "o", logs.DefaultFormat(), fmt.Sprintf("output format of logs, "+
"supported values: %s", strings.Join(logs.Formats(), ", ")))
// Common instance-related flags
cmd.PersistentFlags().BoolVar(&lazyPull, "lazy-pull", false,
"attempt to pull container and VM images only if they are missing locally "+
"(helpful in case of registry rate limits; enables --container-lazy-pull and --tart-lazy-pull)")
// Container-related flags
cmd.PersistentFlags().StringVar(&containerBackendType, "container-backend", containerbackend.BackendTypeAuto,
fmt.Sprintf("container engine backend to use, either \"%s\", \"%s\" or \"%s\"",
containerbackend.BackendTypeDocker, containerbackend.BackendTypePodman, containerbackend.BackendTypeAuto))
cmd.PersistentFlags().BoolVar(&containerLazyPull, "container-lazy-pull", false,
"attempt to pull images only if they are missing locally (helpful in case of registry rate limits)")
// Container-related flags: Dockerfile as CI environment feature
cmd.PersistentFlags().StringVar(&dockerfileImageTemplate, "dockerfile-image-template",
"gcr.io/cirrus-ci-community/%s:latest", "image that Dockerfile as CI environment feature should produce")
cmd.PersistentFlags().BoolVar(&dockerfileImagePush, "dockerfile-image-push",
false, "whether to push whe image produced by the Dockerfile as CI environment feature")
// Tart-related flags
cmd.PersistentFlags().BoolVar(&tartLazyPull, "tart-lazy-pull", false,
"attempt to pull Tart VM images only if they are missing locally (helpful in case of registry rate limits)")
// Flags useful for debugging
cmd.PersistentFlags().BoolVar(&debugNoCleanup, "debug-no-cleanup", false,
"don't remove containers and volumes after execution")
_ = cmd.PersistentFlags().MarkHidden("debug-no-cleanup")
return cmd
}