DevOps GitLab CICD 实践2——Runner 部署

本文详细介绍如何通过Docker部署GitLab Runner,并提供详细的配置步骤和命令实例。从安装Docker到配置及运行GitLab Runner容器,帮助读者顺利完成CI/CD流水线搭建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前置步骤:DevOps GitLab CICD 实践1——GitLab 部署

官方文档

文档地址

虽然有官方指引,但个人感觉指引的不够清晰,导致初次配置可能频繁失败

Run GitLab Runner in a container

This is how you can run GitLab Runner inside a Docker container.

General GitLab Runner Docker image usage

GitLab Runner Docker images (based on Ubuntu or Alpine Linux) are designed as wrappers around the standard gitlab-runner command, like if GitLab Runner was installed directly on the host.

The general rule is that every GitLab Runner command that normally would be executed as:

gitlab-runner [Runner command and options...]
复制代码

can be executed with:

docker run [chosen docker options...] gitlab/gitlab-runner [Runner command and options...]
复制代码

For example, getting the top-level help information for GitLab Runner command could be executed as:

docker run --rm -t -i gitlab/gitlab-runner --help

NAME:
   gitlab-runner - a GitLab Runner

USAGE:
   gitlab-runner [global options] command [command options] [arguments...]

VERSION:
   10.7.0 (7c273476)

(...)
复制代码

In short, the gitlab-runner part of the command is replaced with docker run [docker options] gitlab/gitlab-runner, while the rest of Runner’s command stays as it is described in the register documentation. The only difference is that the gitlab-runner command is executed inside of a Docker container.

Docker image installation and configuration

  1. Install Docker first:

     curl -sSL https://get.docker.com/ | sh
    复制代码
  2. You need to mount a config volume into the gitlab-runner container to be used for configs and other resources:

     docker run -d --name gitlab-runner --restart always \
       -v /srv/gitlab-runner/config:/etc/gitlab-runner \
       -v /var/run/docker.sock:/var/run/docker.sock \
       gitlab/gitlab-runner:latest
    复制代码

    Tip: On macOS, use /Users/Shared instead of /srv.

    Or, you can use a config container to mount your custom data volume:

     docker run -d --name gitlab-runner-config \
         -v /etc/gitlab-runner \
         busybox:latest \
         /bin/true
    复制代码

    And then, run the Runner:

     docker run -d --name gitlab-runner --restart always \
         -v /var/run/docker.sock:/var/run/docker.sock \
         --volumes-from gitlab-runner-config \
         gitlab/gitlab-runner:latest
    复制代码
  3. Register the runner you just launched by following the instructions in the Docker section of Registering Runners. The runner won’t pick up any jobs until it’s registered.

安装步骤

获取Gitlab Runner秘钥

可以通过Gitlab管理员账号获取,也可以让每一个用户自行配置

  • 普通用户查看秘钥

进入任意一个仓库的设置中,查看CICD配置

准备注册专用Runner令牌

  • 管理员查看令牌

进入总设置页面,配置全局Runner令牌

Runner注册

由于Runner一般运行复杂构建、打包任务,推荐配置在性能、带宽更大的机房

准备Docker环境
$ curl -sSL https://get.docker.com/ | sh
$ systemctl start docker
复制代码
注册

可以根据需要选择注册Runner类型

同时,为了方便配置,使用单行注册并且关闭交互

单行注册官方文档

命令说明:

  • -v挂载的目的是为了将注册后的配置文件持久化,用于运行容器
  • --rm指定容器运行结束后自动删除停止的容器
  • -it指定使用命令行交互方式运行,便于查看注册结果
$ docker run --rm -it \
  -v /www/wwwroot/gitlab/srv/gitlab-runner/config:/etc/gitlab-runner \
  gitlab/gitlab-runner:alpine-v11.8.0 register \
  --non-interactive \
  --executor "docker" \
  --docker-image docker:stable \
  --url "Gitlab URL" \
  --registration-token "令牌" \
  --description "描述" \
  --tag-list "标签1,标签2" \
  --run-untagged \
  --docker-privileged \
  --locked="false"
复制代码

注册成功提示

此时管理面板显示新的Runner已经注册

Runner 启动

挂载本地配置信息并启动Runner

$ docker run -d --name gitlab-runner --restart always \
 	-v /www/wwwroot/gitlab/srv/gitlab-runner/config:/etc/gitlab-runner \
    -v /var/run/docker.sock:/var/run/docker.sock \
    gitlab/gitlab-runner:alpine-v11.8.0
复制代码

此时可见Runner已经保持和Gitlab的联系

转载于:https://juejin.im/post/5ca804ec51882543bd2d7756

### 极狐 GitLab CI/CD 使用教程 极狐 GitLab 是一款功能强大的 DevOps 平台,其内置的 CI/CD 功能可以帮助团队自动完成代码的构建、测试和部署过程。以下是关于如何配置和使用极狐 GitLab CI/CD 的详细介绍。 #### 基本概念 GitLab CI/CD 提供了一种基于 YAML 文件的方式来自动生成流水线 (Pipeline),该文件名为 `.gitlab-ci.yml`,位于项目的根目录下。它定义了整个 CI/CD 流程中的各个阶段及其执行的任务[^1]。 #### 配置方法 为了启用 GitLab CI/CD,需要创建并编辑项目根目录下的 `.gitlab-ci.yml` 文件。下面是一个简单的示例: ```yaml stages: # 定义流水线的不同阶段 - build - test - deploy build_job: # 定义一个 job 名称为 'build_job' stage: build # 此 job 属于 'build' 阶段 script: - echo "Building the project..." - ./gradlew assembleDebug --info test_job: # 定义另一个 job 名称为 'test_job' stage: test # 此 job 属于 'test' 阶段 script: - echo "Running tests..." - ./gradlew check deploy_job: # 定义第三个 job 名称为 'deploy_job' stage: deploy # 此 job 属于 'deploy' 阶段 script: - echo "Deploying application..." - scp app/build/outputs/apk/debug/app-debug.apk user@remote:/var/www/html/ ``` 上述配置展示了三个主要阶段:构建 (`build`)、测试 (`test`) 和部署 (`deploy`)。每个阶段都由多个 Job 组成,这些 Job 将按照顺序依次运行。 #### 运行环境设置 为了让流水线正常工作,还需要安装并注册至少一个 **Runner** 来执行实际的任务。可以通过以下命令来手动启动一个新的 Runner 实例: ```bash sudo gitlab-runner register ``` 在此过程中会提示输入几个参数,比如 URL 地址、Token 等信息,它们可以从目标项目的 Settings -> CI / CD 页面获取。 #### 高级特性 除了基础的功能外,GitLab CI/CD 还支持许多高级选项,例如条件分支控制、缓存机制以及矩阵化作业等。例如,如果希望仅当特定标签存在时才触发某些 Jobs,则可以在 `only` 或者 `rules` 关键字中指定相应的规则集。 ```yaml release_job: stage: release only: - tags # 只有推送带有 tag 的提交才会触发此 job script: - echo "Creating a new release based on this commit." ``` 以上就是有关极狐 GitLab CI/CD 的基本介绍及简单实例说明。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值