Skip to content

Commit f1204b3

Browse files
committed
Add Dagger module and workflow
Signed-off-by: Vikram Vaswani <vikram@dagger.io>
1 parent 2c6c49b commit f1204b3

File tree

8 files changed

+777
-0
lines changed

8 files changed

+777
-0
lines changed

.dagger/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/sdk/** linguist-generated

.dagger/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.venv
2+
/**/__pycache__
3+
/sdk
4+
/.env

.dagger/pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[project]
2+
name = "book"
3+
version = "0.1.0"
4+
requires-python = ">=3.12"
5+
dependencies = ["dagger-io"]
6+
7+
[build-system]
8+
requires = ["hatchling==1.25.0"]
9+
build-backend = "hatchling.build"
10+
11+
[tool.uv.sources]
12+
dagger-io = { path = "sdk", editable = true }

.dagger/src/book/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""A generated module for Book functions
2+
3+
This module has been generated via dagger init and serves as a reference to
4+
basic module structure as you get started with Dagger.
5+
6+
Two functions have been pre-created. You can modify, delete, or add to them,
7+
as needed. They demonstrate usage of arguments and return types using simple
8+
echo and grep commands. The functions can be called from the dagger CLI or
9+
from one of the SDKs.
10+
11+
The first line in this comment block is a short description line and the
12+
rest is a long description with more detail on the module's purpose or usage,
13+
if appropriate. All modules should have a short description.
14+
"""
15+
16+
from .main import Book as Book

.dagger/src/book/main.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import dagger
2+
from typing import Annotated
3+
from dagger import dag, function, object_type, DefaultPath
4+
5+
6+
@object_type
7+
class Book:
8+
9+
source: Annotated[dagger.Directory, DefaultPath(".")]
10+
11+
@function
12+
def env(self) -> dagger.Container:
13+
"""Returns a Python container with the current source directory mounted and requirements installed"""
14+
return (
15+
dag.container()
16+
.from_("python:3.11")
17+
.with_mounted_directory("/app", self.source)
18+
.with_workdir("/app")
19+
.with_mounted_cache("/root/.cache/pip", dag.cache_volume("python-pip"))
20+
.with_exec(["pip", "install", "-r", "requirements.txt"])
21+
)
22+
23+
@function
24+
async def test(self) -> str:
25+
"""Returns the result of running unit tests using pytest"""
26+
27+
postgresdb = (
28+
dag.container()
29+
.from_("postgres:alpine")
30+
.with_env_variable("POSTGRES_USER", "app_user")
31+
.with_env_variable("POSTGRES_DB", "app_test")
32+
.with_env_variable("POSTGRES_PASSWORD", "secret")
33+
.with_exposed_port(5432)
34+
.as_service(args=[], use_entrypoint=True)
35+
)
36+
37+
return await (
38+
self.env()
39+
.with_service_binding("db", postgresdb)
40+
.with_env_variable("DATABASE_URL", "postgresql://app_user:secret@db/app_test")
41+
.with_exec(["pytest"])
42+
.stdout()
43+
)
44+
45+
@function
46+
async def publish(self) -> str:
47+
"""Returns the container address after publishing to ttl.sh"""
48+
#await self.test()
49+
ctr = self.env()
50+
return await (
51+
ctr
52+
.with_exposed_port(8000)
53+
.with_entrypoint(["fastapi", "run", "main.py"])
54+
#.with_registry_auth("docker.io", "my-username", "my-password")
55+
#.with_registry_auth("ghcr.io", "my-username", "my-password")
56+
.publish("ttl.sh/fastapi-app-1234")
57+
)
58+
59+
60+
@function
61+
def container_echo(self, string_arg: str) -> dagger.Container:
62+
"""Returns a container that echoes whatever string argument is provided"""
63+
return dag.container().from_("alpine:latest").with_exec(["echo", string_arg])
64+
65+
@function
66+
async def grep_dir(self, directory_arg: dagger.Directory, pattern: str) -> str:
67+
"""Returns lines that match a pattern in the files of the provided Directory"""
68+
return await (
69+
dag.container()
70+
.from_("alpine:latest")
71+
.with_mounted_directory("/mnt", directory_arg)
72+
.with_workdir("/mnt")
73+
.with_exec(["grep", "-R", pattern, "."])
74+
.stdout()
75+
)

.dagger/uv.lock

Lines changed: 637 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/workflows/dagger.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: dagger
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
9+
10+
jobs:
11+
dagger:
12+
name: dagger
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
- name: Test
18+
id: test
19+
uses: dagger/dagger-for-github@8.0.0
20+
with:
21+
version: "0.18.14"
22+
verb: call
23+
args: test
24+
cloud-token: ${{ secrets.DAGGER_CLOUD_TOKEN }}

dagger.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "book",
3+
"engineVersion": "v0.18.14",
4+
"sdk": {
5+
"source": "python"
6+
},
7+
"source": ".dagger"
8+
}

0 commit comments

Comments
 (0)