Skip to content

Commit

Permalink
ci: add dagger
Browse files Browse the repository at this point in the history
  • Loading branch information
fredleger committed Nov 23, 2023
1 parent e45a33a commit edcb4a0
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 36 deletions.
122 changes: 122 additions & 0 deletions .dagger/ci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import random
import sys
import uuid

import anyio
import dagger


async def main():
config = dagger.Config(log_output=sys.stdout)

async with dagger.Connection(config) as client:
base_image = "python:3.10.0-slim"

# set build context
src = client.host().directory(".")
pc_cache = client.cache_volume("pc")

# run pre-commit hooks
# FIXME: pre-commit is not working because we don't mount .git inside the container
# pre_commit = (
# await docker_image_dev.with_directory("/code", src)
# .with_workdir("/code")
# .with_mounted_cache("/cache", pc_cache)
# .with_env_variable("PRE_COMMIT_HOME", "/cache")
# .with_entrypoint(["/bin/bash"])
# .with_exec(["git", "remote", "-v"])
# .with_exec(["pre-commit", "run", "--all-files"])
# .stdout()
# )
# print(pre_commit)

# build the prod docker image
docker_image_prod = await src.docker_build(target="prod")

# build the dev docker image
docker_image_dev = await src.docker_build(target="dev")

# lint
print("Running linting")
lint = (
await docker_image_dev.with_directory("/code", src)
.with_workdir("/code")
.with_entrypoint(["/bin/sh", "-c"])
.with_exec(["/bin/bash", "scripts/lint.sh"])
.stdout()
)
print(lint)

# format
print("Running format")
format = (
await docker_image_dev.with_directory("/code", src)
.with_workdir("/code")
.with_entrypoint(["/bin/sh", "-c"])
.with_exec(["/bin/bash", "scripts/format.sh"])
.stdout()
)
print(format)

# unit tests
print("Running unit tests")
unit = (
await docker_image_dev.with_directory("/code", src)
.with_workdir("/code")
.with_entrypoint(["/bin/sh", "-c"])
.with_exec(["/bin/bash", "scripts/unit-tests.sh"])
.stdout()
)
print(unit)

# coverage
print("Running coverage")
coverage = (
await docker_image_dev.with_directory("/code", src)
.with_workdir("/code")
.with_entrypoint(["/bin/sh", "-c"])
.with_exec(["/bin/bash", "scripts/coverage.sh"])
.stdout()
)
print(coverage)

# fonctional tests
print("Running fonctional tests")
# FIXME: this should work but it hangs on stopping the mongo process after initial setup
# mongo = (
# client.container()
# .from_("bitnami/mongodb:4.4-debian-10")
# .with_env_variable("MONGODB_ROOT_PASSWORD", "root")
# .with_env_variable("MONGODB_USERNAME", "app")
# .with_env_variable("MONGODB_PASSWORD", "app")
# .with_env_variable("MONGODB_DATABASE", "test")
# .with_exposed_port(27017)
# .as_service()
# )
mongo = client.host().service(
[
dagger.PortForward(
backend=27017, frontend=27017, protocol=dagger.NetworkProtocol.TCP
)
]
)

api = (
docker_image_prod
.with_env_variable("MONGO_URI", "mongodb://app:app@mongo:27017/test")
.with_service_binding("mongo", mongo)
.with_exposed_port(8000)
.as_service()
)

newman = (
await docker_image_dev
.with_directory("/code", src)
.with_workdir("/code")
.with_service_binding("api", api)
.with_entrypoint(["/bin/bash", "scripts/functional-tests.sh"])
.stdout()
)
print(newman)

anyio.run(main)
19 changes: 11 additions & 8 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
.venv
.vscode
.helm
_CI.md
.dagger
.dockerignore
.env
.git
.github
.helm
.mypy_cache
.pytest_cache
.env
virtualenv
README.md
_CI.md
.venv
.vscode
Conduit.postman_collection.json
docker-compose*.yaml
docker-compose*.yml
docker/data/**
Conduit.postman_collection.json
README.md
virtualenv
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ ignore =
# See https://github.com/PyCQA/pycodestyle/issues/373
E203,
W503,
exclude =
.dagger/*.py
39 changes: 39 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI Workflow (dagger)

on:
push:
branches:
- ci/dagger

jobs:
realworld-tests:
timeout-minutes: 10
runs-on: ubuntu-latest

services:
mongo:
image: bitnami/mongodb:4.4-debian-10
ports:
- 27017:27017
env:
MONGODB_ROOT_PASSWORD: root
MONGODB_USERNAME: app
MONGODB_PASSWORD: app
MONGODB_DATABASE: test

steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
cache: "pip"

- name: Dagger install
run: pip install dagger.io==0.9.3

- name: Run dagger tests
run: python .dagger/ci.py
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dagger 0.6.1
22 changes: 4 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
all: build ci publish

build: docker-build
ci: lint format unit-tests coverage functional-tests ci-clean
ci: dagger
publish: docker-publish

lint:
bash scripts/lint.sh

format:
bash scripts/format.sh

pre-commit:
bash scripts/pre-commit.sh

unit-tests:
bash scripts/unit-tests.sh

coverage:
bash scripts/coverage.sh

functional-tests:
bash scripts/functional-tests.sh
dagger:
pip install dagger.io==0.9.3 && \
python .dagger/ci.py

ci-clean:
rm -rf .pytest_cache
Expand Down
5 changes: 5 additions & 0 deletions nohup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Creating virtualenv fastapi-odmantic-example-MATOk_fk-py3.10 in /root/.cache/pypoetry/virtualenvs
INFO: Started server process [8]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ click==8.1.3
colorama==0.4.6
crashtest==0.4.1
cryptography==37.0.4
dagger-io==0.6.1
dagger-io==0.9.3
distlib==0.3.6
dnspython==2.3.0
dulwich==0.21.5
Expand All @@ -32,6 +32,7 @@ idna==3.3
importlib-metadata==6.6.0
installer==0.7.0
jaraco.classes==3.2.3
jeepney==0.8.0
jsonschema==4.17.3
keyring==23.13.1
lockfile==0.12.2
Expand Down Expand Up @@ -64,6 +65,7 @@ requests==2.31.0
requests-toolbelt==1.0.0
rich==13.4.1
rsa==4.9
SecretStorage==3.3.3
shellingham==1.5.0.post1
six==1.16.0
sniffio==1.2.0
Expand All @@ -72,7 +74,7 @@ tomli==2.0.1
tomlkit==0.11.8
trove-classifiers==2023.5.24
typer==0.9.0
typing_extensions==4.6.3
typing_extensions==4.8.0
urllib3==1.26.16
uvicorn==0.18.3
virtualenv==20.23.0
Expand Down
7 changes: 2 additions & 5 deletions scripts/functional-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
set -e

echo "Starting API stack"
docker compose up -d api
echo "Waiting for API container to be up"
bash scripts/wait-for-it.sh localhost:8000 --timeout=60 --strict -- echo "API is up"
bash scripts/wait-for-it.sh api:8000 --timeout=60 --strict -- echo "API is up"
echo "Waiting for API to be healthy"
until curl --output /dev/null --silent --fail http://localhost:8000/health; do
until curl --output /dev/null --silent --fail http://api:8000/health; do
printf '.'
sleep 5
done
echo ""
echo "API is healthy"
echo "Running functional tests"
bash scripts/test.sh
docker compose stop
10 changes: 9 additions & 1 deletion scripts/start-mongo.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#!/bin/bash

docker run --rm -p 27017:27017 --name fastapi-odmantic-example -d mongo:4
set -e

docker run --rm -d --name mongodb \
-e MONGODB_ROOT_PASSWORD="root" \
-e MONGODB_USERNAME="app" \
-e MONGODB_PASSWORD="app" \
-e MONGODB_DATABASE="test" \
-p 27017:27017 \
bitnami/mongodb:4.4-debian-10
5 changes: 4 additions & 1 deletion scripts/stop-mongo.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#!/bin/bash
docker stop fastapi-odmantic-example

set -e

docker stop mongodb
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e

SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
export APIURL=http://realworld-127-0-0-1.sslip.io:8000
export APIURL=http://api:8000

USERNAME=${USERNAME:-u$(date +%s)}
EMAIL=${EMAIL:-$USERNAME@mail.com}
Expand Down

0 comments on commit edcb4a0

Please sign in to comment.