Skip to content

Commit 79df85a

Browse files
committed
docker caching
1 parent f0c4e77 commit 79df85a

4 files changed

Lines changed: 60 additions & 32 deletions

File tree

.github/workflows/main.yml

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Continuous Integration and Delivery
33
on: [push]
44

55
env:
6-
IMAGE: docker.pkg.github.com/$GITHUB_REPOSITORY/foo
6+
IMAGE: docker.pkg.github.com/$GITHUB_REPOSITORY/web
77

88
jobs:
99

@@ -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,37 +76,47 @@ 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 flake8 isort pytest
6582
- name: Pytest
66-
run: docker exec fastapi-tdd python -m pytest .
83+
run: docker exec fastapi-tdd pytest .
6784
- name: Flake8
68-
run: docker exec fastapi-tdd python -m flake8 .
85+
run: docker exec fastapi-tdd flake8 .
6986
- name: Black
70-
run: docker exec fastapi-tdd python -m black . --check
87+
run: docker exec fastapi-tdd black . --check
7188
- name: isort
72-
run: docker exec fastapi-tdd /bin/sh -c "python -m isort ./*/*.py --check-only"
89+
run: docker exec fastapi-tdd /bin/sh -c "isort ./*/*.py --check-only"
7390

7491
deploy:
7592
name: Deploy to Heroku
7693
runs-on: ubuntu-latest
7794
needs: [build, test]
7895
env:
7996
HEROKU_APP_NAME: salty-fortress-93778
80-
HEROKU_REGISTRY_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/foo
97+
HEROKU_REGISTRY_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/web
8198
steps:
8299
- name: Checkout master
83100
uses: actions/checkout@v1
84101
- name: Log in to GitHub Packages
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 ::set-env name=HEROKU_REGISTRY_IMAGE::${{ env.HEROKU_REGISTRY_IMAGE }}

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/requirements-dev.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
black==19.10b0
2+
flake8===3.8.2
3+
isort==4.3.21
4+
pytest==5.4.2
5+
pytest-cov==2.9.0
6+
pytest-xdist==1.32.0
7+
8+
-r requirements.txt

project/requirements.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
asyncpg==0.20.1
2-
black==19.10b0
32
fastapi==0.55.1
4-
flake8===3.8.2
53
gunicorn==20.0.4
6-
isort==4.3.21
7-
newspaper3k
8-
pytest==5.4.2
9-
pytest-cov==2.9.0
10-
pytest-xdist==1.32.0
4+
newspaper3k==0.2.8
115
requests==2.23.0
126
tortoise-orm==0.16.12
137
uvicorn==0.11.5

0 commit comments

Comments
 (0)