Skip to content

Commit 057aa5a

Browse files
feat: add fastapi docker (#52)
## What I'm changing - Adds a `.dockerignore` that defines the files that get moved into the docker image. - Adds a `Dockerfile` that installs the Python package based on the uv lock file and executes the FastAPI application. - Adds a GitHub job executed on pushed tags that builds the image and pushes them to the GitHub registry. closes #48 ## How I did it - We put the GitHub job into the release workflow, as it should only be executed on release. - Therefore, we renamed the existing jobs to associate them with the pypi release process. ## Checklist - [x] Tests pass: `uv run pytest` - [x] Checks pass: `uv run pre-commit --all-files` - [x] CHANGELOG is updated (if necessary) --------- Co-authored-by: Pete Gadomski <[email protected]>
1 parent 55833aa commit 057aa5a

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

.dockerignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
*
2+
3+
!/pyproject.toml
4+
!/README.md
5+
!/uv.lock
6+
7+
!/stapi-fastapi/pyproject.toml
8+
!/stapi-fastapi/README.md
9+
!/stapi-fastapi/src/*
10+
!/stapi-fastapi/tests/*
11+
12+
!/stapi-pydantic/pyproject.toml
13+
!/stapi-pydantic/README.md
14+
!/stapi-pydantic/src/*
15+
!/stapi-pydantic/tests/*
16+
17+
!/pystapi-validator/pyproject.toml
18+
!/pystapi-validator/README.md
19+
!/pystapi-validator/src/*
20+
!/pystapi-validator/tests/*

.github/workflows/ci.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ jobs:
3838
if: ${{ matrix.python-version == '3.12' }}
3939
with:
4040
path: site/
41+
- name: Set up Docker Buildx
42+
uses: docker/setup-buildx-action@v3
43+
- name: Build Docker Image
44+
uses: docker/build-push-action@v6
45+
with:
46+
push: false
47+
build-args: PYTHON_VERSION=${{ matrix.python-version }}
4148

4249
docs:
4350
name: Deploy docs
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
TAG: ${{ github.ref_name }}
1919
id: split
2020
run: echo "package_name=${TAG%%/*}" >> $GITHUB_OUTPUT
21+
2122
release:
2223
name: Release ${{ needs.package_name.outputs.package_name }}
2324
runs-on: ubuntu-latest
@@ -43,3 +44,30 @@ jobs:
4344
- uses: pypa/gh-action-pypi-publish@release/v1
4445
with:
4546
packages-dir: ${{ needs.package_name.outputs.package_name}}/dist
47+
48+
docker:
49+
name: Build Docker Image
50+
runs-on: ubuntu-latest
51+
needs: package_name
52+
if: ${{ needs.package_name.outputs.package_name }} == 'stapi-fastapi'
53+
54+
permissions:
55+
contents: read
56+
packages: write
57+
id-token: write
58+
59+
steps:
60+
- name: Log in to the Container Registry
61+
uses: docker/login-action@v3
62+
with:
63+
registry: ghcr.io
64+
username: ${{ github.actor }}
65+
password: ${{ secrets.GITHUB_TOKEN }}
66+
- name: Set up Docker Buildx
67+
uses: docker/setup-buildx-action@v3
68+
- name: Build and Push Docker Image
69+
uses: docker/build-push-action@v6
70+
with:
71+
push: true
72+
tags: stapi-spec/stapi-fastapi:${{ github.ref_name }}
73+
build-args: PYTHON_VERSION='3.12'

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ARG PYTHON_VERSION=3.12
2+
ARG BASE_IMAGE=ghcr.io/astral-sh/uv:python${PYTHON_VERSION}-alpine
3+
4+
FROM ${BASE_IMAGE} AS base
5+
6+
WORKDIR /app
7+
COPY . /app
8+
9+
RUN uv sync --frozen
10+
11+
CMD ["uv", "run", "fastapi", "dev", "/app/stapi-fastapi/tests/application.py", "--host", "0.0.0.0", "--port", "80"]

0 commit comments

Comments
 (0)