3

I have been tring to setup gitlab ci/cd config for a django project which will be deployed as a container.

This is what i have tried: CI/CD -

image: creatiwww/docker-compose:latest

services:
  - docker:dind



stages:
    - lint
    - build
    - deploy

variables:
  TAG_LATEST: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:latest
  TAG_COMMIT: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHORT_SHA

lint:
  stage: lint
  image: python:3.8
  before_script:
    - pip install pipenv
    - pipenv install --dev
  script:
    - pipenv run python -m flake8 --exclude=migrations,settings.py backend
  allow_failure: false

build:
  stage: build
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY_IMAGE
    - echo "IMAGE_APP_TAG=$TAG_LATEST" >> .env
    - docker-compose build
    - docker-compose push
  only:
    - master

deploy-to-prod:
  stage: deploy
  script:
    - eval $(ssh-agent -s)
    - echo "${ID_RSA}" | tr -d '\r' | ssh-add - > /dev/null
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY_IMAGE
    - echo "IMAGE_APP_TAG=$TAG_LATEST" >> .env
    - echo "SECRET_KEY=$SECRET_KEY" >> .env
    - docker-compose -H "ssh://$SERVER_USER@$SERVER_IP" down --remove-orphans
    - docker-compose -H "ssh://$SERVER_USER@$SERVER_IP" pull
    - docker-compose -H "ssh://$SERVER_USER@$SERVER_IP" up -d
  only:
    - master
  when: manual

The pipeline succeds but while checking the log of container i get following output-

python: can't open file 'manage.py': [Errno 2] No such file or directory

also my image field in docker ps is empty.

Please help

  • Hmm not sure but do you have to accept the host ssh fiungerprint the first time? If so you may obtain it in advance by `ssh-keyscan gitlab.com` and pass the fingerprint to the ssh command somehow – jlapoutre Nov 05 '20 at 08:28

1 Answers1

-2

Put the code in Docker_Compose.yml

version: '3.7'

services: backend: build: ./project_name command: sh -c "cd project && python3 manage.py migrate && python3 manage.py runserver 0.0.0.0:8000" ports: - 8000:8000 depends_on: - db network_mode: host

db: image: postgres:12.0-alpine network_mode: host ports: - 5432:5432 volumes: - postgres_data:/var/lib/postgresql/data/ environment: - POSTGRES_USER='db_user' - POSTGRES_PASSWORD='db_password' - POSTGRES_DB='db_name'

volumes: postgres_data:

  • I'm trying to figure out what @Akash was suggesting but it's impossible, could you please be a little more specific/extend your answer? – iba Jan 15 '21 at 15:02