Search code examples
dockergitlabcicd

GitLab CI/CD pipeline not changing the code on remote server


I made a change to the code on GitLab repo and then, after the merge pipeline is finishing successfully, but with no change to the code in the server. How can I fix that?

My gitlab-ci.yml:

stages:
  - publish
  - deploy

build_and_push:
  image: docker:stable
  stage: publish
  script:
    - docker system prune -a -f
    - docker build -t $IMAGE_NAME .
    - docker login $REGISTRY -u $REGISTRY_USER -p $REGISTRY_TOKEN
    - docker push $IMAGE_NAME
  only:
    - main

deploy:
  image: ubuntu
  stage: deploy
  script:
    - chmod og= $SSH_KEY
    - apt-get update && apt-get install openssh-client -y
    - ssh -i $SSH_KEY -o StrictHostKeyChecking=no $USER@$HOST docker login $REGISTRY -u $REGISTRY_USER -p $REGISTRY_TOKEN
    - ssh -i $SSH_KEY -o StrictHostKeyChecking=no $USER@$HOST docker-compose pull
    - ssh -i $SSH_KEY -o StrictHostKeyChecking=no $USER@$HOST docker-compose down
    - ssh -i $SSH_KEY -o StrictHostKeyChecking=no $USER@$HOST docker-compose up -d

  environment:
    name: production
    url: http://192.168.0.130
  only:
    - main

My compose file:

version: '3'
  volumes:
   app:
   pg_data:

  services:
   web-app:
     env_file: .env
     image: ${IMAGE_NAME}
     ports:
       - "8000:8080"
     volumes:
       - app:/app
     command: >
       sh -c "python manage.py runserver 0:8080"
     depends_on:
       - pgdb

    pgdb:
     image: postgres:13.10
     env_file: .env
     volumes:
       - pg_data:/var/lib/postgresql/data

Solution

  • The culprit is your volume configuration:

      volumes:
       app:
         ...
         volumes:
           - app:/app
    

    What you are doing is creating a named volume app and mounting it to the container. I assume you also have in your Dockerfile something like this:

    WORKDIR app
    COPY . .
    

    Or along those lines. So in other words, you build an image that contains a directory called app. But in your compose file you overwrite that directory with whatever is in the named volume app in your host machine. And the content of that volume is whatever was available in the first version of the image.

    Remove this from your docker-compose.yml

     volumes:
       - app:/app
    

    and you should be OK.