How to automatically deploy Docker Image on your own server using GitLab?

I have been trying Google for several hours but cannot find it. I have a Java / Spring application (+ MySQL, if that matters), and I'm looking to create a CI for this.

I know what to do and how:

  • I know that I need to move my Git repository to Gitlab.
  • Push to repo will call a CI script.
  • Gitlab will build my docker image in the Gitlab Docker registry.

Question:

What do I need to do to get docker to compose my VPS in order to pull a new image from Gitlab and restart the server? I know (correct me if I'm wrong) that on my VPS I have to run docker-compose pull && docker-compose upinside the folder of my application, but I have no idea how to do this automatically using Gitlab?

+8
source share
1 answer

What do I need to do to get the docker collector on my VPS to retrieve a new image from Gitlab and reboot the server?

@ m-uu, you don’t have to restart the server at all, just follow docker-compose upto get a new image and restart the service.

I know (correct me if I'm wrong) that on my VPS I have to run docker-compose pull && docker-compose inside the application folder, but I literally have no idea how to do this automatically with Gitlab?

, . Gitlab CI, , Java. , , . , , SSH (.ssh/authorized_keys) GITLAB (https://docs.gitlab.com/ee/ci/variables/#secret-variables)

cache:
  key: "cache"
  paths:
  - junte-api

stages:
  - build
  - build_image
  - deploy

build:
  image: golang:1.7
  stage: build
  script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" > ~/key && chmod 600 ~/key
    - ssh-add ~/key
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    - go get -u github.com/kardianos/govendor
    - mkdir -p $GOPATH/src/github.com/junte/junte-api
    - mv * $GOPATH/src/github.com/junte/junte-api
    - cd $GOPATH/src/github.com/junte/junte-api
    - govendor sync
    - go build -o junte-api
    - cd -
    - cp $GOPATH/src/github.com/toscale/junte-api/junte-api .

build_image:
  image: docker:latest
  stage: build_image
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE

deploy-dev:
  stage: deploy
  image: junte/ssh-agent
  variables:
    # should be set up at Gitlab CI env vars
    SSH_PRIVATE_KEY: $SSH_DEV_PRIVATE_KEY
  script:
    # copy docker-compose yml to server
    - scp docker-compose.dev.yml root@SERVER_IP:/home/junte/junte-api/
    # login to gitlab registry       
    - ssh root@SERVER_IP docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    # then we cd to folder with docker-compose, run docker-compose pull to update images, and run services with 'docker-compose up -d'
    - ssh root@SERVER_IP "cd /home/junte/junte-api/ && docker-compose -f docker-compose.dev.yml pull api-dev && HOME=/home/dev docker-compose -f docker-compose.dev.yml up -d"
  environment:
      name: dev
  only:
    - dev

Gitlab Runner Docker. Gitlab doc, .

:

  • build - , ,
  • build_image - , gitlab, . cache, .
  • deploy-dev - , . 6 ssh , VPS. SSH_PRIVATE_KEY Gitlab. 3 SSH .
+8

Source: https://habr.com/ru/post/1679263/


All Articles