-
Notifications
You must be signed in to change notification settings - Fork 559
/
cli.go
175 lines (150 loc) · 4.49 KB
/
cli.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
package main
import (
"context"
"fmt"
"golang.org/x/sync/errgroup"
"github.com/dagger/dagger/ci/build"
"github.com/dagger/dagger/ci/internal/dagger"
)
type CLI struct {
Dagger *Dagger // +private
}
// Build the CLI binary
func (cli *CLI) File(
ctx context.Context,
// +optional
platform dagger.Platform,
) (*File, error) {
builder, err := build.NewBuilder(ctx, cli.Dagger.Source)
if err != nil {
return nil, err
}
builder = builder.WithVersion(cli.Dagger.Version.String())
if platform != "" {
builder = builder.WithPlatform(platform)
}
return builder.CLI(ctx)
}
const (
// https://github.com/goreleaser/goreleaser/releases
goReleaserVersion = "v1.26.0"
)
// Publish the CLI using GoReleaser
func (cli *CLI) Publish(
ctx context.Context,
gitDir *Directory,
githubOrgName string,
githubToken *Secret,
goreleaserKey *Secret,
awsAccessKeyID *Secret,
awsSecretAccessKey *Secret,
awsRegion *Secret,
awsBucket *Secret,
artefactsFQDN *Secret,
) error {
args := []string{"release", "--clean", "--skip-validate", "--debug"}
if cli.Dagger.Version.Tag != "" {
args = append(args, "--release-notes", fmt.Sprintf(".changes/%s.md", cli.Dagger.Version.Tag))
} else {
// if this isn't an official semver version, do a dev release
args = append(args,
"--nightly",
"--config", ".goreleaser.nightly.yml",
)
}
ctr, err := publishEnv(ctx)
if err != nil {
return err
}
_, err = ctr.
WithWorkdir("/app").
WithMountedDirectory("/app", cli.Dagger.Source).
WithDirectory("/app/.git", gitDir).
WithEnvVariable("GH_ORG_NAME", githubOrgName).
WithSecretVariable("GITHUB_TOKEN", githubToken).
WithSecretVariable("GORELEASER_KEY", goreleaserKey).
WithSecretVariable("AWS_ACCESS_KEY_ID", awsAccessKeyID).
WithSecretVariable("AWS_SECRET_ACCESS_KEY", awsSecretAccessKey).
WithSecretVariable("AWS_REGION", awsRegion).
WithSecretVariable("AWS_BUCKET", awsBucket).
WithSecretVariable("ARTEFACTS_FQDN", artefactsFQDN).
WithEnvVariable("ENGINE_VERSION", cli.Dagger.Version.String()).
With(func(ctr *dagger.Container) *dagger.Container {
if cli.Dagger.Version.Tag == "" {
// goreleaser refuses to run if there isn't a tag, so set it to a dummy but valid semver
return ctr.WithExec([]string{"git", "tag", "0.0.0"})
}
return ctr
}).
WithEntrypoint([]string{"/sbin/tini", "--", "/entrypoint.sh"}).
WithExec(args).
Sync(ctx)
return err
}
// Verify that the CLI builds without actually publishing anything
func (cli *CLI) TestPublish(ctx context.Context) error {
// TODO: ideally this would also use go releaser, but we want to run this
// step in PRs and locally and we use goreleaser pro features that require
// a key which is private. For now, this just builds the CLI for the same
// targets so there's at least some coverage
oses := []string{"linux", "windows", "darwin"}
arches := []string{"amd64", "arm64", "arm"}
builder, err := build.NewBuilder(ctx, cli.Dagger.Source)
if err != nil {
return err
}
builder = builder.WithVersion(cli.Dagger.Version.String())
var eg errgroup.Group
for _, os := range oses {
for _, arch := range arches {
if arch == "arm" && os == "darwin" {
continue
}
platform := os + "/" + arch
if arch == "arm" {
platform += "/v7" // not always correct but not sure of better way
}
eg.Go(func() error {
f, err := builder.
WithPlatform(Platform(platform)).
CLI(ctx)
if err != nil {
return err
}
_, err = f.Sync(ctx)
return err
})
}
}
eg.Go(func() error {
env, err := publishEnv(ctx)
if err != nil {
return err
}
_, err = env.Sync(ctx)
return err
})
return eg.Wait()
}
func publishEnv(ctx context.Context) (*dagger.Container, error) {
ctr := dag.Container().
From(fmt.Sprintf("ghcr.io/goreleaser/goreleaser-pro:%s-pro", goReleaserVersion)).
WithEntrypoint([]string{}).
WithExec([]string{"apk", "add", "aws-cli"})
// install nix
ctr = ctr.
WithExec([]string{"apk", "add", "xz"}).
WithDirectory("/nix", dag.Directory()).
WithNewFile("/etc/nix/nix.conf", dagger.ContainerWithNewFileOpts{
Contents: `build-users-group =`,
}).
WithExec([]string{"sh", "-c", "curl -L https://nixos.org/nix/install | sh -s -- --no-daemon"})
path, err := ctr.EnvVariable(ctx, "PATH")
if err != nil {
return nil, err
}
ctr = ctr.WithEnvVariable("PATH", path+":/nix/var/nix/profiles/default/bin")
// goreleaser requires nix-prefetch-url, so check we can run it
ctr = ctr.WithExec([]string{"sh", "-c", "nix-prefetch-url 2>&1 | grep 'error: you must specify a URL'"})
return ctr, nil
}