Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Jan 25, 2024
1 parent 3498333 commit c6d116a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 30 deletions.
61 changes: 43 additions & 18 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,27 @@ jobs:
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
- name: Pull images
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
docker pull ${{ env.IMAGE }}-builder:latest || true
docker pull ${{ env.IMAGE }}-final:latest || true
- name: Build images
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE }}:latest \
--target builder \
--cache-from ${{ env.IMAGE }}-builder:latest \
--tag ${{ env.IMAGE }}-builder:latest \
--file ./project/Dockerfile.prod \
"./project"
docker build \
--cache-from ${{ env.IMAGE }}-final:latest \
--tag ${{ env.IMAGE }}-final:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Push image
- name: Push images
run: |
docker push ${{ env.IMAGE }}:latest
docker push ${{ env.IMAGE }}-builder:latest
docker push ${{ env.IMAGE }}-final:latest
test:
name: Test Docker Image
Expand All @@ -46,14 +54,21 @@ jobs:
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
- name: Pull images
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
docker pull ${{ env.IMAGE }}-builder:latest || true
docker pull ${{ env.IMAGE }}-final:latest || true
- name: Build images
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE }}:latest \
--target builder \
--cache-from ${{ env.IMAGE }}-builder:latest \
--tag ${{ env.IMAGE }}-builder:latest \
--file ./project/Dockerfile.prod \
"./project"
docker build \
--cache-from ${{ env.IMAGE }}-final:latest \
--tag ${{ env.IMAGE }}-final:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Run container
Expand All @@ -66,7 +81,9 @@ jobs:
-e DATABASE_URL=sqlite://sqlite.db \
-e DATABASE_TEST_URL=sqlite://sqlite.db \
-p 5003:8765 \
${{ env.IMAGE }}:latest
${{ env.IMAGE }}-final:latest
- name: Install requirements
run: docker exec fastapi-tdd pip install black==23.12.1 flake8==7.0.0 isort==5.13.2 pytest==7.4.4
- name: Pytest
run: docker exec fastapi-tdd python -m pytest .
- name: Flake8
Expand All @@ -92,13 +109,21 @@ jobs:
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull image
- name: Pull images
run: |
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
docker pull ${{ env.IMAGE }}-builder:latest || true
docker pull ${{ env.IMAGE }}-final:latest || true
- name: Build images
run: |
docker build \
--cache-from ${{ env.IMAGE }}:latest \
--target builder \
--cache-from ${{ env.IMAGE }}-builder:latest \
--tag ${{ env.IMAGE }}-builder:latest \
--file ./project/Dockerfile.prod \
"./project"
docker build \
--cache-from ${{ env.IMAGE }}-final:latest \
--tag ${{ env.IMAGE }}:latest \
--tag ${{ env.HEROKU_REGISTRY_IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
Expand All @@ -107,7 +132,7 @@ jobs:
env:
HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }}
- name: Push to the registry
run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }}
run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }}:latest
- name: Set environment variables
run: |
echo "HEROKU_REGISTRY_IMAGE=${{ env.HEROKU_REGISTRY_IMAGE }}" >> $GITHUB_ENV
Expand Down
3 changes: 2 additions & 1 deletion project/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ RUN apt-get update \
# install python dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY ./requirements-dev.txt .
RUN pip install -r requirements-dev.txt

# add app
COPY . .
Expand Down
43 changes: 40 additions & 3 deletions project/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
###########
# BUILDER #
###########

# pull official base image
FROM python:3.12.1-slim-bookworm as builder

# install system dependencies
RUN apt-get update \
&& apt-get -y install netcat-traditional gcc postgresql \
&& apt-get clean

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt

# lint
COPY . /usr/src/app/
RUN pip install black==23.12.1 flake8==7.0.0 isort==5.13.2
RUN flake8 .
RUN black --exclude=migrations . --check
RUN isort . --check-only


#########
# FINAL #
#########

# pull official base image
FROM python:3.12.1-slim-bookworm

Expand Down Expand Up @@ -25,16 +61,17 @@ RUN apt-get update \
&& apt-get clean

# install python dependencies
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN pip install --no-cache /wheels/*
RUN pip install "uvicorn[standard]==0.26.0"

# add app
COPY . .

# chown all the files to the app user
RUN chown -R app:app $APP_HOME
RUN chown -R app:app $HOME

# change to the app user
USER app
Expand Down
2 changes: 1 addition & 1 deletion project/app/api/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@router.get("/ping")
async def pong(settings: Settings = Depends(get_settings)):
return {
"ping": "pong!!",
"ping": "pong!",
"environment": settings.environment,
"testing": settings.testing,
}
8 changes: 8 additions & 0 deletions project/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
black==23.12.1
flake8==7.0.0
isort==5.13.2
pytest==7.4.4
pytest-cov==4.1.0
pytest-xdist==3.5.0

-r requirements.txt
6 changes: 0 additions & 6 deletions project/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
aerich==0.7.2
asyncpg==0.29.0
black==23.12.1
fastapi==0.109.0
flake8==7.0.0
gunicorn==21.0.1
httpx==0.26.0
isort==5.13.2
newspaper3k==0.2.8
pydantic-settings==2.1.0
pytest==7.4.4
pytest-cov==4.1.0
pytest-xdist==3.5.0
tortoise-orm==0.20.0
uvicorn==0.26.0
2 changes: 1 addition & 1 deletion project/tests/test_ping.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_ping(test_app):
response = test_app.get("/ping")
assert response.status_code == 200
assert response.json() == {"environment": "dev", "ping": "pong!!", "testing": True}
assert response.json() == {"environment": "dev", "ping": "pong!", "testing": True}

0 comments on commit c6d116a

Please sign in to comment.