-
Notifications
You must be signed in to change notification settings - Fork 242
/
app.go
178 lines (157 loc) · 4.2 KB
/
app.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
package image
import (
"dagger.io/dagger"
)
const (
// https://github.com/orgs/thechangelog/packages/container/package/changelog-legacy-assets
LegacyAssetsImageRef = "ghcr.io/thechangelog/changelog-legacy-assets@sha256:4f1d2aa7036836bd59ff3af74bfd054c33a1ed558514a8dd91062a14804d7153"
)
func (image *Image) WithAppSrc() *Image {
appSrc := image.dag.Host().Directory(".", dagger.HostDirectoryOpts{
Include: []string{
"config",
"lib",
"priv/repo",
"test",
"env.op",
"mix.exs",
"mix.lock",
},
})
image.container = image.container.
WithDirectory("/app", appSrc).
WithWorkdir("/app")
return image
}
func (image *Image) WithAppDeps() *Image {
image.container = image.container.
WithMountedCache(
"/app/deps", image.dag.CacheVolume("app-deps"),
).
WithExec([]string{
"echo", "Fetch app deps...",
}).
WithExec([]string{
"sh", "-c", "mix deps.get --only $MIX_ENV",
}).
WithExec([]string{
"sh", "-c", "echo \"Ensure app deps are present & OK...\"",
}).
WithExec([]string{
"sh", "-c", "ls -lahd /app/deps/*",
})
return image
}
func (image *Image) WithAppCompiled() *Image {
image.container = image.container.
WithMountedCache(
"/app/_build", image.dag.CacheVolume("app-build"),
).
WithExec([]string{
"sh", "-c", "echo \"Compile app $MIX_ENV deps...\"",
}).
WithExec([]string{
"mix", "deps.compile",
}).
WithExec([]string{
"sh", "-c", "echo \"Compile app for $MIX_ENV...\"",
}).
WithExec([]string{
"mix", "compile",
}).
WithExec([]string{
"sh", "-c", "echo \"Ensure $MIX_ENV bytecode is present & OK...\"",
}).
WithExec([]string{
"sh", "-c", "ls -lahd /app/_build/$MIX_ENV/lib/*/ebin",
})
return image
}
func (image *Image) WithAppStaticAssets() *Image {
appAssets := image.dag.Host().Directory("./assets", dagger.HostDirectoryOpts{
Exclude: []string{
"node_modules",
},
})
image.container = image.container.
WithDirectory("/app/assets", appAssets).
WithWorkdir("/app/assets").
WithMountedCache(
"/app/assets/node_modules", image.dag.CacheVolume("app-node-modules"),
).
WithExec([]string{
"yarn", "install", "--frozen-lockfile",
}).
WithExec([]string{
"yarn", "run", "compile",
}).
WithWorkdir("/app").
WithExec([]string{
"mix", "phx.digest", "--no-vsn",
}).
WithExec([]string{
"echo", "Ensure static assets are present & OK...",
}).
WithExec([]string{
"ls", "-lah", "/app/priv/static/cache_manifest.json",
})
return image
}
// TODO: upload legacy assets to R2 + /wp-content/.* redirect
func (image *Image) WithAppLegacyAssets() *Image {
legacyAssets := image.NewContainer().
From(LegacyAssetsImageRef).
Directory("/var/www/wp-content")
image.container = image.container.
WithDirectory("/app/priv/static/wp-content", legacyAssets).
WithExec([]string{
"echo", "Ensure legacy assets are present & OK...",
}).
WithExec([]string{
"ls", "-lah", "/app/priv/static/wp-content/uploads",
})
return image
}
func (image *Image) WithOnDeploy() *Image {
image.container = image.container.
WithNewFile("/usr/local/bin/on.deploy", dagger.ContainerWithNewFileOpts{
Contents: `#!/bin/bash
OP_SERVICE_ACCOUNT_TOKEN="${OP_SERVICE_ACCOUNT_TOKEN:?must be set}"
op run --env-file="./env.op" --no-masking -- mix changelog.static.upload
op run --env-file="./env.op" --no-masking -- mix ecto.migrate`,
Permissions: 555,
})
return image
}
func (image *Image) WithAppStart() *Image {
image.container = image.container.
WithNewFile("/usr/local/bin/app.start", dagger.ContainerWithNewFileOpts{
Contents: `#!/bin/bash
OP_SERVICE_ACCOUNT_TOKEN="${OP_SERVICE_ACCOUNT_TOKEN:?must be set}"
op run --env-file="./env.op" --no-masking -- mix phx.server`,
Permissions: 555,
})
return image
}
func (image *Image) WithAppRelease() *Image {
image.container = image.container.
WithExec([]string{
"echo", "Generate release files...",
}).
WithExec([]string{
"mix", "phx.gen.release",
}).
WithExec([]string{
"echo", "Create a self-contained release...",
}).
WithExec([]string{
"mix", "release", "--path", "/app.release",
}).
WithExec([]string{
"echo", "Check release version...",
}).
WithExec([]string{
"/app.release/bin/changelog", "version",
})
return image
}