Skip to content

Commit ff3e4a4

Browse files
author
Vikram Vaswani
committed
Added Dagger
Signed-off-by: Vikram Vaswani <vikram@dagger.io>
1 parent a16b8ca commit ff3e4a4

File tree

7 files changed

+120
-0
lines changed

7 files changed

+120
-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
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/.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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import random
2+
from typing import Annotated
3+
from datetime import datetime
4+
5+
import dagger
6+
from dagger import Container, dag, Directory, DefaultPath, Doc, File, Secret, function, object_type, ReturnType
7+
8+
9+
@object_type
10+
class Book:
11+
12+
source: Annotated[dagger.Directory, DefaultPath(".")]
13+
14+
@function
15+
def env(self) -> dagger.Container:
16+
"""Returns a container with the Python environment and the source code mounted"""
17+
return (
18+
dag.container()
19+
.from_("python:3.11")
20+
.with_directory("/app", self.source)
21+
.with_workdir("/app")
22+
.with_mounted_cache("/root/.cache/pip", dag.cache_volume("python-pip"))
23+
.with_exec(["pip", "install", "-r", "requirements.txt"])
24+
)
25+
26+
@function
27+
async def test(self) -> str:
28+
"""Runs the tests in the source code and returns the output"""
29+
postgresdb = (
30+
dag.container()
31+
.from_("postgres:alpine")
32+
.with_env_variable("POSTGRES_DB", "app_test")
33+
.with_env_variable("POSTGRES_PASSWORD", "secret")
34+
.with_exposed_port(5432)
35+
.as_service(args=[], use_entrypoint=True)
36+
)
37+
38+
cmd = (
39+
self.env()
40+
.with_service_binding("db", postgresdb)
41+
.with_env_variable("DATABASE_URL", "postgresql://postgres:secret@db/app_test")
42+
.with_exec(["pytest"])
43+
)
44+
return await cmd.stdout()
45+
46+
@function
47+
async def publish(self) -> str:
48+
"""Builds and publishes the application container to a registry"""
49+
await self.test()
50+
return await (
51+
self.env()
52+
.with_exposed_port(8000)
53+
.with_entrypoint(["fastapi", "run", "main.py"])
54+
.publish(f"ttl.sh/my-fastapi-app-{random.randrange(10**8)}")
55+
)

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)