28

I am trying to Google it for few hours, but can't find it. I have Java/Spring application (+MySQL if it matters) and I am looking to create CI for that.

I know what to do and how:

  1. I know that I have to move my Git repo to Gitlab.
  2. Push to repo will trigger CI script.
  3. Gitlab will build my docker image into Gitlab Docker Registry.

Question is:

What do I have to do to force docker compose on my VPS to pull the new image from Gitlab and restart the server? I know (correct me if I am wrong) that on my VPS I should run docker-compose pull && docker-compose up inside my app folder, but I have literally no idea how to make it automatically with Gitlab?

1 Answer 1

31

What do I have to do to force docker compose on my VPS to pull the new image from Gitlab and restart the server?

@m-uu, you don't need restart the server at all, just do docker-compose up to pull new image and restart service

I know (correct me if I am wrong) that on my VPS I should run docker-compose pull && docker-compose up inside my app folder, but I have literally no idea how to make it automatically with Gitlab?

Yes, you are on the right way. Look at my Gitlab CI configuration file, I think it doesn't difficult to change it for Java project. Just give you ideas how to build, push to your registry and deploy an image to your server. One thing you need to do is generate SSH keys and push public to server (.ssh/authorized_keys) and private to GITLAB pipeline secret variable (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/junte/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 

You also need Gitlab runner with Docker support. How install it look at in Gitlab doc, please.

About stages:

  • build - just change it to build what you need
  • build_image - very simple, just login to gitlab registry, build new image and push it to registry. Look at cache part, it need to cache files between stages and can be different for you.
  • deploy-dev - that part more about what you asked. Here first 6 commands just install ssh and create your private key file to have access to your VPS. Just copy it and add your SSH_PRIVATE_KEY to secret vars in Gitlab UI. Last 3 SSH commands more interesting for you.
Sign up to request clarification or add additional context in comments.

10 Comments

1. Where you configure SSH_PRIVATE_KEY, IP etc for your VPS?
@MU on gitlab <Project> - Settings - CI/CD Pipelines and Secret Variables part
Thanks. Could you elaborate more what is going on in the deploy script? I can try to understand parts, but would prefer to know what happens in each line.
@MU well, I've added more info to post and also commented most interesting lines in my configuration file. It should be enough to run your own setup. Don't hesitate read Gitlab docs for more info)))
@MU generate ssh keys without password thegeekstuff.com/2008/11/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.