-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (121 loc) · 3.78 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
package main
import (
_ "embed"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/env"
"github.com/bitrise-io/go-utils/errorutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/sliceutil"
)
//go:embed checks.bitrise.yml
var checkConfig string
//go:embed .yamllint.yml
var yamllintConfig string
const e2eWorkflow = "e2e"
const yamllintEnvKey = "YAMLLINT_CONFIG_FILE"
// Config ...
type Config struct {
WorkDir string `env:"step_dir,dir"`
Workflow []string `env:"workflow,multiline"`
SkipStepYMLValidation bool `env:"skip_step_yml_validation,opt[yes,no]"`
SkipGoChecks bool `env:"skip_go_checks,opt[yes,no]"`
SegmentWriteKey string `env:"SEGMENT_WRITE_KEY"`
ParentBuildURL string `env:"PARENT_BUILD_URL"`
IsCI bool `env:"CI"`
IsPR bool `env:"PR"`
}
func mainR() error {
envRepository := env.NewRepository()
commandFactory := command.NewFactory(envRepository)
var config Config
configParser := stepconf.NewInputParser(envRepository)
if err := configParser.Parse(&config); err != nil {
return fmt.Errorf("Invalid inputs: %v", err)
}
stepconf.Print(config)
var err error
config.WorkDir, err = pathutil.AbsPath(config.WorkDir)
if err != nil {
return fmt.Errorf("failed to expand path (%s): %v", config.WorkDir, err)
}
if len(config.Workflow) == 0 {
return fmt.Errorf("no workflow specified")
}
runE2EWorkflow := false
i := sliceutil.IndexOfStringInSlice(e2eWorkflow, config.Workflow)
if i != -1 {
runE2EWorkflow = true
config.Workflow = append(config.Workflow[:i], config.Workflow[i+1:]...)
}
if err := os.Chdir(config.WorkDir); err != nil {
return fmt.Errorf("failed to change working directory (%s): %v", config.WorkDir, err)
}
if runE2EWorkflow {
log.Donef("Running '%s' workflow", e2eWorkflow)
shouldFailOnFirstError := !config.IsCI || config.IsPR
if err := runE2E(commandFactory, config.WorkDir, shouldFailOnFirstError, config.SegmentWriteKey, config.ParentBuildURL); err != nil {
return fmt.Errorf("workflow %s failed: %w", e2eWorkflow, err)
}
log.Donef("Check '%s' succeeded", e2eWorkflow)
}
// Run other, non-e2e workflows
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
return err
}
configPath := filepath.Join(tmpDir, "bitrise.yml")
if err := ioutil.WriteFile(configPath, []byte(checkConfig), 0600); err != nil {
return err
}
yamllintPath := filepath.Join(tmpDir, ".yamllint.yml")
if err := ioutil.WriteFile(yamllintPath, []byte(yamllintConfig), 0600); err != nil {
return err
}
if err := os.Setenv(yamllintEnvKey, yamllintPath); err != nil {
return err
}
for _, wf := range config.Workflow {
workflowCmd :=
commandFactory.Create(
"bitrise",
[]string{"run", wf, "--config", configPath},
&command.Opts{
Dir: config.WorkDir,
Env: []string{
fmt.Sprintf("STEP_DIR=%s", config.WorkDir),
fmt.Sprintf("SKIP_STEP_YML_VALIDATION=%t", config.SkipStepYMLValidation),
fmt.Sprintf("SKIP_GO_CHECKS=%t", config.SkipGoChecks),
},
Stdout: os.Stdout,
Stderr: os.Stderr,
})
fmt.Println()
log.Donef("$ %s", workflowCmd.PrintableCommandArgs())
if err := workflowCmd.Run(); err != nil {
if errorutil.IsExitStatusError(err) {
return fmt.Errorf("workflow %s failed: %w", wf, err)
}
return fmt.Errorf("failed to run command: %v", err)
}
log.Donef("Check '%s' succeeded", wf)
}
return nil
}
func main() {
if err := mainR(); err != nil {
log.Errorf("%s", err)
var exitErr *exec.ExitError
if ok := errors.As(err, &exitErr); ok {
os.Exit(exitErr.ExitCode())
}
os.Exit(1)
}
}