Skip to content

Instantly share code, notes, and snippets.

@Mastermind-U
Last active July 3, 2022 17:45
Show Gist options
  • Save Mastermind-U/01e16a4e87748b068c3c036b4dd634b0 to your computer and use it in GitHub Desktop.
Save Mastermind-U/01e16a4e87748b068c3c036b4dd634b0 to your computer and use it in GitHub Desktop.
Django gitlab.ci
image: docker:latest
services:
- docker:dind
stages:
- build
- test
- deploy
before_script:
- apk add --update --no-cache py-pip python-dev build-base python
- pip install docker-compose==1.9.0
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
build:
stage: build
script:
- docker-compose build
- docker-compose push
test:
stage: test
script:
- docker-compose pull test
- docker-compose run test
deploy:
stage: deploy
only:
- master
before_script:
- apk add --no-cache bash
- mkdir -p ~/.ssh
- ssh-keyscan -t rsa gitlab.com >> ~/.ssh/known_hosts
- eval $(ssh-agent -s)
- bash -c 'ssh-add <(echo "$DEPLOY_KEY")'
- echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
script:
- ssh $DEPLOY_USER@$DEPLOY_ADDR "mkdir -p ~/project/config/ || true"
- scp docker-compose.yml $DEPLOY_USER@$DEPLOY_ADDR:~/project/docker-compose.yml
# - scp config/local.conf $DEPLOY_USER@$DEPLOY_ADDR:~/project/config/local.conf
- ssh $DEPLOY_USER@$DEPLOY_ADDR docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- ssh $DEPLOY_USER@$DEPLOY_ADDR "docker rm --force django || true && docker rm --force test || true"
- ssh $DEPLOY_USER@$DEPLOY_ADDR "cd ~/project/ && docker-compose -f docker-compose.yml pull && docker-compose up -d"
- ssh $DEPLOY_USER@$DEPLOY_ADDR "docker network create nginx || true && docker network connect nginx project || true && docker network connect nginx django || true"
- ssh $DEPLOY_USER@$DEPLOY_ADDR "docker rmi \$(docker images -f dangling=true -q)"
version: '2.0'
services:
project:
image: registry.gitlab.com/project:latest
container_name: django
build: .
environment:
- SECRET_KEY
command: gunicorn project.wsgi:application --bind 0.0.0.0:8000 -w 3
expose:
- "8000"
restart: unless-stopped
test:
image: registry.gitlab.com/project:latest
container_name: test
command: python manage.py test --noinput
restart: "no"
# nginx:
# image: nginx:mainline
# container_name: nginx
# ports:
# - "80:80"
# - "443:443"
# volumes:
# - ./config:/etc/nginx/conf.d
# - static/:/usr/src/app/static/
# networks:
# - front_nginx
# depends_on:
# - project
# restart: unless-stopped
FROM python:3.8-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN apk update --no-cache && apk add --update --no-cache libressl-dev libevent-dev gcc musl-dev libc-dev libffi-dev postgresql-dev jpeg-dev zlib-dev libzmq zeromq-dev build-base
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
COPY . /usr/src/app
RUN SECRET_KEY=build python manage.py collectstatic --noinput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment