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

查看:28
本文介绍了如何使用 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. Push to repo 将触发 CI 脚本.
  3. Gitlab 会将我的 docker 镜像构建到 Gitlab Docker Registry 中.

问题是:

我必须做些什么来强制我的 VPS 上的 docker compose 从 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?

推荐答案

我必须做些什么来强制我的 VPS 上的 docker compose 从 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 在我的应用程序文件夹中,但我真的不知道如何使用 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 管道秘密变量(https://docs.gitlab.com/ee/ci/variables/#secret-variables)

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 *
	StrictHostKeyChecking no

" > ~/.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天全站免登陆