如何使用 GitLab runner 启动服务并防止它停止? [英] How to start a service using GitLab runner and prevent it from stopping?

查看:158
本文介绍了如何使用 GitLab runner 启动服务并防止它停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用 GitLab CI 服务器部署简单的 Spring Boot 应用程序.我的 .gitlab-ci.yml 如下:

I'm going to deploy simple Spring Boot application using GitLab CI server. My .gitlab-ci.yml is as follows:

stages:
  - build_and_test
  - deploy

web_server_build_and_test:
  stage: build_and_test
  script:
    - mvn clean package

web_server_deploy:
  stage: deploy
  script:
    - mvn clean package -Pprod
    - service gitlab-runner-test stop
    - cp target/*.war /var/gitlab-runner-test/gitlab-runner-test.war
    - chmod +x /var/gitlab-runner-test/gitlab-runner-test.war
    - service gitlab-runner-test start

deploy 阶段产生以下输出:

$ service gitlab-runner-test stop
Stopped [13247]
$ cp target/*.war /var/gitlab-runner-test/gitlab-runner-test.war
$ chmod +x /var/gitlab-runner-test/gitlab-runner-test.war
$ service gitlab-runner-test start
Started [21177]

但是,我无法加载应用程序,因为一旦 runner 完成阶段,服务就停止了:

However, I'm unable to load the application since the service has been stopped once runner finishes the stage:

$ service gitlab-runner-test status
Not running (process 21177 not found)

我的服务脚本将实际工作委托给组装好的 war 包:

My service scripts delegates actual work to the assembled war package:

#!/usr/bin/env bash

export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre/bin/java
export MODE=service
export APP_NAME=gitlab-runner-test
export PID_FOLDER=/var/run/gitlab-runner-test


/var/gitlab-runner-test/gitlab-runner-test.war $*

此外,当我手动启动服务时(service gitlab-runner-test start),即使在用户会话关闭后它仍然在运行.

Moreover, when I'm starting service by hands (service gitlab-runner-test start) it remains running even after user session has been closed.

我不确定,问题的根源是什么 - Spring Boot 启动脚本、GitLab 配置、我的服务脚本或其他什么?

I'm not sure, what is the root of the problem - Spring Boot startup script, GitLab configuration, my service scripts or something else?

我正在运行带有 GitLab CI 多运行器版本 0.5.0 (c38415a) 的 Ubuntu 14.04.

I'm running Ubuntu 14.04 with GitLab CI multi runner version 0.5.0 (c38415a).

UPD:

将运行器升级到版本 1.0.1 (cffb5c7) 并不能解决问题.

Upgrading runner to version 1.0.1 (cffb5c7) does not solve the problem.

推荐答案

为什么这样做是个坏主意...

正如其文档明确指出的那样,GitLab Runner 运行 测试并将结果发送到 GitLab".

Why it's a bad idea to do it...

As its documentation clearly states, the GitLab Runner "runs tests and sends the results to GitLab".

由于测试应该及时启动和停止,运行程序旨在在完成每个构建后终止所有创建的进程.

And as tests should start and stop in a timely manner, the runner is designed to kill all created processes after finishing each build.

所以你的服务被杀死不是一个错误,它是一个功能.;)

So it's not a bug that your service is killed, it's a feature. ;)


GitLab CI 文档 推荐使用 dpl 用于 部署.

GitLab CI documentation recommends using dpl for deployment.

dpl 是一个项目,可让您在各种 PaaS 提供商处部署您的应用,例如 Google App Engine、Heroku 或 Elastic Beanstalk.

dpl is a project that enables you to deploy your app at various PaaS providers, such a Google App Engine, Heroku or Elastic Beanstalk.

因此它会触发一些对某些 REST API 的请求或通过互联网推送一些其他数据,并且它的进程很好地退出.

So it fires some requests to some REST APIs or pushes some other data through the internets and its process nicely exits.


所以做你想做的事实际上需要一些黑客行为——覆盖默认的跑步者行为.而且您不应该将其作为长期解决方案,因为它可能会因某些 runner/gitlab 更新而停止工作.

So doing what you want to do actually requires some hacking - overriding the default runner behaviour. And you should not do it as a long-term solution because it may stop working with some runner/gitlab update.

在您的情况下,当您想在跑步者的主机上实际部署和运行应用程序时,我们需要使用两个技巧:

In your case, when you want to actually deploy and run the app on your runner's host itself, we need to use two hacks:

  • don't use the default shell runner executor but ssh and make the executor ssh to itself (inspired by michael's solution to this question (please upvote his answer too if upvoting mine!),
  • disown the process that is being run by the init script (Joe's solution to this question (again - please upvote it!)

好的,下面是说明:

  1. 确保您可以使用 /root/.ssh/id_rsa 中的 SSH 私钥从运行器主机 SSH 到自身,无需密码,无需确认指纹.由 root 运行的 ssh localhost 应该以非交互方式工作.

  1. Make sure you can SSH from your runner host to itself with a SSH private key in /root/.ssh/id_rsa, without passphrase, without confirming the fingerprint. ssh localhost run by root should work non-interactively.

编辑你的 gitlab-runner 的配置文件,/etc/gitlab-runner/config.toml 使它看起来像这样:

Edit your gitlab-runner's config file, /etc/gitlab-runner/config.toml to make it look like this:

[[runners]]
  name = "your-runner-name"
  url = "https://<your_gitlab_instance_fqdn>/ci"
  token = "<your_project_CI_token>"
  tls-ca-file = ""
  executor = "ssh"
  [runners.ssh]
    user = "root"
    password = ""
    host = "localhost"
    port = "22"
    identity_file = "/root/.ssh/id_rsa"

(保存配置文件后运行器会重新加载)

(The runner will reload itself after saving the config file)

  1. 编辑您的服务脚本,使其创建的进程不会是 init 脚本的子进程,并且不会打开 stdin、stdout 和 stderr:

  1. Edit your service script so the process it will create will NOT be a child of the init script and it won't open stdin, stdout and stderr:

#!/usr/bin/env bash

export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre/bin/java
export MODE=service
export APP_NAME=gitlab-runner-test
export PID_FOLDER=/var/run/gitlab-runner-test


/var/gitlab-runner-test/gitlab-runner-test.war $* <&- >&- 2>&- & disown

通过重试上次构建或提交到您的项目 repo 进行测试.

Test by retrying last build or making a commit to your project repo.

PS 我使用如下所示的初始化脚本测试了我的解决方案:

PS I tested my solution with an init script looking like this:

#!/usr/bin/env bash

start() {
  # Completely disowned process, not a child
  # Credits: Joe at https://stackoverflow.com/a/26420299/2693875
  sleep 99999 <&- >&- 2>&- & disown
  exit 0
}

stop() {
  echo "doing nothing"
  exit 0
}

echo "running on $HOSTNAME..."

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  *)
    echo $"Use this options $0 {start|stop}"
    exit 1
esac

..在带有 gitlab-multi-runner v. 1.02 和 GitLab CE 8.5.0 的 Ubuntu 14.04 上.

..on Ubuntu 14.04 with gitlab-multi-runner v. 1.02 and GitLab CE 8.5.0.

这篇关于如何使用 GitLab runner 启动服务并防止它停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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