forked from getporter/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
126 lines (103 loc) · 3.36 KB
/
magefile.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
//go:build mage
// This is a magefile, and is a "makefile for go".
// See https://magefile.org/
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"get.porter.sh/example-bundles/mage/examples"
"get.porter.sh/example-bundles/mage/setup"
"get.porter.sh/magefiles/git"
"get.porter.sh/magefiles/porter"
"github.com/carolynvs/magex/mgx"
// mage:import
_ "get.porter.sh/magefiles/ci"
"github.com/carolynvs/magex/shx"
"github.com/hashicorp/go-multierror"
"github.com/magefile/mage/mg"
)
func Build() {
mg.Deps(BuildExamples)
}
func Test() error {
return shx.RunV("go", "test", "./...")
}
// BuildExamples builds every example bundle
func BuildExamples() error {
var bigErr *multierror.Error
names, err := examples.List(".")
if err != nil {
return err
}
for _, bundleName := range names {
if err := BuildExample(bundleName); err != nil {
// Keep trying to build all the bundles, don't stop on the first one
bigErr = multierror.Append(bigErr, fmt.Errorf("error building bundle %s: %w", bundleName, err))
continue
}
}
return bigErr.ErrorOrNil()
}
// BuildExample builds the specified example bundle
func BuildExample(name string) error {
mg.SerialDeps(setup.InstallMixins)
fmt.Println("\n==========================")
fmt.Printf("Building example bundle: %s\n", name)
if customBuildFlags, err := os.ReadFile(filepath.Join(name, "build-args.txt")); err == nil {
customBuildArgs := strings.Split(string(customBuildFlags), " ")
buildArgs := append([]string{"build"}, customBuildArgs...)
return shx.Command("porter", buildArgs...).
CollapseArgs().In(name).RunV()
}
// Always build for amd64 even if on an arm host
// This is a bit of a hack until we have multi-arch support
return shx.Command("porter", "build").
In(name).Env("DOCKER_DEFAULT_PLATFORM=linux/amd64").RunV()
}
func Publish() {
mg.Deps(PublishExamples)
}
// PublishExamples publishes every example bundle
func PublishExamples() error {
var bigErr *multierror.Error
names, err := examples.List(".")
if err != nil {
return err
}
for _, bundleName := range names {
if err := PublishExample(bundleName); err != nil {
// Keep trying to publish all the bundles, don't stop on the first one
bigErr = multierror.Append(bigErr, fmt.Errorf("error publishing bundle %s: %w", bundleName, err))
continue
}
}
return bigErr.ErrorOrNil()
}
// PublishExample publishes the specified example bundle
func PublishExample(name string) error {
mg.SerialDeps(porter.UseBinForPorterHome, porter.EnsurePorter)
fmt.Println("\n==========================")
registryFlag := ""
registry := os.Getenv("PORTER_REGISTRY")
if registry != "" {
registryFlag = "--registry=" + registry
}
// Check if the bundle already is published
bundleRef, err := examples.GetBundleRef(name, registry)
mgx.Must(err)
// Do not overwrite an already published bundle
// See https://github.com/getporter/porter/issues/2017
if err := shx.RunS("porter", "explain", "-r", bundleRef); err == nil {
fmt.Printf("Skipping publish for example bundle: %s. The bundle is already published to %s.\n", name, bundleRef)
return nil
}
fmt.Printf("Publishing example bundle: %s\n", name)
return shx.Command("porter", "publish", registryFlag).CollapseArgs().In(name).RunV()
}
// SetupDCO configures your git repository to automatically sign your commits
// to comply with our DCO
func SetupDCO() error {
return git.SetupDCO()
}