如何使用GitLab在自己的服务器上自动部署Docker Image? [英] How to auto deploy Docker Image on own server with GitLab?

查看:519
本文介绍了如何使用GitLab在自己的服务器上自动部署Docker Image?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Google几个小时,但找不到它. 我有Java/Spring应用程序(如果需要的话,可以使用MySQL),并且正在为此创建CI.

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.

我知道该怎么做以及如何做:

  1. 我知道我必须将Git存储库移至Gitlab.
  2. 按回购将触发CI脚本.
  3. Gitlab将我的docker映像构建到Gitlab Docker Registry中.

问题是:

该如何强制Docker在我的VPS上撰写以从Gitlab提取新映像并重新启动服务器? 我知道(如果我错了,请纠正我)在我的VPS上应该在我的应用文件夹中运行docker-compose pull && docker-compose up,但是我真的不知道如何使用Gitlab自动创建它?

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?

推荐答案

我该如何强制Docker在我的VPS上撰写以从Gitlab提取新映像并重新启动服务器?

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,您根本不需要重新启动服务器,只需执行docker-compose up即可提取新映像并重新启动服务

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

我知道(如果我错了,请纠正我)在我的VPS上,我应该运行docker-compose pull&& docker-compose up在我的app文件夹中,但是我真的不知道如何使用Gitlab自动创建它?

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?

是的,您的方法正确.查看我的Gitlab CI配置文件,我认为为Java项目更改它并不难.只是给您一些想法,例如如何构建,推送到注册表以及将映像部署到服务器.您需要做的一件事是生成SSH密钥,并将公共密钥推送到服务器(.ssh/authorized_keys),将私有密钥推送到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

您还需要具有Docker支持的Gitlab运行程序.请在Gitlab文档中查看安装方式.

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

关于阶段:

  • build-只需对其进行更改即可构建所需的内容
  • build_image-非常简单,只需登录gitlab注册表,构建新映像并将其推送到注册表.查看cache部分,它需要在阶段之间缓存文件,并且可能与您有所不同.
  • deploy-dev-有关您所要求内容的那部分.在这里,前6个命令只需安装ssh并创建您的私钥文件即可访问您的VPS.只需复制它,然后将您的SSH_PRIVATE_KEY添加到Gitlab UI中的秘密变量中即可.最后3个SSH命令对您来说更有趣.
  • 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.

这篇关于如何使用GitLab在自己的服务器上自动部署Docker Image?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆