-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathdeployment.go
141 lines (127 loc) · 4.2 KB
/
deployment.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
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trigger
import (
"context"
"fmt"
"path/filepath"
"time"
"github.com/google/uuid"
"go.uber.org/zap"
"github.com/pipe-cd/pipe/pkg/app/api/service/pipedservice"
"github.com/pipe-cd/pipe/pkg/config"
"github.com/pipe-cd/pipe/pkg/git"
"github.com/pipe-cd/pipe/pkg/model"
)
func (t *Trigger) triggerDeployment(ctx context.Context, app *model.Application, branch string, commit git.Commit, commander string) (runErr error) {
deployment, err := buildDeploment(app, branch, commit, commander, time.Now())
if err != nil {
return err
}
defer func() {
if runErr != nil {
return
}
var envName string
if env, ok := t.environmentLister.Get(deployment.EnvId); ok {
envName = env.Name
}
t.notifier.Notify(model.Event{
Type: model.EventType_EVENT_DEPLOYMENT_TRIGGERED,
Metadata: &model.EventDeploymentTriggered{
Deployment: deployment,
EnvName: envName,
},
})
}()
t.logger.Info(fmt.Sprintf("application %s will be triggered to sync", app.Id),
zap.String("commit-hash", commit.Hash),
)
req := &pipedservice.CreateDeploymentRequest{
Deployment: deployment,
}
if _, err = t.apiClient.CreateDeployment(ctx, req); err != nil {
t.logger.Error("failed to create deployment", zap.Error(err))
return err
}
if err := t.reportMostRecentlyTriggeredDeployment(ctx, deployment); err != nil {
t.logger.Error("failed to report most recently triggered deployment", zap.Error(err))
}
return nil
}
func (t *Trigger) loadDeploymentConfiguration(repoPath string, app *model.Application) (*config.Config, error) {
path := filepath.Join(repoPath, app.GitPath.GetDeploymentConfigFilePath())
cfg, err := config.LoadFromYAML(path)
if err != nil {
return nil, err
}
if appKind, ok := config.ToApplicationKind(cfg.Kind); !ok || appKind != app.Kind {
return nil, fmt.Errorf("application in deployment configuration file is not match, got: %s, expected: %s", appKind, app.Kind)
}
return cfg, nil
}
func (t *Trigger) reportMostRecentlyTriggeredDeployment(ctx context.Context, d *model.Deployment) error {
var (
err error
req = &pipedservice.ReportApplicationMostRecentDeploymentRequest{
ApplicationId: d.ApplicationId,
Status: model.DeploymentStatus_DEPLOYMENT_PENDING,
Deployment: &model.ApplicationDeploymentReference{
DeploymentId: d.Id,
Trigger: d.Trigger,
Summary: d.Summary,
Version: d.Version,
StartedAt: d.CreatedAt,
CompletedAt: d.CompletedAt,
},
}
retry = pipedservice.NewRetry(10)
)
for retry.WaitNext(ctx) {
if _, err = t.apiClient.ReportApplicationMostRecentDeployment(ctx, req); err == nil {
return nil
}
err = fmt.Errorf("failed to report most recent successful deployment: %w", err)
}
return err
}
func buildDeploment(app *model.Application, branch string, commit git.Commit, commander string, now time.Time) (*model.Deployment, error) {
deployment := &model.Deployment{
Id: uuid.New().String(),
ApplicationId: app.Id,
ApplicationName: app.Name,
EnvId: app.EnvId,
PipedId: app.PipedId,
ProjectId: app.ProjectId,
Kind: app.Kind,
Trigger: &model.DeploymentTrigger{
Commit: &model.Commit{
Hash: commit.Hash,
Message: commit.Message,
Author: commit.Author,
Branch: branch,
CreatedAt: int64(commit.CreatedAt),
},
Commander: commander,
Timestamp: now.Unix(),
},
GitPath: app.GitPath,
CloudProvider: app.CloudProvider,
Status: model.DeploymentStatus_DEPLOYMENT_PENDING,
StatusReason: "The deployment is waiting to be planned",
CreatedAt: now.Unix(),
UpdatedAt: now.Unix(),
}
return deployment, nil
}