Skip to content

Commit 447e8e2

Browse files
authored
refactor dagger (#55)
1 parent fc95c4b commit 447e8e2

9 files changed

Lines changed: 89 additions & 39 deletions

File tree

.github/workflows/release-dev.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414

1515
- name: Setup Dagger
1616
uses: dagger/dagger-for-github@v5
17+
with:
18+
verb: functions
1719

1820
- name: Setup Node.js
1921
uses: actions/setup-node@v4

.github/workflows/release-prod.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414

1515
- name: Setup Dagger
1616
uses: dagger/dagger-for-github@v5
17+
with:
18+
verb: functions
1719

1820
- name: Setup Node.js
1921
uses: actions/setup-node@v4

.github/workflows/release-stg.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414

1515
- name: Setup Dagger
1616
uses: dagger/dagger-for-github@v5
17+
with:
18+
verb: functions
1719

1820
- name: Setup Node.js
1921
uses: actions/setup-node@v4

.github/workflows/validate-pr.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414

1515
- name: Setup Dagger
1616
uses: dagger/dagger-for-github@v5
17+
with:
18+
verb: functions
1719

1820
- name: Setup Node.js
1921
uses: actions/setup-node@v4

dagger/con.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
type Con struct {
9+
*Container
10+
}
11+
12+
func (c *Con) SetEnvs(ctx context.Context, m *NdthanhdevGithubIo) *Con {
13+
c.Container = c.Container.
14+
WithEnvVariable("MODE", m.Mode)
15+
16+
if m.GhToken != nil {
17+
tokenString, _ := m.GhToken.Plaintext(ctx)
18+
19+
c.Container = c.
20+
WithEnvVariable("GH_TOKEN", tokenString)
21+
}
22+
23+
return c
24+
}
25+
26+
func (c *Con) MoonRun(command string) *Con {
27+
c.Container = c.
28+
Container.
29+
WithExec([]string{fmt.Sprintf(`moon run %s`, command)})
30+
31+
return c
32+
}

dagger/main.go

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,36 @@ package main
1616

1717
import (
1818
"context"
19-
"fmt"
2019
)
2120

2221
type NdthanhdevGithubIo struct {
22+
Dir *Directory
23+
Mode string
24+
GhToken *Secret
2325
}
2426

25-
// Returns a container that echoes whatever string argument is provided
26-
func (m *NdthanhdevGithubIo) ContainerEcho(stringArg string) *Container {
27-
return dag.Container().From("alpine:latest").WithExec([]string{"echo", stringArg})
27+
func New(
28+
// +required
29+
dir *Directory,
30+
// +optional
31+
// +default="dev"
32+
mode string,
33+
// +optional
34+
ghToken *Secret,
35+
) *NdthanhdevGithubIo {
36+
return &NdthanhdevGithubIo{
37+
Dir: dir,
38+
Mode: mode,
39+
GhToken: ghToken,
40+
}
2841
}
2942

30-
type Con struct {
31-
*Container
32-
}
33-
34-
func (c *Con) MoonRun(command string) *Container {
35-
return c.Container.WithExec([]string{fmt.Sprintf(`moon run %s`, command)})
36-
}
37-
38-
func (m *NdthanhdevGithubIo) Init(ctx context.Context, dir *Directory) *Container {
39-
source := dag.Directory().WithDirectory("/", dir, DirectoryWithDirectoryOpts{
43+
func (m *NdthanhdevGithubIo) init(ctx context.Context) *Con {
44+
source := dag.Directory().WithDirectory("/", m.Dir, DirectoryWithDirectoryOpts{
4045
Exclude: []string{"node_modules", ".cache", "moon/.cache"},
4146
})
4247

43-
return dag.
48+
con := dag.
4449
Container().
4550
From("node:22-bookworm").
4651
WithExec([]string{"apt-get", "install", "-y", "bash", "curl", "git", "unzip", "gzip", "xz-utils"}).
@@ -50,7 +55,7 @@ func (m *NdthanhdevGithubIo) Init(ctx context.Context, dir *Directory) *Containe
5055
WithEnvVariable("HOME", "/root").
5156
WithUser("root").
5257
// curl -fsSL https://moonrepo.dev/install/proto.sh | bash -s 0.35.3 --yes
53-
WithExec([]string{`curl -fsSL https://moonrepo.dev/install/proto.sh | bash -s -- 0.35.3 --yes`}).
58+
WithExec([]string{`curl -fsSL https://moonrepo.dev/install/proto.sh | bash -s -- 0.35.5 --yes`}).
5459
// export PROTO_HOME="$HOME/.proto"
5560
WithEnvVariable("PROTO_HOME", "$HOME/.proto", ContainerWithEnvVariableOpts{
5661
Expand: true,
@@ -68,28 +73,30 @@ func (m *NdthanhdevGithubIo) Init(ctx context.Context, dir *Directory) *Containe
6873
// yarn install --immutable
6974
WithExec([]string{"yarn install --immutable"})
7075

76+
return (&Con{con}).SetEnvs(ctx, m)
7177
}
7278

73-
func (m *NdthanhdevGithubIo) Test(ctx context.Context, dir *Directory) *Container {
74-
return (&Con{m.Init(ctx, dir)}).
75-
MoonRun("scripts:test")
79+
func (m *NdthanhdevGithubIo) MoonRun(ctx context.Context, command string) *Container {
80+
return m.
81+
init(ctx).
82+
MoonRun(command).
83+
Container
7684
}
7785

78-
func (m *NdthanhdevGithubIo) Build(ctx context.Context, dir *Directory, mode string) *Directory {
79-
return (&Con{m.Init(ctx, dir).
80-
WithEnvVariable("MODE", mode)}).
81-
MoonRun("scripts:build").
82-
Directory("/mnt/app/public")
86+
func (m *NdthanhdevGithubIo) Test(ctx context.Context) (string, error) {
87+
return m.
88+
MoonRun(ctx, "scripts:test").
89+
Stdout(ctx)
8390
}
8491

85-
func (m *NdthanhdevGithubIo) Publish(ctx context.Context, dir *Directory, mode string, token *Secret) (string, error) {
86-
87-
tokenString, _ := token.Plaintext(ctx)
92+
func (m *NdthanhdevGithubIo) Build(ctx context.Context) *Directory {
93+
return m.
94+
MoonRun(ctx, "scripts:build").
95+
Directory("/mnt/app/public")
96+
}
8897

89-
return (&Con{m.
90-
Init(ctx, dir).
91-
WithEnvVariable("GH_TOKEN", tokenString).
92-
WithEnvVariable("MODE", mode)}).
93-
MoonRun("scripts:publish").
98+
func (m *NdthanhdevGithubIo) Publish(ctx context.Context) (string, error) {
99+
return m.
100+
MoonRun(ctx, "scripts:publish").
94101
Stdout(ctx)
95102
}

docs/guidelines.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# `/scripts`
1+
# Folder Structure
22

3+
- `dagger`: Contains `functions` that run an `action` under `scripts/actions/` inside `dagger` container.
34
- `scripts/`
45
- `utils/`: Contains utility modules for reuse in other scripts.
5-
- `actions/`: Contains the main scripts that perform small tasks should be able to run independently.
6-
- `pipelines/`: Contains the main scripts that run a series of `actions` inside `dagger`. They only require `dagger` && `tsx` to be installed.
6+
- `actions/`: Contains the main scripts that perform small tasks should be able to run independently. Requires `tsx` to be installed
7+
- `pipelines/`: Contains the main scripts that run a series of `dagger functions`. Requires `dagger` && `tsx` to be installed.

scripts/pipelines/release.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ await cleanBuild();
88

99
cd(workDirs.path);
1010

11-
await $`dagger call build \
11+
await $`dagger call \
1212
--dir . \
1313
--mode ${$.env.MODE ?? "dev"} \
14+
build \
1415
export --path ${workDirs.app.public.path} \
1516
`;
1617

17-
await $`dagger call publish \
18+
await $`dagger call \
1819
--dir . \
19-
--token env:GH_TOKEN \
2020
--mode ${$.env.MODE ?? "dev"} \
21+
--token env:GH_TOKEN \
22+
publish \
2123
`;

scripts/pipelines/validate-pr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ await cleanBuild();
88

99
cd(workDirs.path);
1010

11-
await $`dagger call test --dir .`;
11+
await $`dagger call --dir . test`;

0 commit comments

Comments
 (0)