forked from gaia-pipeline/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_pipeline.go
115 lines (100 loc) · 3.19 KB
/
create_pipeline.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
package pipeline
import (
"fmt"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
)
const (
// Percent of pipeline creation progress after git clone
pipelineCloneStatus = 25
// Percent of pipeline creation progress after compile process done
pipelineCompileStatus = 75
// Completed percent progress
pipelineCompleteStatus = 100
)
// CreatePipeline is the main function which executes step by step the creation
// of a plugin.
// After each step, the status is written to store and can be retrieved via API.
func CreatePipeline(p *gaia.CreatePipeline) {
gitToken := p.GitHubToken
p.GitHubToken = ""
storeService, _ := services.StorageService()
// Define build process for the given type
bP := newBuildPipeline(p.Pipeline.Type)
if bP == nil {
// Pipeline type is not supported
p.StatusType = gaia.CreatePipelineFailed
p.Output = fmt.Sprintf("create pipeline failed. Pipeline type is not supported %s is not supported", p.Pipeline.Type)
storeService.CreatePipelinePut(p)
return
}
// Setup environment before cloning repo and command
err := bP.PrepareEnvironment(p)
if err != nil {
p.StatusType = gaia.CreatePipelineFailed
p.Output = fmt.Sprintf("cannot prepare build: %s", err.Error())
storeService.CreatePipelinePut(p)
return
}
// Clone git repo
err = gitCloneRepo(&p.Pipeline.Repo)
if err != nil {
p.StatusType = gaia.CreatePipelineFailed
p.Output = fmt.Sprintf("cannot prepare build: %s", err.Error())
storeService.CreatePipelinePut(p)
return
}
// Update status of our pipeline build
p.Status = pipelineCloneStatus
err = storeService.CreatePipelinePut(p)
if err != nil {
gaia.Cfg.Logger.Error("cannot put create pipeline into store", "error", err.Error())
return
}
// Run compile process
err = bP.ExecuteBuild(p)
if err != nil {
p.StatusType = gaia.CreatePipelineFailed
storeService.CreatePipelinePut(p)
return
}
// Update status of our pipeline build
p.Status = pipelineCompileStatus
err = storeService.CreatePipelinePut(p)
if err != nil {
gaia.Cfg.Logger.Error("cannot put create pipeline into store", "error", err.Error())
return
}
// Copy compiled binary to plugins folder
err = bP.CopyBinary(p)
if err != nil {
p.StatusType = gaia.CreatePipelineFailed
p.Output = fmt.Sprintf("cannot copy compiled binary: %s", err.Error())
storeService.CreatePipelinePut(p)
return
}
// Save the generated pipeline data
err = bP.SavePipeline(&p.Pipeline)
if err != nil {
p.StatusType = gaia.CreatePipelineFailed
p.Output = fmt.Sprintf("failed to save the created pipeline: %s", err.Error())
storeService.CreatePipelinePut(p)
return
}
// Set create pipeline status to complete
p.Status = pipelineCompleteStatus
p.StatusType = gaia.CreatePipelineSuccess
err = storeService.CreatePipelinePut(p)
if err != nil {
gaia.Cfg.Logger.Error("cannot put create pipeline into store", "error", err.Error())
return
}
if !gaia.Cfg.Poll && len(gitToken) > 0 {
// if there is a githubtoken provided, that means that a webhook was requested to be added.
err = createGithubWebhook(gitToken, &p.Pipeline.Repo, nil)
if err != nil {
gaia.Cfg.Logger.Error("error while creating webhook for repository", "error", err.Error())
return
}
}
}