「GitLab CI/CD」- 在远程主机中执行命令

  CREATED BY JENKINSBOT

问题描述

我们使用 GitLab CI/CD 实践自动化,有时我们需要在远程主机执行部署命令。

该笔记将记录:在 GitLab CI/CD 中,如何在远程主机中执行命令。

解决方案

使用 GitLab Runner / SSH Executor 功能。

第一步、注册 SSH Execurtor 执行器

# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=42 revision=ac8e767a version=12.6.0
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://gitlab.exmaple.com ------------------------------------------------------ <输入 GitLab 地址>
Please enter the gitlab-ci token for this runner:
xxxxxxxxxx --------------------------------------------------------------------- <在 Admin Area / Overview Runners 中查看>
Please enter the gitlab-ci description for this runner:
[eec8077625c3]: ssh executor --------------------------------------------------- <添加描述信息>
Please enter the gitlab-ci tags for this runner (comma separated):
staging-host ------------------------------------------------------------------- <添加 Tag 信息,我们通常设置为主机名>
Registering runner... succeeded                     runner=zxTzZ7Ec
Please enter the executor: virtualbox, docker+machine, kubernetes, docker, parallels, shell, docker-ssh+machine, custom, docker-ssh, ssh:
ssh ---------------------------------------------------------------------------- <选择 Executor 类型,针对我们的问题,应该填写 ssh >
Please enter the SSH server address (e.g. my.server.com):
staging-host.example.com ------------------------------------------------------- <设置 ssh 地址>
Please enter the SSH server port (e.g. 22):
22 ----------------------------------------------------------------------------- <端口号>
Please enter the SSH user (e.g. root):
example ------------------------------------------------------------------------ <用户>
Please enter the SSH password (e.g. docker.io):
xxxxxxxxxxxxxx ----------------------------------------------------------------- <密码>
Please enter path to SSH identity file (e.g. /home/user/.ssh/id_rsa):
/path/to/id_rsa ---------------------------------------------------------------- <私钥地址>
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

第二步、在 .gitlab-ci.yml 中,使用该执行器

在 .gitlab-ci.yaml 中,需要指定使用该执行器,这是通过 Tags 实现的:

stages:
  - deploy
run:
  tags:
    # 这里的 tag 应与注册 SSH Execurotr 时填写的 Tag 相同
    # 管理员也可以在后台添加其他 Tag 说明:Admin Area / Overview Runners / <runner> / Tags
    - staging-host

常见问题汇总

在远程执行时,当前工作目录在哪里?

Advanced configuration/The [runners.custom_build_dir] section | GitLab
docs/executors/ssh.md · master · GitLab.org / gitlab-runner · GitLab
docs/configuration/advanced-configuration.md · master · GitLab.org / gitlab-runner · GitLab

源码,将被检出到:~/builds/<short-token>/<concurrent-id>/<namespace>/<project-name>

但是,可以通过设置 builds_dir 进行修改(只能修改 ~/builds 部分):

[[runners]]
...
  builds_dir = ""
...

除此之外,通过启用 config.toml / [runners.custom_build_dir] / enabled = trueconifg.toml / [[runners]] / builds_dir="/path/to/folder".gitlab-ci.yml / variables: / GIT_CLONE_PATH: $CI_BUILDS_DIR/project-name 可以直接克隆到指定目录。(但是,GIT_CLONE_PATH 必须是 $CI_BUILD_DIR 的子目录(即 builds_dir 的子目录))。详细内容参考 Advanced configuration / The [runners.custom_build_dir] sectionConfiguring runners in GitLab / Custom build directories 文档;

参考文献

SSH | GitLab
Advanced configuration / The [runners.custom_build_dir] section
Configuring runners in GitLab / Custom build directories