/
main.go
262 lines (205 loc) · 8.67 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package build_and_publish
import (
"fmt"
"github.com/spf13/cobra"
"github.com/werf/logboek"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/container_runtime"
"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/logging"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
"github.com/werf/werf/pkg/tmp_manager"
"github.com/werf/werf/pkg/true_git"
"github.com/werf/werf/pkg/werf"
)
var cmdData struct {
PullUsername string
PullPassword string
}
var commonCmdData common.CmdData
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "build-and-publish [IMAGE_NAME...]",
Short: "Build stages and publish images",
Long: common.GetLongCommandDescription(`Build stages and final images using each specified tag with the tagging strategy and push into images repo.
Command combines 'werf stages build' and 'werf images publish'.
After stages has been built, new docker layer with service info about tagging strategy will be built for each tag of each image from werf.yaml. Images will be pushed into docker repo with the names IMAGES_REPO/IMAGE_NAME:TAG.
The result of build-and-publish command is stages in stages storage and named images pushed into the docker repo.
If one or more IMAGE_NAME parameters specified, werf will build images stages and publish only these images from werf.yaml`),
Example: ` # Build and publish all images from werf.yaml into specified docker repo, built stages will be placed locally; tag images with the mytag tag using custom tagging strategy
$ werf build-and-publish --stages-storage :local --images-repo registry.mydomain.com/myproject --tag-custom mytag
# Build and publish all images from werf.yaml into minikube registry; tag images with the mybranch tag, using git-branch tagging strategy
$ werf build-and-publish --stages-storage :local --images-repo :minikube --tag-git-branch mybranch
# Build and publish with enabled drop-in shell session in the failed assembly container in the case when an error occurred
$ werf build-and-publish --stages-storage :local --introspect-error --images-repo :minikube --tag-git-branch mybranch
# Set --stages-storage default value using $WERF_STAGES_STORAGE param and --images-repo default value using $WERF_IMAGE_REPO param
$ export WERF_STAGES_STORAGE=:local
$ export WERF_IMAGES_REPO=myregistry.mydomain.com/myproject
$ werf build-and-publish --tag-git-tag v1.4.9`,
DisableFlagsInUseLine: true,
Annotations: map[string]string{
common.CmdEnvAnno: common.EnvsDescription(common.WerfDebugAnsibleArgs),
},
RunE: func(cmd *cobra.Command, args []string) error {
defer werf.PrintGlobalWarnings(common.BackgroundContext())
if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
return err
}
common.LogVersion()
return common.LogRunningTime(func() error {
return runBuildAndPublish(args)
})
},
}
common.SetupDir(&commonCmdData, cmd)
common.SetupConfigPath(&commonCmdData, cmd)
common.SetupConfigTemplatesDir(&commonCmdData, cmd)
common.SetupTmpDir(&commonCmdData, cmd)
common.SetupHomeDir(&commonCmdData, cmd)
common.SetupSSHKey(&commonCmdData, cmd)
common.SetupStagesStorageOptions(&commonCmdData, cmd)
common.SetupImagesRepoOptions(&commonCmdData, cmd)
common.SetupTag(&commonCmdData, cmd)
common.SetupDockerConfig(&commonCmdData, cmd, "Command needs granted permissions to read, pull and push images into the specified stages storage, to push images into the specified images repo, to pull base images")
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
common.SetupLogOptions(&commonCmdData, cmd)
common.SetupLogProjectDir(&commonCmdData, cmd)
common.SetupIntrospectAfterError(&commonCmdData, cmd)
common.SetupIntrospectBeforeError(&commonCmdData, cmd)
common.SetupIntrospectStage(&commonCmdData, cmd)
common.SetupSynchronization(&commonCmdData, cmd)
common.SetupKubeConfig(&commonCmdData, cmd)
common.SetupKubeConfigBase64(&commonCmdData, cmd)
common.SetupKubeContext(&commonCmdData, cmd)
common.SetupPublishReportPath(&commonCmdData, cmd)
common.SetupPublishReportFormat(&commonCmdData, cmd)
common.SetupVirtualMerge(&commonCmdData, cmd)
common.SetupVirtualMergeFromCommit(&commonCmdData, cmd)
common.SetupVirtualMergeIntoCommit(&commonCmdData, cmd)
common.SetupGitUnshallow(&commonCmdData, cmd)
common.SetupAllowGitShallowClone(&commonCmdData, cmd)
common.SetupParallelOptions(&commonCmdData, cmd, common.DefaultBuildParallelTasksLimit)
return cmd
}
func runBuildAndPublish(imagesToProcess []string) error {
tmp_manager.AutoGCEnabled = true
ctx := common.BackgroundContext()
werf.PostponeMultiwerfNotUpToDateWarning()
if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
}
if err := image.Init(); err != nil {
return err
}
if err := lrumeta.Init(); err != nil {
return err
}
if err := true_git.Init(true_git.Options{LiveGitOutput: *commonCmdData.LogVerbose || *commonCmdData.LogDebug}); err != nil {
return err
}
if err := common.DockerRegistryInit(&commonCmdData); err != nil {
return err
}
if err := docker.Init(ctx, *commonCmdData.DockerConfig, *commonCmdData.LogVerbose, *commonCmdData.LogDebug); err != nil {
return err
}
ctxWithDockerCli, err := docker.NewContext(ctx)
if err != nil {
return err
}
ctx = ctxWithDockerCli
projectDir, err := common.GetProjectDir(&commonCmdData)
if err != nil {
return fmt.Errorf("getting project dir failed: %s", err)
}
common.ProcessLogProjectDir(&commonCmdData, projectDir)
werfConfig, err := common.GetRequiredWerfConfig(ctx, projectDir, &commonCmdData, true)
if err != nil {
return fmt.Errorf("unable to load werf config: %s", err)
}
for _, imageToProcess := range imagesToProcess {
if !werfConfig.HasImage(imageToProcess) {
return fmt.Errorf("specified image %s is not defined in werf.yaml", logging.ImageLogName(imageToProcess, false))
}
}
projectName := werfConfig.Meta.Project
projectTmpDir, err := tmp_manager.CreateProjectDir(ctx)
if err != nil {
return fmt.Errorf("getting project tmp dir failed: %s", err)
}
defer tmp_manager.ReleaseProjectDir(projectTmpDir)
containerRuntime := &container_runtime.LocalDockerServerRuntime{} // TODO
stagesStorage, err := common.GetStagesStorage(containerRuntime, &commonCmdData)
if err != nil {
return err
}
synchronization, err := common.GetSynchronization(ctx, &commonCmdData, projectName, stagesStorage)
if err != nil {
return err
}
stagesStorageCache, err := common.GetStagesStorageCache(synchronization)
if err != nil {
return err
}
storageLockManager, err := common.GetStorageLockManager(ctx, synchronization)
if err != nil {
return err
}
storageManager := manager.NewStorageManager(projectName, storageLockManager, stagesStorageCache)
if err := storageManager.UseStagesStorage(ctx, stagesStorage); err != nil {
return err
}
imagesRepo, err := common.GetImagesRepo(ctx, projectName, &commonCmdData)
if err != nil {
return err
}
tagOpts, err := common.GetTagOptions(&commonCmdData, common.TagOptionsGetterOptions{})
if err != nil {
return err
}
if err := ssh_agent.Init(ctx, common.GetSSHKey(&commonCmdData)); err != nil {
return fmt.Errorf("cannot initialize ssh agent: %s", err)
}
defer func() {
err := ssh_agent.Terminate()
if err != nil {
logboek.Warn().LogF("WARNING: ssh agent termination failed: %s\n", err)
}
}()
buildStagesOptions, err := common.GetBuildStagesOptions(&commonCmdData, werfConfig)
if err != nil {
return err
}
publishReportFormat, err := common.GetPublishReportFormat(&commonCmdData)
if err != nil {
return err
}
buildAndPublishOptions := build.BuildAndPublishOptions{
BuildStagesOptions: buildStagesOptions,
PublishImagesOptions: build.PublishImagesOptions{
ImagesToPublish: imagesToProcess,
TagOptions: tagOpts,
PublishReportPath: *commonCmdData.PublishReportPath,
PublishReportFormat: publishReportFormat,
},
}
conveyorOptions, err := common.GetConveyorOptionsWithParallel(&commonCmdData, buildAndPublishOptions.BuildStagesOptions)
if err != nil {
return err
}
logboek.LogOptionalLn()
conveyorWithRetry := build.NewConveyorWithRetryWrapper(werfConfig, imagesToProcess, projectDir, projectTmpDir, ssh_agent.SSHAuthSock, containerRuntime, storageManager, imagesRepo, storageLockManager, conveyorOptions)
defer conveyorWithRetry.Terminate()
if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
return c.BuildAndPublish(ctx, buildAndPublishOptions)
}); err != nil {
return err
}
return nil
}