Skip to content

Commit c727ec2

Browse files
committed
Add Dagger workflow
Signed-off-by: Vikram Vaswani <112123850+vikram-dagger@users.noreply.github.com>
1 parent 1e5b53e commit c727ec2

File tree

8 files changed

+911
-0
lines changed

8 files changed

+911
-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.13"
5+
dependencies = ["dagger-io"]
6+
7+
[build-system]
8+
requires = ["uv_build>=0.8.4,<0.9.0"]
9+
build-backend = "uv_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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import random
2+
import dagger
3+
from dagger import dag, function, object_type
4+
5+
6+
@object_type
7+
class Book:
8+
9+
@function
10+
def env(self, source: dagger.Directory) -> dagger.Container:
11+
"""Returns a base image"""
12+
return (
13+
dag.container()
14+
.from_("python:3.11")
15+
.with_exec(["sh", "-c", "apt-get update && apt-get install -y libpq-dev"])
16+
.with_directory("/app", source)
17+
.with_workdir("/app")
18+
.with_mounted_cache("/root/.cache/pip", dag.cache_volume("python-pip"))
19+
.with_exec(["pip", "install", "-r", "requirements.txt"])
20+
.with_exposed_port(8000)
21+
.with_entrypoint(["fastapi", "run", "main.py", "--port", "8000"])
22+
)
23+
24+
@function
25+
async def test(self, source: dagger.Directory) -> str:
26+
"""Runs tests"""
27+
postgres = (
28+
dag.container()
29+
.from_("postgres:15-alpine")
30+
.with_env_variable("POSTGRES_USER", "app_user")
31+
.with_env_variable("POSTGRES_PASSWORD", "secret")
32+
.with_file("/docker-entrypoint-initdb.d/init-dbs.sh", source.file("./init-dbs.sh"))
33+
.with_exposed_port(5432)
34+
.as_service(args=[], use_entrypoint=True)
35+
)
36+
37+
return await (
38+
self.env(source)
39+
.with_service_binding("db", postgres)
40+
.with_env_variable("DATABASE_URL", "postgresql://app_user:secret@db/app_db")
41+
.with_env_variable("TEST_DATABASE_URL", "postgresql://app_user:secret@db/app_db_test")
42+
.with_exec(["pytest"])
43+
.stdout()
44+
)
45+
46+
@function
47+
async def publish(self, source: dagger.Directory) -> str:
48+
"""Publishes the image"""
49+
await self.test(source)
50+
return await (
51+
self.env(source)
52+
.publish(f"ttl.sh/my-fastapi-app-{random.randrange(10000)}")
53+
)
54+
55+
@function
56+
def container_echo(self, string_arg: str) -> dagger.Container:
57+
"""Returns a container that echoes whatever string argument is provided"""
58+
return dag.container().from_("alpine:latest").with_exec(["echo", string_arg])
59+
60+
@function
61+
async def grep_dir(self, directory_arg: dagger.Directory, pattern: str) -> str:
62+
"""Returns lines that match a pattern in the files of the provided Directory"""
63+
return await (
64+
dag.container()
65+
.from_("alpine:latest")
66+
.with_mounted_directory("/mnt", directory_arg)
67+
.with_workdir("/mnt")
68+
.with_exec(["grep", "-R", pattern, "."])
69+
.stdout()
70+
)

.dagger/uv.lock

Lines changed: 776 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.19.2"
22+
verb: call
23+
args: test --source=.
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.19.2",
4+
"sdk": {
5+
"source": "python"
6+
},
7+
"source": ".dagger"
8+
}

0 commit comments

Comments
 (0)