Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: docker deployment #801

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 126 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
version: 2.1

jobs:
build:
working_directory: ~/memegen

test:
docker:
- image: cimg/python:3.11.2

working_directory: ~/memegen

steps:
- checkout
Expand Down Expand Up @@ -47,3 +47,126 @@ jobs:
- run:
name: Building site
command: make site
docker-build:
machine:
image: ubuntu-2204:2023.02.1

steps:
- checkout

- run:
name: Make cache directory
command: mkdir -p /tmp/cache

- restore_cache:
keys:
- v2-{{ .Branch }}-{{ arch }}-{{ checksum "Containerfile" }}

- run:
name: Loading Docker image
command: |
if [ -f /tmp/cache/memegen.tar ]; then
echo "Loading Docker image from cache"
else
echo "No Docker image found in cache"
exit 0
fi
docker load < /tmp/cache/memegen.tar

- run:
name: Building Docker image
command: |
# Build the image with the tag "latest" and the tag of the current git tag (if any)
TAGS=$(for tag in latest ${CIRCLE_TAG}; do echo "-t memegen:$tag"; done)
echo "Building Docker image with tags: $TAGS"
docker build ${TAGS} -f Containerfile .

- run:
name: Running Docker image
command: |
docker run --name memegen -p 5000:5000 --rm memegen || :
background: true

- run:
name: Validate API is running
command: |
set +e
echo "Waiting for API to start..."
MAX_TRIES=15
for i in $(seq 1 $MAX_TRIES); do
sleep 5
http_code=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:5000/images/preview.jpg" 2>/dev/null)
if [ "$http_code" = "200" ]; then
exit 0
fi
done
echo "Failed to validate API is running after $MAX_TRIES tries"
exit 1

- run:
name: Stopping Docker image
command: docker exec memegen bash -c "kill -9 1"

- run:
name: Saving Docker image
command: docker save memegen > /tmp/cache/memegen.tar

- save_cache:
key: v2-{{ .Branch }}-{{ arch }}-{{ checksum "Containerfile" }}
paths:
- /tmp/cache/memegen.tar

- persist_to_workspace:
root: /tmp/cache
paths:
- memegen.tar

docker-deploy:
docker:
- image: cimg/base:current

steps:
- checkout

- setup_remote_docker

- attach_workspace:
at: .

- run:
name: Loading Docker image
command: |
if [ -f ./memegen.tar ]; then
echo "Loading Docker image from cache"
else
echo "No Docker image found in cache"
exit 0
fi
docker load < ./memegen.tar

- run:
name: Pushing Docker image
command: |
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
docker push memegen

workflows:
default:
jobs:
- test:
filters:
tags:
only: /.*/
- docker-build:
filters:
tags:
only: /.*/
- docker-deploy:
requires:
Copy link
Author

@KyleTryon KyleTryon Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a https://circleci.com/docs/contexts/ context with the Dockerhub login and password.

Required:

  • DOCKER_PASSWORD
  • DOCKER_USERNAME
Suggested change
requires:
context: [docker-deploy]
requires:

In this configuration, this job will not run on pull requests, as contexts will only run when triggered by org users for security.

If you would like, I could split the docker build/test job and docker deploy job. In that situation, we will build and test the docker image on every commit and only deploy on tag. This is more correct but will use a bit more credits.

- test
- docker-build
filters:
tags:
only: /v.*/
branches:
ignore: /.*/
2 changes: 1 addition & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG ARG_MAX_REQUESTS_JITTER=0
FROM docker.io/python:3.11-bullseye as build

# Install webp dependencies
RUN apt update && apt install -y webp cmake
RUN apt-get update && apt-get install -y webp cmake

# Create the memegen user
RUN useradd -md /opt/memegen -u 1000 memegen
Expand Down