Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding continuous integration #1

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Build and test

on:
pull_request:
workflow_call:
inputs:
environment:
type: string
description: "Environment to deploy to"
required: false
version:
type: string
description: Version to build
required: true
push:
type: boolean
description: Whether to push the image or not
required: false
default: true

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Python
uses: actions/setup-python@v4

- name: Install poetry
uses: abatilo/actions-poetry@v4

- name: Setup a local virtual environment
run: |
poetry config virtualenvs.create true --local
poetry config virtualenvs.in-project true --local

- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
path: ./.venv
key: venv-${{ hashFiles('poetry.lock') }}

- name: Install the project dependencies
run: poetry install

- name: Run the automated tests (for example)
run: poetry run pytest -v

build:
uses: ./.github/workflows/docker.yml
needs: [test]
with:
environment: ${{ inputs.environment }}
image: europe-west1-docker.pkg.dev/marble-infra/marble/marble-ner:${{ inputs.version || 'dev' }}
push: ${{ inputs.push || false }}

53 changes: 53 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Build container image

on:
workflow_call:
inputs:
environment:
type: string
description: "Environment to deploy to"
required: false
image:
type: string
description: "Version to build"
required: true
push:
type: boolean
description: "Whether to push the container image or not"
default: false

jobs:
build_container_image:
name: Build container image
environment: ${{ inputs.environment }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Docker
uses: docker/setup-buildx-action@v3

- name: Authenticate to GCP
id: auth
uses: google-github-actions/auth@v2
if: ${{ inputs.push }}
with:
token_format: access_token
project_id: ${{ vars.GCP_PROJECT_ID }}
workload_identity_provider: ${{ vars.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ vars.SERVICE_ACCOUNT_EMAIL }}

- name: Authenticate to Artifact Registry
uses: docker/login-action@v3
if: ${{ inputs.push }}
with:
registry: europe-west1-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}

- name: Build container image
uses: docker/build-push-action@v5
with:
push: ${{ inputs.push }}
tags: ${{ inputs.image }}
18 changes: 18 additions & 0 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Deploy to staging

on:
push:
branches: [main]

jobs:
build:
uses: ./.github/workflows/build.yml
with:
environment: staging
version: latest

deploy:
needs: [build]
runs-on: ubuntu-latest
steps:
- run: echo Deploying...
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ COPY pyproject.toml poetry.lock /app/
RUN \
poetry install && \
poetry run python -c 'import os, gliner; gliner.GLiNER.from_pretrained(os.getenv("GLINER_MODEL"))' && \
poetry cache clear --all ''
poetry cache clear -n --all '' && \
rm -rf /root/.cache/pypoetry/artifacts

COPY . /app

Expand Down
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Match(BaseModel):
model = GLiNER.from_pretrained(MODEL)
app = FastAPI()

@app.get("/-/health")
def healthcheck():
pass

@app.post("/detect")
def detect(query: Query):
labels = LABELS
Expand Down
570 changes: 446 additions & 124 deletions poetry.lock

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ dependencies = [
"uvicorn[default] (>=0.34.0,<0.35.0)",
"gunicorn (>=23.0.0,<24.0.0)",
"torch (>=2.6.0,<3.0.0)",
"gliner (>=0.2.16,<0.3.0)"
"gliner (>=0.2.16,<0.3.0)",
"httpx (>=0.28.1,<0.29.0)",
]

[tool.poetry]
package-mode = false

[[tool.poetry.source]]
name = "pytorch"
url = "https://download.pytorch.org/whl/cpu"
priority = "explicit"

[tool.poetry.dependencies]
torch = { source = "pytorch" }
torchvision = { source = "pytorch" }
[tool.poetry.group.dev.dependencies]
pytest = "^8.3.4"

[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
Expand Down
20 changes: 20 additions & 0 deletions test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

def test_healthcheck():
response = client.get('/-/health')

assert response.status_code == 200

def test_detection():
response = client.post('/detect', json={"text": "dinner with joe finnigan"})

assert response.status_code == 200

json = response.json()

assert len(json) == 1
assert json[0]['type'] == "Person"
assert json[0]['text'] == "joe finnigan"
Loading