-
Notifications
You must be signed in to change notification settings - Fork 414
/
main.go
170 lines (144 loc) · 5.8 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
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
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/rockbears/log"
"github.com/ovh/cds/contrib/grpcplugins"
art "github.com/ovh/cds/contrib/integrations/artifactory"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/artifact_manager"
"github.com/ovh/cds/sdk/grpcplugin/integrationplugin"
)
/*
This plugin have to be used as a build info plugin
Artifactory build info plugin must configured as following:
name: artifactory-build-info-plugin
type: integration
author: "Steven Guiheux"
description: "OVH Artifactory Build Info Plugin"
$ cdsctl admin plugins import artifactory-build-info-plugin.yml
Build the present binaries and import in CDS:
os: linux
arch: amd64
cmd: <path-to-binary-file>
$ cdsctl admin plugins binary-add artifactory-build-info-plugin artifactory-build-info-plugin-bin.yml <path-to-binary-file>
*/
type artifactoryBuildInfoPlugin struct {
integrationplugin.Common
}
type executionContext struct {
buildInfo string
projectKey string
workflowName string
version string
defaultLowMaturitySuffix string
}
func (e *artifactoryBuildInfoPlugin) Manifest(_ context.Context, _ *empty.Empty) (*integrationplugin.IntegrationPluginManifest, error) {
return &integrationplugin.IntegrationPluginManifest{
Name: "OVH Artifactory Build Info Plugin",
Author: "Steven Guiheux",
Description: "OVH Artifactory Build Info Plugin",
Version: sdk.VERSION,
}, nil
}
func (e *artifactoryBuildInfoPlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
log.Factory = log.NewStdWrapper(log.StdWrapperOptions{DisableTimestamp: true, Level: log.LevelInfo})
log.UnregisterField(log.FieldCaller, log.FieldSourceFile, log.FieldSourceLine, log.FieldStackTrace)
artifactoryURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigURL)]
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigToken)]
tokenName := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigTokenName)]
lowMaturitySuffix := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigPromotionLowMaturity)]
artifactoryProjectKey := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigProjectKey)]
buildInfo := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigBuildInfoPrefix)]
artifactClient, err := artifact_manager.NewClient("artifactory", artifactoryURL, token)
if err != nil {
return fail("Failed to create artifactory client: %s", err)
}
nodeRunURL := opts.GetOptions()["cds.ui.pipeline.run"]
runURL := nodeRunURL[0:strings.Index(nodeRunURL, "/node/")]
// Get the build agent from env variable set by worker
workerName := os.Getenv("CDS_WORKER")
if workerName == "" {
workerName = "CDS"
}
// Compute git url
gitUrl := opts.GetOptions()["git.url"]
if gitUrl == "" {
gitUrl = opts.GetOptions()["git.http_url"]
}
// Get run results
runResults, err := grpcplugins.GetRunResults(e.HTTPPort)
if err != nil {
return fail("unable to get run results: %v", err)
}
buildInfoRequest, err := art.PrepareBuildInfo(ctx, artifactClient, art.BuildInfoRequest{
BuildInfoPrefix: buildInfo,
ProjectKey: opts.GetOptions()["cds.project"],
WorkflowName: opts.GetOptions()["cds.workflow"],
Version: opts.GetOptions()["cds.version"],
AgentName: workerName,
TokenName: tokenName,
RunURL: runURL,
GitBranch: opts.GetOptions()["git.branch"],
GitMessage: opts.GetOptions()["git.message"],
GitURL: gitUrl,
GitHash: opts.GetOptions()["git.hash"],
RunResults: runResults,
DefaultLowMaturitySuffix: lowMaturitySuffix,
})
if err != nil {
return fail("unable to prepare build info: %v", err)
}
fmt.Printf("Creating Artifactory Build %s %s on project %s...\n", buildInfoRequest.Name, buildInfoRequest.Number, artifactoryProjectKey)
if err := artifactClient.DeleteBuild(artifactoryProjectKey, buildInfoRequest.Name, buildInfoRequest.Number); err != nil {
return fail("unable to clean existing build: %v", err)
}
var nbAttempts int
for {
nbAttempts++
err := artifactClient.PublishBuildInfo(artifactoryProjectKey, buildInfoRequest)
if err == nil {
break
} else if nbAttempts >= 3 {
return fail("unable to push build info: %v", err)
} else {
fmt.Printf("Error while pushing buildinfo %s %s. Retrying...\n", buildInfoRequest.Name, buildInfoRequest.Number)
}
}
// Temporary code
if opts.GetOptions()["cds.proj.xray.enabled"] == "true" {
fmt.Printf("Triggering XRay Build %s %s scan...\n", buildInfoRequest.Name, buildInfoRequest.Number)
// Scan build info
scanBuildRequest := services.NewXrayScanParams()
scanBuildRequest.BuildName = buildInfoRequest.Name
scanBuildRequest.BuildNumber = buildInfoRequest.Number
scanBuildRequest.ProjectKey = artifactoryProjectKey
scanBuildResponseBtes, err := artifactClient.XrayScanBuild(scanBuildRequest)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(string(scanBuildResponseBtes))
}
}
return &integrationplugin.RunResult{
Status: sdk.StatusSuccess,
}, nil
}
func main() {
e := artifactoryBuildInfoPlugin{}
if err := integrationplugin.Start(context.Background(), &e); err != nil {
panic(err)
}
}
func fail(format string, args ...interface{}) (*integrationplugin.RunResult, error) {
msg := fmt.Sprintf(format, args...)
fmt.Println(msg)
return &integrationplugin.RunResult{
Details: msg,
Status: sdk.StatusFail,
}, nil
}