Skip to content

Commit e6c5460

Browse files
committed
testing ci/cd
1 parent f8e4bf4 commit e6c5460

7 files changed

Lines changed: 95 additions & 30 deletions

File tree

.github/workflows/main.yml

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@ jobs:
1717
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
1818
env:
1919
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20-
- name: Pull image
20+
- name: Pull images
2121
run: |
22-
docker pull ${{ env.IMAGE }}:latest || true
23-
- name: Build image
22+
docker pull ${{ env.IMAGE }}-builder:latest || true
23+
docker pull ${{ env.IMAGE }}-final:latest || true
24+
- name: Build images
2425
run: |
2526
docker build \
26-
--cache-from ${{ env.IMAGE }}:latest \
27-
--tag ${{ env.IMAGE }}:latest \
27+
--target builder \
28+
--cache-from ${{ env.IMAGE }}-builder:latest \
29+
--tag ${{ env.IMAGE }}-builder:latest \
30+
--file ./project/Dockerfile.prod \
31+
"./project"
32+
docker build \
33+
--cache-from ${{ env.IMAGE }}-final:latest \
34+
--tag ${{ env.IMAGE }}-final:latest \
2835
--file ./project/Dockerfile.prod \
2936
"./project"
30-
- name: Push image
37+
- name: Push images
3138
run: |
32-
docker push ${{ env.IMAGE }}:latest
39+
docker push ${{ env.IMAGE }}-builder:latest
40+
docker push ${{ env.IMAGE }}-final:latest
3341
3442
test:
3543
name: Test Docker Image
@@ -42,14 +50,21 @@ jobs:
4250
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
4351
env:
4452
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45-
- name: Pull image
53+
- name: Pull images
4654
run: |
47-
docker pull ${{ env.IMAGE }}:latest || true
48-
- name: Build image
55+
docker pull ${{ env.IMAGE }}-builder:latest || true
56+
docker pull ${{ env.IMAGE }}-final:latest || true
57+
- name: Build images
4958
run: |
5059
docker build \
51-
--cache-from ${{ env.IMAGE }}:latest \
52-
--tag ${{ env.IMAGE }}:latest \
60+
--target builder \
61+
--cache-from ${{ env.IMAGE }}-builder:latest \
62+
--tag ${{ env.IMAGE }}-builder:latest \
63+
--file ./project/Dockerfile.prod \
64+
"./project"
65+
docker build \
66+
--cache-from ${{ env.IMAGE }}-final:latest \
67+
--tag ${{ env.IMAGE }}-final:latest \
5368
--file ./project/Dockerfile.prod \
5469
"./project"
5570
- name: Run container
@@ -61,7 +76,9 @@ jobs:
6176
-e ENVIRONMENT=dev \
6277
-e DATABASE_TEST_URL=sqlite://sqlite.db \
6378
-p 5003:8765 \
64-
${{ env.IMAGE }}:latest
79+
${{ env.IMAGE }}-final:latest
80+
- name: Install requirements
81+
run: docker exec fastapi-tdd pip install black==21.6b0 flake8==3.9.2 isort==5.9.1 pytest==6.2.4
6582
- name: Pytest
6683
run: docker exec fastapi-tdd python -m pytest .
6784
- name: Flake8
@@ -85,13 +102,21 @@ jobs:
85102
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
86103
env:
87104
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
88-
- name: Pull image
105+
- name: Pull images
89106
run: |
90-
docker pull ${{ env.IMAGE }}:latest || true
91-
- name: Build image
107+
docker pull ${{ env.IMAGE }}-builder:latest || true
108+
docker pull ${{ env.IMAGE }}-final:latest || true
109+
- name: Build images
92110
run: |
93111
docker build \
94-
--cache-from ${{ env.IMAGE }}:latest \
112+
--target builder \
113+
--cache-from ${{ env.IMAGE }}-builder:latest \
114+
--tag ${{ env.IMAGE }}-builder:latest \
115+
--file ./project/Dockerfile.prod \
116+
"./project"
117+
docker build \
118+
--cache-from ${{ env.IMAGE }}-final:latest \
119+
--tag ${{ env.IMAGE }}:latest \
95120
--tag ${{ env.HEROKU_REGISTRY_IMAGE }}:latest \
96121
--file ./project/Dockerfile.prod \
97122
"./project"
@@ -100,7 +125,7 @@ jobs:
100125
env:
101126
HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }}
102127
- name: Push to the registry
103-
run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }}
128+
run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }}:latest
104129
- name: Set environment variables
105130
run: |
106131
echo "HEROKU_REGISTRY_IMAGE=${{ env.HEROKU_REGISTRY_IMAGE }}" >> $GITHUB_ENV

project/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ RUN apt-get update \
1616
# install python dependencies
1717
RUN pip install --upgrade pip
1818
COPY ./requirements.txt .
19-
RUN pip install -r requirements.txt
19+
COPY ./requirements-dev.txt .
20+
RUN pip install -r requirements-dev.txt
2021

2122
# add app
2223
COPY . .

project/Dockerfile.prod

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
###########
2+
# BUILDER #
3+
###########
4+
5+
# pull official base image
6+
FROM python:3.8.11-slim-buster as builder
7+
8+
# install system dependencies
9+
RUN apt-get update \
10+
&& apt-get -y install gcc postgresql \
11+
&& apt-get clean
12+
13+
# set work directory
14+
WORKDIR /usr/src/app
15+
16+
# set environment variables
17+
ENV PYTHONDONTWRITEBYTECODE 1
18+
ENV PYTHONUNBUFFERED 1
19+
20+
# install dependencies
21+
RUN pip install --upgrade pip
22+
COPY ./requirements.txt .
23+
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
24+
25+
# lint
26+
COPY . /usr/src/app/
27+
RUN pip install black==21.6b0 flake8==3.9.2 isort==5.9.1
28+
RUN flake8 .
29+
RUN black --exclude=migrations .
30+
RUN isort .
31+
32+
33+
#########
34+
# FINAL #
35+
#########
36+
137
# pull official base image
238
FROM python:3.8.11-slim-buster
339

@@ -25,16 +61,17 @@ RUN apt-get update \
2561
&& apt-get clean
2662

2763
# install python dependencies
64+
COPY --from=builder /usr/src/app/wheels /wheels
65+
COPY --from=builder /usr/src/app/requirements.txt .
2866
RUN pip install --upgrade pip
29-
COPY ./requirements.txt .
30-
RUN pip install -r requirements.txt
67+
RUN pip install --no-cache /wheels/*
3168
RUN pip install "uvicorn[standard]==0.14.0"
3269

3370
# add app
3471
COPY . .
3572

3673
# chown all the files to the app user
37-
RUN chown -R app:app $APP_HOME
74+
RUN chown -R app:app $HOME
3875

3976
# change to the app user
4077
USER app

project/app/api/ping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@router.get("/ping")
1212
async def pong(settings: Settings = Depends(get_settings)):
1313
return {
14-
"ping": "pong!",
14+
"ping": "pong",
1515
"environment": settings.environment,
1616
"testing": settings.testing,
1717
}

project/requirements-dev.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
black==21.6b0
2+
flake8==3.9.2
3+
isort==5.9.1
4+
pytest==6.2.4
5+
pytest-cov==2.12.1
6+
pytest-xdist==2.3.0
7+
8+
-r requirements.txt

project/requirements.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
aerich==0.5.3
22
asyncpg==0.23.0
3-
black==21.6b0
43
fastapi==0.65.3
5-
flake8==3.9.2
64
gunicorn==20.1.0
7-
isort==5.9.1
85
newspaper3k==0.2.8
9-
pytest==6.2.4
10-
pytest-cov==2.12.1
11-
pytest-xdist==2.3.0
126
requests==2.25.1
137
tortoise-orm==0.17.4
148
uvicorn==0.14.0

project/tests/test_ping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
def test_ping(test_app):
55
response = test_app.get("/ping")
66
assert response.status_code == 200
7-
assert response.json() == {"environment": "dev", "ping": "pong!", "testing": True}
7+
assert response.json() == {"environment": "dev", "ping": "pong", "testing": True}

0 commit comments

Comments
 (0)