|
1 | 1 | package main
|
2 | 2 |
|
3 |
| -type CI struct{} |
4 |
| - |
5 |
| -func (m *CI) devbox(src *Directory) *Container { |
6 |
| - // TODO(kakkoyun): Use local container. |
7 |
| - return dag.Container(). |
8 |
| - From("jetpackio/devbox-root-user:latest"). |
9 |
| - WithMountedDirectory("/code", src). |
10 |
| - WithWorkdir("/code"). |
11 |
| - WithExec([]string{"devbox", "run", "--", "echo", "'Installed Packages.'"}) |
12 |
| -} |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "runtime" |
| 6 | +) |
13 | 7 |
|
14 |
| -func (m *CI) build(src *Directory) *Container { |
15 |
| - return m.devbox(src). |
16 |
| - WithExec([]string{"devbox", "run", "build"}) |
| 8 | +func New() *CI { |
| 9 | + return &CI{ |
| 10 | + Base: base().WithDirectory("/src", source()).WithWorkdir("/src"), |
| 11 | + } |
17 | 12 | }
|
18 | 13 |
|
19 |
| -func (m *CI) generate(src *Directory) *Container { |
20 |
| - return m.build(src). |
21 |
| - WithExec([]string{"devbox", "run", "generate"}) |
| 14 | +type CI struct { |
| 15 | + // Base is the base container for the CI contains required dependencies, nix, devbox and the source code. |
| 16 | + Base *Container |
22 | 17 | }
|
23 | 18 |
|
24 |
| -func (m *CI) validate(src *Directory) *Container { |
25 |
| - return m.generate(src). |
| 19 | +// Validate runs the build, format, and generate commands and checks if there are any changes in the source code except the out directory. |
| 20 | +func (m *CI) Validate() *Container { |
| 21 | + return m.Base. |
| 22 | + WithFocus(). |
| 23 | + WithExec([]string{"devbox", "run", "build", "format", "generate"}). |
26 | 24 | WithExec([]string{"git", "diff", "--exit-code", ":!out/"})
|
27 | 25 | }
|
28 | 26 |
|
29 |
| -func (m *CI) Run(src *Directory) *Container { |
30 |
| - return m.validate(src) |
| 27 | +// base returns a ubuntu container with nix and devbox installed. |
| 28 | +func base() *Container { |
| 29 | + // base container |
| 30 | + |
| 31 | + base := dag.Container().From("ubuntu:latest") |
| 32 | + |
| 33 | + // install base dependencies |
| 34 | + |
| 35 | + base = base.WithExec([]string{"apt-get", "update"}) |
| 36 | + base = base.WithExec([]string{"apt-get", "install", "-y", "curl", "git"}) |
| 37 | + |
| 38 | + // Mount the cache volume to the container |
| 39 | + |
| 40 | + base = base.WithMountedCache("/nix", dag.CacheVolume("parca-dev-testdata-nix")) |
| 41 | + base = base.WithMountedCache("/etc/nix", dag.CacheVolume("parca-dev-testdata-nix-etc")) |
| 42 | + |
| 43 | + // install nix |
| 44 | + |
| 45 | + base = base.WithExec([]string{"sh", "-c", "[ -f /etc/nix/group ] && cp /etc/nix/group /etc/group; exit 0"}) |
| 46 | + base = base.WithExec([]string{"sh", "-c", "[ -f /etc/nix/passwd ] && cp /etc/nix/passwd /etc/passwd; exit 0"}) |
| 47 | + base = base.WithExec([]string{"sh", "-c", "[ -f /etc/nix/shadow ] && cp /etc/nix/shadow /etc/shadow; exit 0"}) |
| 48 | + base = base.WithExec([]string{"sh", "-c", "[ ! -f /nix/receipt.json ] && curl --proto =https --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux --extra-conf \"sandbox = false\" --init none --no-confirm; exit 0"}) |
| 49 | + base = base.WithExec([]string{"cp", "/etc/group", "/etc/nix/group"}) |
| 50 | + base = base.WithExec([]string{"cp", "/etc/passwd", "/etc/nix/passwd"}) |
| 51 | + base = base.WithExec([]string{"cp", "/etc/shadow", "/etc/nix/shadow"}) |
| 52 | + base = base.WithExec([]string{"sed", "-i", "s/auto-allocate-uids = true/auto-allocate-uids = false/g", "/etc/nix/nix.conf"}) |
| 53 | + base = base.WithEnvVariable("PATH", "$PATH:/nix/var/nix/profiles/default/bin", ContainerWithEnvVariableOpts{Expand: true}) |
| 54 | + |
| 55 | + // install devbox |
| 56 | + |
| 57 | + base = base.WithExec([]string{"adduser", "--disabled-password", "devbox"}) |
| 58 | + base = base.WithExec([]string{"addgroup", "devbox", "nixbld"}) |
| 59 | + base = base.WithEnvVariable("FORCE", "1") |
| 60 | + base = base.WithExec([]string{"sh", "-c", "curl -fsSL https://get.jetpack.io/devbox | bash"}) |
| 61 | + base = base.WithExec([]string{"sh", "-c", `echo 'eval "$(devbox global shellenv --recompute)"' >> ~/.bashrc`}) |
| 62 | + base = base.WithExec([]string{"devbox", "version"}) |
| 63 | + |
| 64 | + return base |
| 65 | +} |
| 66 | + |
| 67 | +// root returns the root directory of the project as an absolute path. |
| 68 | +func root() string { |
| 69 | + // get location of current file |
| 70 | + _, current, _, _ := runtime.Caller(0) |
| 71 | + |
| 72 | + return filepath.Join(filepath.Dir(current), "..") |
| 73 | +} |
| 74 | + |
| 75 | +// source returns a directory containing the source code of the project with the required files and directories. |
| 76 | +func source() *Directory { |
| 77 | + return dag.Host().Directory( |
| 78 | + root(), |
| 79 | + HostDirectoryOpts{ |
| 80 | + Include: []string{ |
| 81 | + ".git", |
| 82 | + "jitdump/**", |
| 83 | + "src/**", |
| 84 | + "tables/**", |
| 85 | + "vendored/**", |
| 86 | + "devbox.*", |
| 87 | + "go.*", |
| 88 | + "build.go", |
| 89 | + }, |
| 90 | + }, |
| 91 | + ) |
31 | 92 | }
|
0 commit comments