有许多执行器可用于使用 GitLab Runner 实现 CI/CD。但是,Shell 和 Docker 在其中更受欢迎,我们可以使用这些运行器轻松配置存储库。可以根据资源的要求和可用性来选择这些运行器。本文主要关注Java Linux 应用程序的 Shell 和 Docker 执行器,代码是用 bash 脚本编写的。该应用程序可以使用 bash 脚本进行构建和测试。
Shell Executor :Shell Executor 是一个非常简单的执行器,它有助于在安装了 GitLab Runner 的机器上本地构建解决方案。在这种情况下,GitLab Runner 安装在 Linux Machine 上,因此需要在同一系统中安装所需的软件。
Docker Executor:它是一个强大的工具,包含很多软件,可以通过镜像访问。这个执行器的好处是,我们不需要手动安装任何软件,一切都会通过docker来处理,所需的镜像会从docker hub下载。但是,缺点是出于安全目的,此通信在某些组织中被阻止。所以,如果是这种情况,Shell Executor 是最好的选择。
Java在 Shell Executor 上的实现:
要求:这些是需要在 Linux 机器上安装的基本软件。但是,可以根据编译脚本进行更改,需要时需要下载其他软件。
Software | Description |
---|---|
Git | This is the first requirement, to commit the changes on GitLab. It is a version control software that tracks the changing set of files |
JDK | Need to install a specific version of JDK on the machine which you have targeted, to build the jar file. For example, OpenJDK-8 |
Apache Ant | This is a tool that helps to build processes and generate the jar file of the project while running this file. It contains more information about the project and add this information inside the jar. |
路径配置:安装成功后,需要设置本次安装的软件在机器中的路径,如果没有设置。在机器上运行以下命令。
Variable / File | Path |
---|---|
Git |
Set the path of Git in the Linux machine, if it already not set. Can check with which git export Git = /usr/bin/git |
JAVA |
export JAVA=/usr/bin/java Can check with which java |
Apache Ant |
export ANT=/usr/bin/ant Can check with which ant |
Permission | Give Permission to build.xml before it runs: chmod -R 777 * |
build.xml | It will build the project, and generate the jar based on containing information |
.gitlab-ci.yml | This file should be inside the root directory of the project which contains all the CI/CD configuration including software and script path. Here, you can mention how this repository should run. Before adding this file to the root directory, should check it is a valid yml file or not. |
GitLab Runner 设置:按照以下步骤下载和配置 GitLab Runner。
1.在 Linux 机器上下载 GitLab Runner
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
2.使用以下命令授予其执行权限:
sudo chmod +x /usr/local/bin/gitlab-runner
3. 使用以下命令创建 GitLab CI :
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
4.使用以下命令安装并作为服务运行:
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
5.使用以下命令启动 GitLab Runner :
sudo gitlab-runner start
6.在使用以下命令注册存储库之前停止 GitLab Runner :
sudo gitlab-runner stop
7.一旦 GitLab Runner 成功停止,在终端中输入以下命令进行存储库注册。
sudo gitlab-runner register
8.当您使用 GitLab Runner 进行存储库注册时,必须回答以下问题。
- 输入您的 GitLab 实例 URL:每个组织可以不同,格式类似于 http://gitlab.example.com
- 路径:转到 GitLab 帐户 → 选择要向 runner 注册的存储库 → 设置 → CI/CD → 展开 Runner
- 输入此跑步者的 gitlab-ci 令牌:它将是每个项目的唯一令牌,注册时需要,可以找到
- 路径:转到 GitLab 帐户 → 选择要向 runner 注册的存储库 → 设置 → CI/CD → 展开 Runner
- 输入此跑步者的 gitlab-ci 描述: Put Runner name(any name),这将帮助您记住哪个跑步者正在运行
- 输入此运行程序的 gitlab-ci 标签:如果您想在 yml 文件中提供特定标签时启动 GitLab 运行程序,这是可选的。
- 进入执行器:会有几个执行器的列表,输入shell(因为GitLab Runner会运行我们的系统)
9.注册成功后,使用以下命令启动GitLab Runner
sudo gitlab-runner start
验证 GitLab Runner 是否已注册相应的存储库并且运行器已启动。转到 GitLab 帐户 → 选择要向 runner 注册的存储库 → 设置 → CI/CD → 展开 Runner,将出现一个绿色圆圈可用,并显示消息将为此项目激活 Runners。
注意:如果圆圈为灰色,则表示跑步者尚未开始并重新开始。
Linux GitLab Runner 命令
Command | Description |
---|---|
sudo gitlab-runner register | Register the project with GitLab Runner |
sudo gitlab-runner register | Start the runner |
sudo gitlab-runner stop | Stop the runner |
sudo gitlab-runner status | To know the status of gitlab-runner |
sudo gitlab-runner unregister –name test-runner | Unregister the Runner of a project and replace the test-runner with your runner name and this name can be found inside the config.toml file (where your gitlab-runner ) available. |
sudo gitlab-runner unregister –url http://gitlab.example.com/ –token t0k3n | Remove Runner by URL and token |
sudo gitlab-runner unregister –all-runners | Unregister All Runners |
sudo gitlab-runner restart | This command stops and then starts the GitLab Runner service |
sudo gitlab-runner uninstall | This command stops and uninstalls the GitLab Runner from being run as a service |
sudo gitlab-runner exec | To see a list of available executors, run |
sudo gitlab-runner –help | Check a recent list of commands by executing |
sudo gitlab-runner run –help | Can see the name of the environment variable |
sudo gitlab-runner –debug | To run a command in debug mode |
sudo gitlab-runner exec shell | To see a list of all available options for the shell executor, run |
.gitlab-ci.yml_shell Executor : 下面是 shell 执行器模式下 .gitlab-ci.yml 的内容。但是,如果需要,请根据需要更改它。
stages:
- build
- execute
build:
stage: build
script:
- ant -f build.xml
artifacts:
paths:
- abc.jar
execute:
stage: execute
script:
- pwd
- cd scripts
- chmod -R 777 *
- pwd
- ./run.sh
Java在 Docker Executor 上的实现:无需手动安装任何软件,一切都将从 docker 容器中获取。但是,您可以安装所需的软件,在 yml 文件中输入名称,也可以导出路径。要在 docker executor 模式下运行 GitLab 运行程序,请转到 GitLab 运行程序设置(上面),然后选择 docker 而不是 shell。
.gitlab-ci.yml_ Docker Executor : 下面是 .gitlab-ci.yml 在 docker executor 模式下的内容。但是,如果需要,请更改它。
image: ubuntu:latest
stages:
- build
- execute
before_script:
- echo "Before script section"
- apt-get update && apt-get -y install openjdk-8-jdk && apt-get -y install ant
build:
stage: build
script:
- ant -f build.xml
artifacts:
paths:
- abc.jar
execute:
stage: execute
script:
- pwd
- cd scripts
- chmod -R 777 *
- pwd
- ./runtest.sh