📌  相关文章
📜  在GitLab上使用Shell和Docker Executor在Java应用程序(Linux)中实现CI / CD

📅  最后修改于: 2021-04-17 02:09:45             🧑  作者: Mango

有许多执行程序可用于通过GitLab Runner实施CI / CD。但是,Shell和Docker在其中更受欢迎,并且我们可以轻松地使用这些运行器配置存储库。可以根据资源的需求和可用性来选择这些运行程序。本文主要关注Java Linux应用程序的Shell和Docker执行程序,并且代码以bash脚本编写。该应用程序可以使用bash脚本进行构建和测试。

Shell Executor :Shell Executor是一个非常简单的执行程序,可帮助在安装了GitLab Runner的计算机上本地构建解决方案。在这种情况下,GitLab Runner安装在Linux机器上,因此需要在同一系统中安装所需的软件。

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实例网址:每个组织的网址可能不同,格式类似于http://gitlab.example.com
  • 路径:转到GitLab帐户→选择要向运行器注册的存储库→设置→CI / CD→扩展运行器
  • 输入该跑步者的gitlab-ci令牌:它将是每个项目的唯一令牌,注册时将需要它,并且可以找到它
  • 路径:转到GitLab帐户→选择要向运行器注册的存储库→设置→CI / CD→扩展运行器
  • 输入该跑步者的gitlab-ci描述:输入跑步者名称(任何名称),这将帮助您记住哪个跑步者正在跑步
  • 输入该运行程序的gitlab-ci标记:当yml文件中有特定标记时,如果要启动GitLab运行程序,则是可选的。
  • 输入执行程序:将有几个执行程序的列表,并键入shell(因为GitLab Runner将运行我们的系统)

9.成功注册后使用以下命令启动GitLab Runner。

sudo gitlab-runner start

要验证GitLab Runner已注册了各自的存储库,并且已启动了Runner。转到GitLab帐户→选择要向运行程序注册的存储库→设置→CI / CD→扩展运行程序,将有一个绿色圆圈,并且显示消息将为此项目激活运行程序。

注意:如果圆圈为灰色,则表示跑步者尚未启动,请重新启动。

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执行程序:以下是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

在Docker Executor上实现Java :无需手动安装任何软件,所有内容都将从Docker容器中获取。但是,您可以安装所需的软件,在yml文件中输入名称,也可以导出路径。要在docker executor模式下运行GitLab Runner,请转到GitLab Runner设置(上图),然后选择docker而不是shell。

.gitlab-ci.yml_ Docker执行器:以下是docker executor模式下.gitlab-ci.yml的内容。但是,如果需要,请更改它。

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