forked from opcr-io/policy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
130 lines (106 loc) · 2.81 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
127
128
129
130
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/aserto-dev/mage-loot/common"
"github.com/aserto-dev/mage-loot/deps"
"github.com/magefile/mage/mg"
"github.com/pkg/errors"
)
func init() {
// Set go version for docker builds
os.Setenv("GO_VERSION", "1.17")
// Enable docker buildkit capabilities
os.Setenv("DOCKER_BUILDKIT", "1")
}
// Generate generates all code.
func Generate() error {
return common.GenerateWith([]string{
filepath.Dir(deps.GoBinPath("mockgen")),
filepath.Dir(deps.GoBinPath("wire")),
})
}
// Build builds all binaries in ./cmd.
func Build() error {
return common.BuildReleaser()
}
// Release releases the project.
func Release() error {
return common.Release()
}
// BuildAll builds all binaries in ./cmd for
// all configured operating systems and architectures.
func BuildAll() error {
return common.BuildAllReleaser()
}
// Lint runs linting for the entire project.
func Lint() error {
return common.Lint()
}
// Test runs all tests and generates a code coverage report.
func Test() error {
return common.Test()
}
// DockerImage builds the docker image for the project.
func DockerImage() error {
version, err := common.Version()
if err != nil {
return errors.Wrap(err, "failed to calculate version")
}
return common.DockerImage(fmt.Sprintf("policy:%s", version))
}
// DockerPush builds the docker image using all tags specified by sver
// and pushes it to the specified registry
func DockerPush(registry, org string) error {
tags, err := common.DockerTags(registry, fmt.Sprintf("%s/policy", org))
if err != nil {
return err
}
version, err := common.Version()
if err != nil {
return errors.Wrap(err, "failed to calculate version")
}
for _, tag := range tags {
common.UI.Normal().WithStringValue("tag", tag).Msg("pushing tag")
err = common.DockerPush(fmt.Sprintf("policy:%s", version), fmt.Sprintf("%s/%s/policy:%s", registry, org, tag))
if err != nil {
return err
}
}
return nil
}
// Tag creates a new patch version tag and pushes it if it's not dirty.
func Tag() error {
newTag, err := common.NextVersion("patch")
if err != nil {
return err
}
if common.IsDirty(newTag) {
return errors.New("your tree is in a dirty state")
}
newTag = "v" + newTag
err = common.GitTag(newTag)
if err != nil {
return err
}
err = common.GitPushTag(newTag, "origin")
if err != nil {
return err
}
common.UI.Normal().WithStringValue("new-tag", newTag).Msg("created and pushed new tag")
return nil
}
// Deps installs all dependencies required to build the project.
func Deps() {
deps.GetAllDeps()
}
// All runs all targets in the appropriate order.
// The targets are run in the following order:
// deps, generate, lint, test, build, dockerImage
func All() error {
mg.SerialDeps(Deps, Generate, Lint, Test, Build, DockerImage)
return nil
}