Skip to content

Commit f25845a

Browse files
authored
Merge pull request #919 from sanders41/docker
Add setuptools fastapi dockerfile
2 parents b19969e + 3817716 commit f25845a

8 files changed

+186
-5
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ jobs:
408408
- name: Test with pytest
409409
working-directory: ${{ env.WORKING_DIR }}
410410
if: matrix.project_type == 'application'
411-
run: pytest
411+
run: python -m pytest
412412
pixi-linting:
413413
strategy:
414414
fail-fast: false

.github/workflows/testing_fastapi.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,82 @@ jobs:
196196
working-directory: ${{ env.WORKING_DIR }}
197197
if: matrix.project_type == 'application'
198198
run: poetry run pytest
199+
test-setuptools-fastapi-project:
200+
name: test-fastapi-setuptools-setup-fastapi
201+
strategy:
202+
fail-fast: false
203+
runs-on: ubuntu-latest
204+
steps:
205+
- uses: actions/checkout@v5
206+
- name: install Rust
207+
uses: dtolnay/rust-toolchain@stable
208+
- name: Cache Rust dependencies
209+
uses: Swatinem/[email protected]
210+
- name: Install sqlx-cli
211+
run: cargo install sqlx-cli --no-default-features -F native-tls -F postgres
212+
- name: Set up Python
213+
uses: actions/setup-python@v6
214+
with:
215+
python-version: "${{ env.MIN_PYTHON_VERSION }}"
216+
- name: Build package
217+
run: cargo build --release -F fastapi
218+
- name: Run creation
219+
run: ./scripts/ci_run_fastapi.sh "fastapi" 4
220+
shell: bash
221+
- name: Install Dependencies
222+
working-directory: ${{ env.WORKING_DIR }}
223+
run: |
224+
python -m pip install -U pip
225+
python -m pip install -r requirements-dev.txt -r requirements.txt
226+
- name: Pre-commit check
227+
working-directory: ${{ env.WORKING_DIR }}
228+
run: |
229+
pre-commit install
230+
git add .
231+
pre-commit run --all-files
232+
- name: make .env
233+
working-directory: ${{ env.WORKING_DIR }}
234+
run: touch .env
235+
- name: Build and start Docker containers
236+
working-directory: ${{ env.WORKING_DIR }}
237+
run: docker compose up -d
238+
- name: Test with pytest
239+
working-directory: ${{ env.WORKING_DIR }}
240+
run: python -m pytest -n auto
241+
test-setuptools-non-fastapi-project:
242+
name: test-fastapi-setuptools-setup-non-fastapi
243+
strategy:
244+
fail-fast: false
245+
matrix:
246+
project_type: ["application", "lib"]
247+
runs-on: ubuntu-latest
248+
steps:
249+
- uses: actions/checkout@v5
250+
- name: install Rust
251+
uses: dtolnay/rust-toolchain@stable
252+
- name: Cache Rust dependencies
253+
uses: Swatinem/[email protected]
254+
- name: Set up Python
255+
uses: actions/setup-python@v6
256+
with:
257+
python-version: "${{ env.MIN_PYTHON_VERSION }}"
258+
- name: Build package
259+
run: cargo build --release -F fastapi
260+
- name: Run creation
261+
run: ./scripts/ci_run_fastapi.sh ${{ matrix.project_type }} 4
262+
shell: bash
263+
- name: Install Dependencies
264+
working-directory: ${{ env.WORKING_DIR }}
265+
run: |
266+
python -m pip install -U pip
267+
python -m pip install -r requirements-dev.txt
268+
- name: Pre-commit check
269+
working-directory: ${{ env.WORKING_DIR }}
270+
run: |
271+
pre-commit install
272+
git add .
273+
pre-commit run --all-files
274+
- name: Test with pytest
275+
working-directory: ${{ env.WORKING_DIR }}
276+
if: matrix.project_type == 'application'
277+
run: python -m pytest

src/fastapi/docker_files.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,74 @@ EXPOSE 8000
532532
533533
USER appuser
534534
535+
ENTRYPOINT ["./entrypoint.sh"]
536+
"#
537+
),
538+
ProjectManager::Setuptools => format!(
539+
r#"# syntax=docker/dockerfile:1
540+
541+
FROM ubuntu:24.04 AS builder
542+
543+
WORKDIR /app
544+
545+
ENV \
546+
PYTHONUNBUFFERED=true \
547+
PATH="/root/.local/bin:$PATH"
548+
549+
RUN : \
550+
&& apt-get update \
551+
&& apt-get install -y --no-install-recommends \
552+
software-properties-common \
553+
&& add-apt-repository ppa:deadsnakes/ppa \
554+
&& apt-get update \
555+
&& apt-get install -y --no-install-recommends \
556+
python{python_version} \
557+
python{python_version}-venv \
558+
&& apt-get clean \
559+
&& rm -rf /var/lib/apt/lists/*
560+
561+
COPY . ./
562+
563+
RUN : \
564+
&& python{python_version} -m venv .venv \
565+
&& .venv/bin/python -m pip install -r requirements.txt
566+
567+
568+
# Build production stage
569+
FROM ubuntu:24.04 AS prod
570+
571+
ENV \
572+
PYTHONUNBUFFERED=true \
573+
PATH="/app/.venv/bin:$PATH" \
574+
PORT="8000"
575+
576+
RUN : \
577+
&& apt-get update \
578+
&& apt-get install -y --no-install-recommends \
579+
software-properties-common \
580+
&& add-apt-repository ppa:deadsnakes/ppa \
581+
&& apt-get update \
582+
&& apt-get install -y --no-install-recommends \
583+
python3.13 \
584+
&& apt-get clean \
585+
&& rm -rf /var/lib/apt/lists/*
586+
587+
RUN useradd appuser
588+
589+
WORKDIR /app
590+
591+
RUN chown appuser:appuser /app
592+
593+
COPY --from=builder /app/.venv /app/.venv
594+
COPY --from=builder /app/{source_dir} /app/{source_dir}
595+
COPY ./scripts/entrypoint.sh /app
596+
597+
RUN chmod +x /app/entrypoint.sh
598+
599+
EXPOSE 8000
600+
601+
USER appuser
602+
535603
ENTRYPOINT ["./entrypoint.sh"]
536604
"#
537605
),
@@ -692,4 +760,20 @@ mod tests {
692760

693761
assert_yaml_snapshot!(content);
694762
}
763+
764+
#[test]
765+
fn test_save_dockerfile_setuptools() {
766+
let mut project_info = project_info_dummy();
767+
project_info.project_manager = ProjectManager::Setuptools;
768+
let base = project_info.base_dir();
769+
create_dir_all(&base).unwrap();
770+
let expected_file = base.join("Dockerfile");
771+
save_dockerfile(&project_info).unwrap();
772+
773+
assert!(expected_file.is_file());
774+
775+
let content = std::fs::read_to_string(expected_file).unwrap();
776+
777+
assert_yaml_snapshot!(content);
778+
}
695779
}

src/fastapi/fastapi_installer.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ fn setuptools_fastapi_depencency_installer(project_info: &ProjectInfo) -> Result
131131
bail!("Failed to install FastAPI dependencies: {stderr}");
132132
}
133133

134+
let freeze = std::process::Command::new(".venv/bin/python")
135+
.args(["-m", "pip", "freeze"])
136+
.current_dir(project_info.base_dir())
137+
.output()?;
138+
139+
if !freeze.status.success() {
140+
let stderr = String::from_utf8_lossy(&freeze.stderr);
141+
bail!("Failed to get pip freeze output: {stderr}");
142+
}
143+
144+
let requirements_path = project_info.base_dir().join("requirements.txt");
145+
std::fs::write(requirements_path, freeze.stdout)?;
146+
134147
Ok(())
135148
}
136149

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: src/fastapi/docker_files.rs
3+
expression: content
4+
---
5+
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/root/.local/bin:$PATH\"\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n python3.11-venv \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\nCOPY . ./\n\nRUN : \\\n && python3.11 -m venv .venv \\\n && .venv/bin/python -m pip install -r requirements.txt\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.13 \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"

src/github_actions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ jobs:
18251825
- name: Install Dependencies
18261826
run: |
18271827
python -m pip install -U pip
1828-
python -m pip -r requirements-dev.txt
1828+
python -m pip install -r requirements-dev.txt
18291829
python -m pip install build setuptools wheel twine
18301830
- name: Build and publish package
18311831
run: |
@@ -2057,7 +2057,7 @@ jobs:
20572057
- name: Install Dependencies
20582058
run: |
20592059
python -m pip install -U pip
2060-
python -m pip -r requirements-dev.txt
2060+
python -m pip install -r requirements-dev.txt
20612061
- name: Publish docs
20622062
run: mkdocs gh-deploy --force
20632063
"#

src/snapshots/python_project__github_actions__tests__save_docs_publish_file_setuptools.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/github_actions.rs
33
expression: content
44
---
5-
"name: Docs Publish\non:\n release:\n types:\n - published\njobs:\n deploy:\n runs-on: ubuntu-latest\n steps:\n - uses: actions/checkout@v5\n - name: Set up Python\n uses: actions/setup-python@v6\n with:\n python-version: \"3.12\"\n cache: \"pip\"\n - name: Install Dependencies\n run: |\n python -m pip install -U pip\n python -m pip -r requirements-dev.txt\n - name: Publish docs\n run: mkdocs gh-deploy --force\n"
5+
"name: Docs Publish\non:\n release:\n types:\n - published\njobs:\n deploy:\n runs-on: ubuntu-latest\n steps:\n - uses: actions/checkout@v5\n - name: Set up Python\n uses: actions/setup-python@v6\n with:\n python-version: \"3.12\"\n cache: \"pip\"\n - name: Install Dependencies\n run: |\n python -m pip install -U pip\n python -m pip install -r requirements-dev.txt\n - name: Publish docs\n run: mkdocs gh-deploy --force\n"

src/snapshots/python_project__github_actions__tests__save_pypi_publish_file_setuptools.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/github_actions.rs
33
expression: content
44
---
5-
"name: PyPi Publish\non:\n release:\n types:\n - published\njobs:\n deploy:\n runs-on: ubuntu-latest\n permissions:\n # For PyPI's trusted publishing.\n id-token: write\n steps:\n - uses: actions/checkout@v5\n - name: Set up Python\n uses: actions/setup-python@v6\n with:\n python-version: \"3.12\"\n cache: \"pip\"\n - name: Install Dependencies\n run: |\n python -m pip install -U pip\n python -m pip -r requirements-dev.txt\n python -m pip install build setuptools wheel twine\n - name: Build and publish package\n run: |\n python -m build\n twine upload dist/*\n"
5+
"name: PyPi Publish\non:\n release:\n types:\n - published\njobs:\n deploy:\n runs-on: ubuntu-latest\n permissions:\n # For PyPI's trusted publishing.\n id-token: write\n steps:\n - uses: actions/checkout@v5\n - name: Set up Python\n uses: actions/setup-python@v6\n with:\n python-version: \"3.12\"\n cache: \"pip\"\n - name: Install Dependencies\n run: |\n python -m pip install -U pip\n python -m pip install -r requirements-dev.txt\n python -m pip install build setuptools wheel twine\n - name: Build and publish package\n run: |\n python -m build\n twine upload dist/*\n"

0 commit comments

Comments
 (0)