-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
96 lines (80 loc) · 2.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
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"time"
"github.com/sirupsen/logrus"
"github.com/uw-labs/go-mono/cmd/deploy/internal/binary"
"github.com/uw-labs/go-mono/cmd/deploy/internal/deploy"
"github.com/uw-labs/go-mono/cmd/deploy/internal/docker"
"github.com/uw-labs/go-mono/cmd/deploy/internal/git"
pkgcontext "github.com/uw-labs/go-mono/pkg/context"
)
var (
repoRoot = flag.String("repo-root", ".", "The root of the repo, to find the git folder.")
dockerUser = flag.String("docker-user", "", "The docker user to use when authenticating against the registry.")
dockerPassword = flag.String("docker-password", "", "The password to use when authenticating the user against the registry.")
dockerRegistry = flag.String("docker-registry", "docker.pkg.github.com/uw-labs/go-mono", "The registry to push images to. Can include any subpaths.")
deployFile = flag.String("deploy-file", "", "The deploy file to read deployment configuration from.")
)
func main() {
flag.Parse()
logger := logrus.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.StampMilli,
}
if *deployFile == "" {
logger.Fatal("deploy-file must be specified")
}
err := run(logger, *repoRoot, *dockerUser, *dockerPassword, *dockerRegistry, *deployFile)
if err != nil {
logger.WithError(err).Fatal()
}
}
func run(logger *logrus.Logger, repoRoot, dockerUser, dockerPassword, dockerRegistry, deployFile string) error {
ctx := pkgcontext.WithSignalHandler(context.Background())
md, err := git.GetMetadata(repoRoot)
if err != nil {
return fmt.Errorf("get git metadata: %w", err)
}
conf, err := deploy.Parse(repoRoot, deployFile)
if err != nil {
return fmt.Errorf("parse deployment: %w", err)
}
logger.Infoln("Deploying", conf.Name)
logger.Infoln("Building binary")
binPath, err := binary.Build(ctx, logger, &binary.Request{
Name: conf.Name,
RepoRoot: repoRoot,
MainPath: conf.Main,
})
if err != nil {
return fmt.Errorf("build binary: %w", err)
}
defer func() {
err := os.RemoveAll(filepath.Dir(binPath))
if err != nil {
logger.WithError(err).Infof("remove binary directory (%s)", filepath.Dir(binPath))
}
}()
logger.Infoln("Building Docker image")
digest, err := docker.BuildAndPushImage(ctx, logger, &docker.Request{
RepoRoot: repoRoot,
BinaryPath: binPath,
Registry: dockerRegistry,
RegistryUser: dockerUser,
RegistryPassword: dockerPassword,
GitSHA: md.GitSHA,
Name: conf.Name,
Tag: md.GitBranch,
})
if err != nil {
return fmt.Errorf("build Docker image: %w", err)
}
logger.Infof("Published %s", digest)
return nil
}