-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
74 lines (67 loc) · 2.9 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
package queue
import (
"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/database"
"github.com/ovh/cds/engine/api/environment"
"github.com/ovh/cds/engine/api/pipeline"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/log"
"github.com/ovh/cds/sdk"
)
// RunPipeline the given pipeline with the given parameters
func RunPipeline(db database.QueryExecuter, projectKey string, app *sdk.Application, pipelineName string, environmentName string, params []sdk.Parameter, version int64, trigger sdk.PipelineBuildTrigger, user *sdk.User) (*sdk.PipelineBuild, error) {
// Load pipeline + Args
p, err := pipeline.LoadPipeline(db, projectKey, pipelineName, false)
if err != nil {
log.Warning("scheduler.Run> Cannot load pipeline %s: %s\n", pipelineName, err)
return nil, err
}
parameters, err := pipeline.GetAllParametersInPipeline(db, p.ID)
if err != nil {
log.Warning("scheduler.Run> Cannot load pipeline %s parameters: %s\n", pipelineName, err)
return nil, err
}
p.Parameter = parameters
// Pipeline type check
if p.Type == sdk.BuildPipeline && environmentName != "" && environmentName != sdk.DefaultEnv.Name {
log.Warning("scheduler.Run> Pipeline %s/%s/%s is a %s pipeline, but environment '%s' was provided\n", projectKey, app.Name, pipelineName, p.Type, environmentName)
return nil, sdk.ErrEnvironmentProvided
}
if p.Type != sdk.BuildPipeline && (environmentName == "" || environmentName == sdk.DefaultEnv.Name) {
log.Warning("scheduler.Run> Pipeline %s/%s/%s is a %s pipeline, but no environment was provided\n", projectKey, app.Name, pipelineName, p.Type)
return nil, sdk.ErrNoEnvironmentProvided
}
applicationPipelineParams, err := application.GetAllPipelineParam(db, app.ID, p.ID)
if err != nil {
log.Warning("scheduler.Run> Cannot load application pipeline args: %s\n", err)
return nil, err
}
// Load project + var
projectData, err := project.LoadProject(db, projectKey, user)
if err != nil {
log.Warning("scheduler.Run> Cannot load project %s: %s\n", projectKey, err)
return nil, err
}
projectsVar, err := project.GetAllVariableInProject(db, projectData.ID, project.WithClearPassword())
if err != nil {
log.Warning("scheduler.Run> Cannot load project variable: %s\n", err)
return nil, err
}
projectData.Variable = projectsVar
var env *sdk.Environment
if environmentName != "" && environmentName != sdk.DefaultEnv.Name {
env, err = environment.LoadEnvironmentByName(db, projectKey, environmentName)
if err != nil {
log.Warning("scheduler.Run> Cannot load environment %s for project %s: %s\n", environmentName, projectKey, err)
return nil, err
}
} else {
env = &sdk.DefaultEnv
}
pb, err := pipeline.InsertPipelineBuild(db, projectData, p, app, applicationPipelineParams, params, env, version, trigger)
if err != nil {
log.Warning("scheduler.Run> Cannot start pipeline %s: %s\n", pipelineName, err)
return nil, err
}
return &pb, nil
}