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

Draft: Add dockerfile and deploy examples #8

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
26 changes: 26 additions & 0 deletions .github/workflows/build_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Build Docker Image
on:
push:
branches:
- master
- main # TODO: Remove this file before merging to main

jobs:
build_docker:
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- run: docker build -t ghcr.io/$GITHUB_REPOSITORY:latest .
- name: Push built image
run: docker push ghcr.io/$GITHUB_REPOSITORY --all-tags
28 changes: 28 additions & 0 deletions .github/workflows/deploy_azure_container_apps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Azure Container Apps Deploy

on:
push:
branches:
- master
- main
- add-dockerfile # TODO: Remove this before merging to main

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Log in to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}

- name: Build and deploy Container App
uses: azure/container-apps-deploy-action@v1
with:
appSourcePath: ${{ github.workspace }}
acrName: ca4949f1fb1bacr
containerAppName: faststream-gen-test
resourceGroup: kumaran_rg_1622
48 changes: 48 additions & 0 deletions .github/workflows/deploy_docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build FastStream Docs
on:
push:
branches:
- master
- main
- add-dockerfile # TODO: Remove this before merging to main

jobs:
deploy_docs:
name: Deploy FastStream Docs to GitHub Pages
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ inputs.version }}
- name: Install Dependencies
shell: bash
run: |
set -ux
python -m pip install --upgrade pip
test -f pyproject.toml && pip install -e "." || echo "pyproject.toml doesn't exists"
test -f requirements.txt && pip install -r requirements.txt || echo "requirements.txt doesn't exists"
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Generate asyncapi.yaml
shell: bash
run: |
faststream docs gen --yaml app.application:app
- name: Generate AsyncAPI Docs
shell: bash
run: |
npx --version
npx -y -p @asyncapi/generator ag --version
mkdir asyncapi_docs
npx -y -p @asyncapi/generator ag asyncapi.yaml @asyncapi/html-template -o asyncapi_docs
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
force_orphan: true
publish_dir: ./asyncapi_docs
# The following lines assign commit authorship to the official GH-Actions bot for deploys to `gh-pages` branch.
# You can swap them out with your own user credentials.
user_name: github-actions[bot]
user_email: 41898282+github-actions[bot]@users.noreply.github.com
67 changes: 67 additions & 0 deletions .github/workflows/deploy_to_ecs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Deploy to Amazon ECS

on:
push:
branches:
- main
- main
- add-dockerfile # TODO: Remove this before merging to main

env:
AWS_REGION: us-east-2 # set this to your preferred AWS region, e.g. us-west-1
ECR_REPOSITORY: faststream_gen # set this to your Amazon ECR repository name
ECS_SERVICE: faststream_gen_service # set this to your Amazon ECS service name
ECS_CLUSTER: faststream_gen_cluster # set this to your Amazon ECS cluster name
ECS_TASK_DEFINITION: ./ecs-task-definition.json # set this to the path to your Amazon ECS task definition
# file, e.g. .aws/task-definition.json
CONTAINER_NAME: faststream_gen_container # set this to the name of the container in the
# containerDefinitions section of your task definition

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@0e613a0980cbf65ed5b322eb7a1e075d28913a83
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@c804dfbdd57f713b6c079302a4c01db7017a36fc
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

asyncapi.json
asyncapi.yaml
asyncapi_docs
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.9-slim-bullseye

SHELL ["/bin/bash", "-c"]
WORKDIR /project

ADD app /project/app
COPY requirements.txt /project/

RUN pip install --no-cache-dir -r /project/requirements.txt

CMD ["faststream", "run", "--workers", "1", "app.application:app"]
27 changes: 27 additions & 0 deletions app/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pydantic import BaseModel, Field

from faststream import FastStream, Logger
from faststream.kafka import KafkaBroker


class MultiplyMessage(BaseModel):
a: int = Field(..., examples=[2], description="Integer value")
b: int = Field(..., examples=[3], description="Integer value")


class Results(BaseModel):
res: int = Field(..., examples=[6], description="Result of multiplication")


broker = KafkaBroker("localhost:9092")
app = FastStream(broker)


@broker.subscriber("multiply")
@broker.publisher("results")
async def multiply_numbers(msg: MultiplyMessage, logger: Logger) -> Results:
logger.info(msg)

result = msg.a * msg.b
result_obj = Results(res=result)
return result_obj
24 changes: 24 additions & 0 deletions ecs-task-definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"family": "faststream_gen_family",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"name": "faststream_gen_container",
"image": "617504802562.dkr.ecr.us-east-2.amazonaws.com/faststream_gen:latest",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
],
"essential": true
}
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "256",
"memory": "512",
"executionRoleArn": "arn:aws:iam::617504802562:role/ecsTaskExecutionRole"
}
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "app"
version = "0.0.1"
14 changes: 14 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from faststream.kafka import TestKafkaBroker

from app.application import MultiplyMessage, Results, broker, multiply_numbers


@pytest.mark.asyncio
async def test_multiply_numbers():
async with TestKafkaBroker(broker):
@broker.subscriber("results")
async def on_result(msg: Results):
assert msg.res == 6

await broker.publish(MultiplyMessage(a=2, b=3), "multiply")