aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2020-11-17 14:34:20 +0100
committerGravatar Sebastiaan Zeeff <[email protected]>2020-11-17 14:34:20 +0100
commitf002d74ad312206f3118867e9421b401a94a97f4 (patch)
tree12053cfbd137beacef02fa9cab57ed8226da9ed9
parentBuild final image and push to GHCR (diff)
Use multi-stage docker file to use local cache
-rw-r--r--.github/workflows/lint-test.yaml58
-rw-r--r--docker/base.Dockerfile23
2 files changed, 48 insertions, 33 deletions
diff --git a/.github/workflows/lint-test.yaml b/.github/workflows/lint-test.yaml
index 7aa8f32..9dc711c 100644
--- a/.github/workflows/lint-test.yaml
+++ b/.github/workflows/lint-test.yaml
@@ -3,7 +3,7 @@ name: Build, Lint, Test
on:
push:
branches:
- - sebastiaan/backend/migrate-ci-to-github-actions
+ - sebastiaan/backend/cache-docker-images
jobs:
@@ -13,9 +13,17 @@ jobs:
DOCKER_BUILDKIT: 1
steps:
+ - name: Create SHA Container Tag
+ id: sha_tag
+ run: |
+ tag=$(cut -c 1-7 <<< $GITHUB_SHA)
+ echo "::set-output name=tag::$tag"
- name: Checkout code
uses: actions/checkout@v2
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v1
+
- name: Login to Github Container Registry
uses: docker/login-action@v1
with:
@@ -23,24 +31,24 @@ jobs:
username: ${{ github.repository_owner }}
password: ${{ secrets.GHCR_TOKEN }}
- - name: Build base image
- run: "docker build \
- -f docker/base.Dockerfile \
- -t ghcr.io/python-discord/snekbox-base:latest \
- --cache-from ghcr.io/python-discord/snekbox-base:latest \
- --build-arg BUILDKIT_INLINE_CACHE=1 \
- ."
-
- - name: Show Containers
- run: docker image ls
+ - name: Cache Base Image Layers
+ uses: actions/cache@v2
+ with:
+ path: /tmp/.base-buildx-cache
+ key: ${{ runner.os }}-buildx-base-${{ github.sha }}
+ restore-keys: |
+ ${{ runner.os }}-buildx-base-
- name: Build base image
- run: "docker build \
- -f docker/venv.Dockerfile \
- -t ghcr.io/python-discord/snekbox-venv:latest \
- --cache-from ghcr.io/python-discord/snekbox-venv:latest \
- --build-arg BUILDKIT_INLINE_CACHE=1 \
- ."
+ uses: docker/build-push-action@v2
+ with:
+ context: .
+ file: ./docker/base.Dockerfile
+ push: false
+ load: true
+ cache-from: type=local,src=/tmp/.base-buildx-cache
+ cache-to: type=local,type=local,dest=/tmp/.base-buildx-cache
+ tags: ghcr.io/python-discord/snekbox-base:${{ steps.sha_tag.outputs.tag }}
- name: Show Containers
run: docker image ls
@@ -91,19 +99,5 @@ jobs:
pip install coveralls~=2.1
coveralls
- - name: Build final image
- run: "docker build \
- -f docker/Dockerfile \
- -t ghcr.io/python-discord/snekbox:latest \
- --cache-from ghcr.io/python-discord/snekbox:latest \
- --build-arg BUILDKIT_INLINE_CACHE=1 \
- ."
-
- name: Push base image
- run: docker push ghcr.io/python-discord/snekbox-base:latest
-
- - name: Push venv image
- run: docker push ghcr.io/python-discord/snekbox-venv:latest
-
- - name: Push final image
- run: docker push ghcr.io/python-discord/snekbox:latest
+ run: docker push ghcr.io/python-discord/snekbox-base:${{ steps.sha_tag.outputs.tag }}
diff --git a/docker/base.Dockerfile b/docker/base.Dockerfile
index a4a5ad1..1655aa1 100644
--- a/docker/base.Dockerfile
+++ b/docker/base.Dockerfile
@@ -19,7 +19,7 @@ RUN git clone \
WORKDIR /nsjail
RUN make
-FROM python:3.9-slim-buster
+FROM python:3.9-slim-buster as venv
ENV PIP_NO_CACHE_DIR=false
RUN apt-get -y update \
@@ -32,3 +32,24 @@ RUN pip install pipenv==2020.11.4
COPY --from=builder /nsjail/nsjail /usr/sbin/
RUN chmod +x /usr/sbin/nsjail
+
+FROM venv
+
+ARG DEV
+ENV PIP_NO_CACHE_DIR=false \
+ PIPENV_DONT_USE_PYENV=1 \
+ PIPENV_HIDE_EMOJIS=1 \
+ PIPENV_NOSPIN=1
+
+COPY Pipfile Pipfile.lock /snekbox/
+WORKDIR /snekbox
+
+RUN if [ -n "${DEV}" ]; \
+ then \
+ pipenv install --deploy --system --dev; \
+ else \
+ pipenv install --deploy --system; \
+ fi
+
+# At the end to avoid re-installing dependencies when only a config changes.
+COPY config/ /snekbox/config