Skip to content

Commit a92cebd

Browse files
authored
Merge pull request #30 from aweris/aweris/improve-dagger-module
build: improve dagger module
2 parents 559ba46 + 3bc23ba commit a92cebd

File tree

5 files changed

+87
-22
lines changed

5 files changed

+87
-22
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ jobs:
1313
with:
1414
verb: call
1515
module: ci
16-
args: run --src "."
16+
args: validate

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.devbox
2+
.idea

ci/.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dagger.gen.go linguist-generated
2+
/querybuilder/** linguist-generated

ci/dagger.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"name": "ci",
3-
"sdk": "go"
3+
"sdk": "go",
4+
"root": ".."
45
}

ci/main.go

+81-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,92 @@
11
package main
22

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+
)
137

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+
}
1712
}
1813

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
2217
}
2318

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"}).
2624
WithExec([]string{"git", "diff", "--exit-code", ":!out/"})
2725
}
2826

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+
)
3192
}

0 commit comments

Comments
 (0)