-
Notifications
You must be signed in to change notification settings - Fork 787
/
upgrade_boot.go
629 lines (557 loc) · 21.4 KB
/
upgrade_boot.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package upgrade
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/v2/pkg/boot"
"github.com/google/uuid"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/config"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/jenkins-x/jx/v2/pkg/gits/operations"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/jenkins-x/jx/v2/pkg/versionstream"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// UpgradeBootOptions options for the command
type UpgradeBootOptions struct {
*opts.CommonOptions
Dir string
UpgradeVersionStreamRef string
LatestRelease bool
Labels []string
}
var (
upgradeBootLong = templates.LongDesc(`
This command creates a pr for upgrading a jx boot gitOps cluster, incorporating changes to the boot
config and version stream ref
`)
upgradeBootExample = templates.Examples(`
# create pr for upgrading a jx boot gitOps cluster
jx upgrade boot
`)
filesExcludedFromCherryPick = []string{
"OWNERS",
}
)
const (
builderImage = "gcr.io/jenkinsxio/builder-go"
keepDevEnvKey = "keepDevEnv"
keepDevEnvDriverName = "Always keep the dev env version during merges and cherry-picks"
)
// NewCmdUpgradeBoot creates the command
func NewCmdUpgradeBoot(commonOpts *opts.CommonOptions) *cobra.Command {
options := &UpgradeBootOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "boot",
Short: "Upgrades jx boot config",
Long: upgradeBootLong,
Example: upgradeBootExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "the directory to look for the Jenkins X Pipeline and requirements")
cmd.Flags().StringVarP(&options.UpgradeVersionStreamRef, "upgrade-version-stream-ref", "", config.DefaultVersionsRef, "a version stream ref to use to upgrade to")
cmd.Flags().BoolVarP(&options.LatestRelease, "latest-release", "", false, "upgrade to latest release tag")
cmd.Flags().StringArrayVarP(&options.Labels, "labels", "", []string{}, "Labels to add to the generated upgrade PR")
return cmd
}
// Run runs this command
func (o *UpgradeBootOptions) Run() error {
err := o.setupGitConfig(o.Dir)
if err != nil {
return errors.Wrap(err, "failed to setup git config")
}
if o.Dir == "" {
err := o.cloneDevEnv()
if err != nil {
return errors.Wrap(err, "failed to clone dev environment repo")
}
}
requirements, requirementsFile, err := config.LoadRequirementsConfig(o.Dir, config.DefaultFailOnValidationError)
if err != nil {
return errors.Wrapf(err, "failed to load requirements config %s", requirementsFile)
}
reqsVersionStream := requirements.VersionStream
upgradeVersionRef, err := o.upgradeAvailable(reqsVersionStream.URL, reqsVersionStream.Ref, o.UpgradeVersionStreamRef)
if err != nil {
return errors.Wrap(err, "failed to get check for available update")
}
if upgradeVersionRef == "" {
return nil
}
localBranch, err := o.checkoutNewBranch()
if err != nil {
return errors.Wrap(err, "failed to checkout upgrade_branch")
}
bootConfigURL, err := o.determineBootConfigURL(reqsVersionStream.URL)
if err != nil {
return errors.Wrap(err, "failed to determine boot configuration URL")
}
err = o.updateBootConfig(reqsVersionStream.URL, reqsVersionStream.Ref, bootConfigURL, upgradeVersionRef)
if err != nil {
return errors.Wrap(err, "failed to update boot configuration")
}
err = o.updateVersionStreamRef(upgradeVersionRef)
if err != nil {
return errors.Wrap(err, "failed to update version stream ref")
}
resolver, err := o.CreateVersionResolver(reqsVersionStream.URL, o.UpgradeVersionStreamRef)
if err != nil {
return errors.Wrapf(err, "failed to create version resolver")
}
err = o.updatePipelineBuilderImage(resolver)
if err != nil {
return errors.Wrap(err, "failed to update pipeline version stream ref")
}
err = o.updateTemplateBuilderImage(resolver)
if err != nil {
return errors.Wrap(err, "failed to update template version stream ref")
}
// load modified requirements so we can merge with the base ones
modifiedRequirements, modifiedRequirementsFile, err := config.LoadRequirementsConfig(o.Dir, config.DefaultFailOnValidationError)
if err != nil {
return errors.Wrapf(err, "failed to load requirements config %s", modifiedRequirementsFile)
}
err = requirements.MergeSave(modifiedRequirements, modifiedRequirementsFile)
if err != nil {
return errors.Wrap(err, "error merging the modified jx-requirements.yml file with the dev environment's one")
}
err = o.createCommitForRequirements(requirementsFile)
if err != nil {
return errors.Wrap(err, "failed to create a merge commit for jx-requirements.yml")
}
err = o.raisePR()
if err != nil {
return errors.Wrap(err, "failed to raise pr")
}
err = o.deleteLocalBranch(localBranch)
if err != nil {
return errors.Wrapf(err, "failed to delete local branch %s", localBranch)
}
return nil
}
func (o UpgradeBootOptions) createCommitForRequirements(requirementsFileName string) error {
reqsChanged, err := o.Git().HasFileChanged(o.Dir, requirementsFileName)
if err != nil {
return errors.Wrap(err, "failed to list changed files")
}
if reqsChanged {
err := o.Git().AddCommitFiles(o.Dir, "Merge jx-requirements.yml", []string{requirementsFileName})
if err != nil {
return errors.Wrapf(err, "error creating a commit with the merged jx-requirements.yml file from dir %s",
requirementsFileName)
}
}
return nil
}
func (o UpgradeBootOptions) determineBootConfigURL(versionStreamURL string) (string, error) {
var bootConfigURL string
if versionStreamURL == config.DefaultVersionsURL {
bootConfigURL = config.DefaultBootRepository
}
if bootConfigURL == "" {
requirements, requirementsFile, err := config.LoadRequirementsConfig(o.Dir, config.DefaultFailOnValidationError)
if err != nil {
return "", errors.Wrapf(err, "failed to load requirements config %s", requirementsFile)
}
exists, err := util.FileExists(requirementsFile)
if err != nil {
return "", errors.Wrapf(err, "failed to check if file %s exists", requirementsFile)
}
if !exists {
return "", fmt.Errorf("no requirements file %s ensure you are running this command inside a GitOps clone", requirementsFile)
}
if requirements.BootConfigURL != "" {
bootConfigURL = requirements.BootConfigURL
}
}
if bootConfigURL == "" {
return "", fmt.Errorf("unable to determine default boot config URL")
}
log.Logger().Infof("using default boot config %s", bootConfigURL)
return bootConfigURL, nil
}
func (o *UpgradeBootOptions) upgradeAvailable(versionStreamURL string, versionStreamRef string, upgradeRef string) (string, error) {
versionsDir, _, err := o.CloneJXVersionsRepo(versionStreamURL, upgradeRef)
if err != nil {
return "", errors.Wrapf(err, "failed to clone versions repo %s", versionStreamURL)
}
if o.LatestRelease {
_, upgradeRef, err = o.Git().GetCommitPointedToByLatestTag(versionsDir)
if err != nil {
return "", errors.Wrapf(err, "failed to get latest tag at %s", o.Dir)
}
} else {
upgradeRef, err = o.Git().GetCommitPointedToByTag(versionsDir, upgradeRef)
if err != nil {
return "", errors.Wrapf(err, "failed to get commit pointed to by %s", upgradeRef)
}
}
if versionStreamRef == upgradeRef {
log.Logger().Infof(util.ColorInfo("No version stream upgrade available"))
return "", nil
}
log.Logger().Infof(util.ColorInfo("Version stream upgrade available"))
return upgradeRef, nil
}
func (o *UpgradeBootOptions) checkoutNewBranch() (string, error) {
localBranchUUID, err := uuid.NewUUID()
if err != nil {
return "", errors.Wrapf(err, "creating UUID for local branch")
}
localBranch := localBranchUUID.String()
err = o.Git().CreateBranch(o.Dir, localBranch)
if err != nil {
return "", errors.Wrapf(err, "failed to create local branch %s", localBranch)
}
err = o.Git().Checkout(o.Dir, localBranch)
if err != nil {
return "", errors.Wrapf(err, "failed to checkout local branch %s", localBranch)
}
return localBranch, nil
}
func (o *UpgradeBootOptions) updateVersionStreamRef(upgradeRef string) error {
requirements, requirementsFile, err := config.LoadRequirementsConfig(o.Dir, config.DefaultFailOnValidationError)
if err != nil {
return errors.Wrapf(err, "failed to load requirements file %s", requirementsFile)
}
if requirements.VersionStream.Ref != upgradeRef {
log.Logger().Infof("Upgrading version stream ref to %s", util.ColorInfo(upgradeRef))
requirements.VersionStream.Ref = upgradeRef
err = requirements.SaveConfig(requirementsFile)
if err != nil {
return errors.Wrapf(err, "failed to write version stream to %s", requirementsFile)
}
err = o.Git().AddCommitFiles(o.Dir, "feat: upgrade version stream", []string{requirementsFile})
if err != nil {
return errors.Wrapf(err, "failed to commit requirements file %s", requirementsFile)
}
}
return nil
}
func (o *UpgradeBootOptions) updateBootConfig(versionStreamURL string, versionStreamRef string, bootConfigURL string, upgradeVersionRef string) error {
configCloneDir, err := o.cloneBootConfig(bootConfigURL)
if err != nil {
return errors.Wrapf(err, "failed to clone boot config repo %s", bootConfigURL)
}
defer func() {
err := os.RemoveAll(configCloneDir)
if err != nil {
log.Logger().Infof("Error removing tmpDir: %v", err)
}
}()
currentSha, currentVersion, err := o.bootConfigRef(configCloneDir, versionStreamURL, versionStreamRef, bootConfigURL)
if err != nil {
return errors.Wrapf(err, "failed to get boot config ref for version stream: %s", versionStreamRef)
}
upgradeSha, upgradeVersion, err := o.bootConfigRef(configCloneDir, versionStreamURL, upgradeVersionRef, bootConfigURL)
if err != nil {
return errors.Wrapf(err, "failed to get boot config ref for version stream ref: %s", upgradeVersionRef)
}
// check if boot config upgrade available
if upgradeSha == currentSha {
log.Logger().Infof(util.ColorInfo("No boot config upgrade available"))
return nil
}
log.Logger().Infof(util.ColorInfo("boot config upgrade available"))
log.Logger().Infof("Upgrading from %s to %s", util.ColorInfo(currentVersion), util.ColorInfo(upgradeVersion))
// Fetch the tag we're upgrading from.
err = o.Git().FetchBranch(o.Dir, bootConfigURL, currentVersion)
if err != nil {
return errors.Wrapf(err, "failed to fetch current tag %s from %s", currentVersion, bootConfigURL)
}
// Fetch the tag we're upgrading to.
err = o.Git().FetchBranch(o.Dir, bootConfigURL, upgradeVersion)
if err != nil {
return errors.Wrapf(err, "failed to fetch upgrade tag %s from %s", upgradeVersion, bootConfigURL)
}
// Set up custom merge driver to ensure that specified files always use the local/dev env version in merges/cherry picks with conflicts
err = o.configureGitMergeExcludes()
if err != nil {
return errors.Wrapf(err, "configuring files (%s) to always use existing version when conflicting with upstream", strings.Join(filesExcludedFromCherryPick, ", "))
}
defer func() {
err = o.removeMergeExcludesFromAttributes()
}()
err = o.cherryPickCommits(configCloneDir, currentSha, upgradeSha)
if err != nil {
return errors.Wrap(err, "failed to cherry pick upgrade commits")
}
return nil
}
// Add a custom merge driver to .git/config that will always choose the current version when there's a change upstream
// in selected files, and use that merge driver in .git/info/attributes for those selected files.
// see https://stackoverflow.com/a/930495 for more details
func (o *UpgradeBootOptions) configureGitMergeExcludes() error {
err := o.Git().Config(o.Dir, "--local", fmt.Sprintf("merge.%s.name", keepDevEnvKey), keepDevEnvDriverName)
if err != nil {
return errors.Wrapf(err, "configuring name for exclude merge driver in %s", o.Dir)
}
err = o.Git().Config(o.Dir, "--local", fmt.Sprintf("merge.%s.driver", keepDevEnvKey), "true")
if err != nil {
return errors.Wrapf(err, "configuring driver for exclude merge driver in %s", o.Dir)
}
existingGitAttr, err := o.Git().ReadRepoAttributes(o.Dir)
if err != nil {
return errors.Wrapf(err, "reading repo-local git attributes")
}
// Write the existing .git/info/attributes content and marking the selected files as using our custom driver
gitAttrContent := existingGitAttr + "\n"
for _, excludedFile := range filesExcludedFromCherryPick {
newGitAttrLine := fmt.Sprintf("%s merge=%s\n", excludedFile, keepDevEnvKey)
if !strings.Contains(gitAttrContent, newGitAttrLine) {
gitAttrContent += newGitAttrLine
}
}
err = o.Git().WriteRepoAttributes(o.Dir, gitAttrContent)
if err != nil {
return errors.Wrapf(err, "writing new repo-local git attributes")
}
return nil
}
func (o *UpgradeBootOptions) removeMergeExcludesFromAttributes() error {
// Read the existing .git/info/attributes if it exists
existingGitAttr, err := o.Git().ReadRepoAttributes(o.Dir)
if err != nil {
return errors.Wrapf(err, "reading current repo-local git attributes")
}
if existingGitAttr != "" {
var newGitAttrLines []string
for _, l := range strings.Split(existingGitAttr, "\n") {
if !strings.Contains(l, fmt.Sprintf("merge=%s", keepDevEnvKey)) {
newGitAttrLines = append(newGitAttrLines, l)
}
}
err = o.Git().WriteRepoAttributes(o.Dir, strings.Join(newGitAttrLines, "\n"))
if err != nil {
return errors.Wrapf(err, "writing cleaned-up git attributes")
}
}
return nil
}
func (o *UpgradeBootOptions) bootConfigRef(dir string, versionStreamURL string, versionStreamRef string, configURL string) (string, string, error) {
resolver, err := o.CreateVersionResolver(versionStreamURL, versionStreamRef)
if err != nil {
return "", "", errors.Wrapf(err, "failed to create version resolver %s", configURL)
}
configVersion, err := resolver.ResolveGitVersion(configURL)
if err != nil {
return "", "", errors.Wrapf(err, "failed to resolve config url %s", configURL)
}
cmtSha, err := o.Git().GetCommitPointedToByTag(dir, fmt.Sprintf("v%s", configVersion))
if err != nil {
return "", "", errors.Wrapf(err, "failed to get commit pointed to by %s", cmtSha)
}
return cmtSha, "v" + configVersion, nil
}
func (o *UpgradeBootOptions) cloneBootConfig(configURL string) (string, error) {
cloneDir, err := ioutil.TempDir("", "")
err = os.MkdirAll(cloneDir, util.DefaultWritePermissions)
if err != nil {
return "", errors.Wrapf(err, "failed to create directory: %s", cloneDir)
}
err = o.Git().CloneBare(cloneDir, configURL)
if err != nil {
return "", errors.Wrapf(err, "failed to clone git URL %s to directory %s", configURL, cloneDir)
}
return cloneDir, nil
}
func (o *UpgradeBootOptions) cherryPickCommits(cloneDir, fromSha, toSha string) error {
cmts := make([]gits.GitCommit, 0)
cmts, err := o.Git().GetCommits(cloneDir, fromSha, toSha)
if err != nil {
return errors.Wrapf(err, "failed to get commits from %s", cloneDir)
}
log.Logger().Infof("cherry picking commits in the range %s..%s", fromSha, toSha)
for i := len(cmts) - 1; i >= 0; i-- {
commitSha := cmts[i].SHA
commitMsg := cmts[i].Subject()
// cherry-pick commits preserving redundant commits to avoid error
err := o.Git().CherryPickTheirsKeepRedundantCommits(o.Dir, commitSha)
if err != nil {
msg := fmt.Sprintf("commit %s is a merge but no -m option was given.", commitSha)
if !strings.Contains(err.Error(), msg) {
return errors.Wrapf(err, "cherry-picking %s", commitSha)
}
} else {
log.Logger().Infof("%s - %s", commitSha, commitMsg)
}
}
return nil
}
func (o *UpgradeBootOptions) setupGitConfig(dir string) error {
jxClient, devNs, err := o.JXClientAndDevNamespace()
if err != nil {
return errors.Wrap(err, "failed to create/get jx client and dev namespace")
}
devEnv, err := kube.GetDevEnvironment(jxClient, devNs)
if err != nil {
return errors.Wrapf(err, "failed to get dev environment in namespace %s", devNs)
}
username := devEnv.Spec.TeamSettings.PipelineUsername
email := devEnv.Spec.TeamSettings.PipelineUserEmail
err = o.Git().SetUsername(dir, username)
if err != nil {
return errors.Wrapf(err, "failed to set username %s", username)
}
err = o.Git().SetEmail(dir, email)
if err != nil {
return errors.Wrapf(err, "failed to set email for %s", email)
}
return nil
}
func (o *UpgradeBootOptions) raisePR() error {
gitInfo, provider, _, err := o.CreateGitProvider(o.Dir)
if err != nil {
return errors.Wrap(err, "failed to get git provider")
}
upstreamInfo, err := provider.GetRepository(gitInfo.Organisation, gitInfo.Name)
if err != nil {
return errors.Wrapf(err, "getting repository %s/%s", gitInfo.Organisation, gitInfo.Name)
}
details, filter, err := o.prDetailsAndFilter()
if err != nil {
return errors.Wrapf(err, "failed to get PR details and filter")
}
_, err = gits.PushRepoAndCreatePullRequest(o.Dir, upstreamInfo, nil, "master", &details, &filter, false, details.Title, true, false, o.Git(), provider)
if err != nil {
return errors.Wrapf(err, "failed to create PR for base %s and head branch %s", "master", details.BranchName)
}
return nil
}
func (o *UpgradeBootOptions) prDetailsAndFilter() (gits.PullRequestDetails, gits.PullRequestFilter, error) {
details := gits.PullRequestDetails{
BranchName: fmt.Sprintf("jx_boot_upgrade"),
Title: "feat(config): upgrade configuration",
Message: "Upgrade configuration",
}
labels := []string{}
if len(o.Labels) > 0 {
labels = append(labels, o.Labels...)
}
filter := gits.PullRequestFilter{
Labels: []string{
boot.PullRequestLabel,
},
}
details.Labels = labels
return details, filter, nil
}
func (o *UpgradeBootOptions) deleteLocalBranch(branch string) error {
err := o.Git().Checkout(o.Dir, "master")
if err != nil {
return errors.Wrapf(err, "failed to checkout master branch")
}
err = o.Git().DeleteLocalBranch(o.Dir, branch)
if err != nil {
return errors.Wrapf(err, "failed to delete local branch %s", branch)
}
return nil
}
func (o *UpgradeBootOptions) cloneDevEnv() error {
jxClient, devNs, err := o.JXClientAndDevNamespace()
if err != nil {
return errors.Wrap(err, "failed to create/get jx client and dev namespace")
}
devEnv, err := kube.GetDevEnvironment(jxClient, devNs)
if err != nil {
return errors.Wrapf(err, "failed to get dev environment in namespace %s", devNs)
}
if devEnv == nil {
return errors.Wrapf(errors.New("Cannot find environment with dev selector"), "No development environment found for namespace %s", devNs)
}
devEnvURL := devEnv.Spec.Source.URL
cloneDir, err := ioutil.TempDir("", "")
if err != nil {
return errors.Wrapf(err, "failed to create tmp dir to clone dev env repo")
}
err = os.MkdirAll(cloneDir, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed to give write perms to tmp dir to clone dev env repo")
}
gitInfo, err := gits.ParseGitURL(devEnvURL)
if err != nil {
return errors.Wrapf(err, "failed to parse url %s", devEnvURL)
}
_, userAuth, err := o.GetPipelineGitAuthForRepo(gitInfo)
if err != nil {
return errors.Wrap(err, "failed to get pipeline user auth")
}
cloneURL, err := o.Git().CreateAuthenticatedURL(devEnvURL, userAuth)
if err != nil {
return errors.Wrapf(err, "failed to create directory for dev env clone: %s", cloneDir)
}
err = o.Git().Clone(cloneURL, cloneDir)
if err != nil {
return errors.Wrapf(err, "failed to clone git URL %s to directory %s", devEnvURL, cloneDir)
}
o.Dir = cloneDir
return nil
}
func (o *UpgradeBootOptions) updatePipelineBuilderImage(resolver *versionstream.VersionResolver) error {
piplineFileGlob := "jenkins-x*.yml"
updatedBuilderImage, err := resolver.ResolveDockerImage(builderImage)
if err != nil {
return errors.Wrapf(err, "failed to resolve image %s", builderImage)
}
log.Logger().Infof("Updating pipeline agent images to %s", util.ColorInfo(updatedBuilderImage))
fn, err := operations.CreatePullRequestRegexFn(updatedBuilderImage, `(?m)^\s*agent:\n\s*image: (gcr.io\/jenkinsxio\/builder-go.*$)`, piplineFileGlob)
if err != nil {
return errors.WithStack(err)
}
_, err = fn(o.Dir, nil)
if err != nil {
return errors.WithStack(err)
}
matches, err := filepath.Glob(filepath.Join(o.Dir, piplineFileGlob))
if err != nil {
return errors.Wrapf(err, "applying glob %s", piplineFileGlob)
}
for i, match := range matches {
matches[i], err = filepath.Rel(o.Dir, match)
if err != nil {
return errors.Wrapf(err, "failed to build path for pipeline file %s", match)
}
}
err = o.Git().AddCommitFiles(o.Dir, "feat: upgrade pipeline builder images", matches)
if err != nil {
log.Logger().Info("Skipping builder image update as no changes were detected")
}
return nil
}
func (o *UpgradeBootOptions) updateTemplateBuilderImage(resolver *versionstream.VersionResolver) error {
templateFile := fmt.Sprintf("env/%s", helm.ValuesTemplateFileName)
updatedBuilderImage, err := resolver.ResolveDockerImage(builderImage)
if err != nil {
return errors.Wrapf(err, "failed to resolve image %s", builderImage)
}
log.Logger().Infof("Updating template builder images to %s", util.ColorInfo(updatedBuilderImage))
fn, err := operations.CreatePullRequestRegexFn(updatedBuilderImage, `(?m)^\s*builderImage: (gcr.io\/jenkinsxio\/builder-go.*$)`, templateFile)
if err != nil {
return errors.WithStack(err)
}
_, err = fn(o.Dir, nil)
if err != nil {
return errors.WithStack(err)
}
err = o.Git().AddCommitFiles(o.Dir, "feat: upgrade template builder images", []string{templateFile})
if err != nil {
log.Logger().Info("Skipping template builder image update as no changes were detected")
}
return nil
}