forked from tilt-dev/tilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathci.go
108 lines (85 loc) · 3.03 KB
/
ci.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
package cli
import (
"context"
"fmt"
"log"
"time"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
"github.com/spf13/cobra"
"github.com/tilt-dev/tilt/internal/analytics"
"github.com/tilt-dev/tilt/internal/hud/prompt"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/logger"
"github.com/tilt-dev/tilt/pkg/model"
)
type ciCmd struct {
fileName string
outputSnapshotOnExit string
}
func (c *ciCmd) name() model.TiltSubcommand { return "ci" }
func (c *ciCmd) register() *cobra.Command {
cmd := &cobra.Command{
Use: "ci [<tilt flags>] [-- <Tiltfile args>]",
DisableFlagsInUseLine: true,
Short: "Start Tilt in CI/batch mode with the given Tiltfile args",
Long: fmt.Sprintf(`
Starts Tilt and runs resources defined in the Tiltfile.
Exits with failure if any build fails or any server crashes.
Exits with success if all tasks have completed successfully
and all servers are healthy.
While Tilt is running, you can view the UI at %s:%d
(configurable with --host and --port).
See blog post for additional information: https://blog.tilt.dev/2020/04/16/how-to-not-break-server-startup.html
`, defaultWebHost, defaultWebPort),
}
addStartServerFlags(cmd)
addDevServerFlags(cmd)
addTiltfileFlag(cmd, &c.fileName)
addKubeContextFlag(cmd)
addNamespaceFlag(cmd)
cmd.Flags().BoolVar(&logActionsFlag, "logactions", false, "log all actions and state changes")
cmd.Flags().Lookup("logactions").Hidden = true
cmd.Flags().StringVar(&c.outputSnapshotOnExit, "output-snapshot-on-exit", "",
"If specified, Tilt will dump a snapshot of its state to the specified path when it exits")
cmd.Flags().DurationVar(&ciTimeout, "timeout", model.CITimeoutDefault,
"Timeout to wait for CI to pass. Set to 0 for no timeout.")
return cmd
}
func (c *ciCmd) run(ctx context.Context, args []string) error {
a := analytics.Get(ctx)
a.Incr("cmd.ci", nil)
defer a.Flush(time.Second)
deferred := logger.NewDeferredLogger(ctx)
ctx = redirectLogs(ctx, deferred)
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
webHost := provideWebHost()
webURL, _ := provideWebURL(webHost, provideWebPort())
startLine := prompt.StartStatusLine(webURL, webHost)
log.Print(startLine)
log.Print(buildStamp())
if ok, reason := analytics.IsAnalyticsDisabledFromEnv(); ok {
log.Printf("Tilt analytics disabled: %s", reason)
}
cmdCIDeps, err := wireCmdCI(ctx, a, "ci")
if err != nil {
deferred.SetOutput(deferred.Original())
return err
}
upper := cmdCIDeps.Upper
l := store.NewLogActionLogger(ctx, upper.Dispatch)
deferred.SetOutput(l)
ctx = redirectLogs(ctx, l)
if c.outputSnapshotOnExit != "" {
defer cmdCIDeps.Snapshotter.WriteSnapshot(ctx, c.outputSnapshotOnExit)
}
err = upper.Start(ctx, args, cmdCIDeps.TiltBuild,
c.fileName, store.TerminalModeStream, a.UserOpt(), cmdCIDeps.Token,
string(cmdCIDeps.CloudAddress))
if err == nil {
_, _ = fmt.Fprintln(colorable.NewColorableStdout(),
color.GreenString("SUCCESS. All workloads are healthy."))
}
return err
}
var ciTimeout time.Duration