MRK*_*187 61 java spring maven gitlab-ci gitlab-omnibus
我一直在试验没有成功,我正在运行一个在Linux上托管的Gitlab,并试图了解CI功能.
根据Gitlab文档,您只需要创建一个.gitlab-ci.yml
文件,即Travis-CI的Gitlab实现.现在从它的外观来看,你可以完成很多工作.gitlab-ci.yml
,但很多文档都引用了Ruby和其他语言.关于如何构建Java Maven项目没有任何说法.
如何在Java中构建一个简单的应用程序?我可以使用共享运行器,还是应该使用特定的运行器,在这种情况下,我应该选择哪种或哪种运行器实现:ssh,docker或shell?那么,我应该把.gitlab-ci.yml
文件放在文件中至少用Maven构建项目?
rol*_*lve 60
注册Docker runner并使用官方Maven Docker镜像之一,例如,maven:3-jdk7
在您的.gitlab-ci.yml
文件中:
image: maven:3-jdk-7
build:
script: "mvn install -B"
Run Code Online (Sandbox Code Playgroud)
请注意该-B
标志,建议用于非交互式使用.
据我了解,跑步者是共享的还是具体的并不重要.
pis*_*smy 32
这里有几个问题。
我将首先回答 Java 构建问题,然后是 Runners 问题。
我将从最基本的 Java 构建配置开始,逐步添加功能。
此配置有望运行您的 Maven 构建(并且仅运行构建,明确排除单元测试):
stages:
- build
java-build:
# select the most appropriate image for your project
image: maven:3.8-openjdk-11
stage: build
script:
- mvn package -DskipTests=true
Run Code Online (Sandbox Code Playgroud)
这个新版本:
.m2/repository
),stages:
- build
variables:
# This will suppress any download for dependencies and plugins or upload messages which would clutter the console log.
# `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
MAVEN_OPTS: >-
-Dhttps.protocols=TLSv1.2
-Dmaven.repo.local=.m2/repository
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN
-Dorg.slf4j.simpleLogger.showDateTime=true
-Djava.awt.headless=true
# As of Maven 3.3.0 instead of this you may define these options in `.mvn/maven.config` so the same config is used
# when running from the command line.
# `installAtEnd` and `deployAtEnd` are only effective with recent version of the corresponding plugins.
MAVEN_CLI_OPTS: >-
--batch-mode
--errors
--fail-at-end
--show-version
-DinstallAtEnd=true
-DdeployAtEnd=true
java-build:
# select the most appropriate image for your project
image: maven:3.8-openjdk-11
stage: build
# Cache downloaded dependencies and plugins between builds.
# The key here separates one cache per branch/tag ($CI_COMMIT_REF_SLUG)
cache:
key: "maven-$CI_COMMIT_REF_SLUG"
paths:
- .m2/repository
script:
- mvn $MAVEN_CLI_OPTS package -DskipTests=true
artifacts:
name: "Maven artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
paths:
- "**/target"
Run Code Online (Sandbox Code Playgroud)
将单元测试集成到 CI/CD 管道中时有两个选项:
就管道执行速度和绿色 IT 考虑而言,我绝对更喜欢选项 1,但我承认人们可能更喜欢第二种。
这是该文件的新版本.gitlab-ci.yml
,还实现了GitLab 单元测试集成:
stages:
- build
variables:
# This will suppress any download for dependencies and plugins or upload messages which would clutter the console log.
# `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
MAVEN_OPTS: >-
-Dhttps.protocols=TLSv1.2
-Dmaven.repo.local=.m2/repository
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN
-Dorg.slf4j.simpleLogger.showDateTime=true
-Djava.awt.headless=true
# As of Maven 3.3.0 instead of this you may define these options in `.mvn/maven.config` so the same config is used
# when running from the command line.
# `installAtEnd` and `deployAtEnd` are only effective with recent version of the corresponding plugins.
MAVEN_CLI_OPTS: >-
--batch-mode
--errors
--fail-at-end
--show-version
-DinstallAtEnd=true
-DdeployAtEnd=true
java-build-and-test:
# select the most appropriate image for your project
image: maven:3.8-openjdk-11
stage: build
# Cache downloaded dependencies and plugins between builds.
# The key here separates one cache per branch/tag ($CI_COMMIT_REF_SLUG)
cache:
key: "maven-$CI_COMMIT_REF_SLUG"
paths:
- .m2/repository
script:
# the 'verify' goal is definitely the most appropriate here
- mvn $MAVEN_CLI_OPTS verify
artifacts:
name: "Maven artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
paths:
- "**/target"
reports:
# declare the JUnit reports (recursive pattern for multi-module projects)
junit:
- "**/target/*-reports/TEST-*.xml"
Run Code Online (Sandbox Code Playgroud)
从这一步开始,构建作业仍然可以进一步增强,例如通过代码覆盖率计算和集成,但这需要更多的代码。
另一种以更少的努力实现最先进管道的方法是使用 GitLab CI/CD 模板。例如:
to be Continuous是一个开源项目,提供了一系列即用型、可配置、可扩展、可组合的模板。
GitLab 架构非常通用,具有Runner的概念。Runners 是基本的执行器池,可以执行 GitLab CI/CD 作业。
关于跑步者需要了解的 2 件事
通过 GitLab,您可以注册多种类型的运行程序,这些运行程序是为了特殊和互补的目的而设计的。
为了隔离它们,GitLab 支持Tags的概念。注册跑步者时,您应将它们与功能标签名称相关联,这将帮助开发人员在其文件中选择最合适的标签名称.gitlab-ci.yml
。
例如,假设您有 4 名跑步者:
# | 描述 | 建议的标签 |
---|---|---|
1 | 基于 Linux 的通用运行程序,用于构建代码、运行测试......并透明地访问互联网 | linux , general ,internet 您还应该允许这个运行未标记的作业(使其成为一种默认运行程序) |
2 | 基于 Microsoft 的通用运行程序,用于构建 .NET 代码 | windows , general ,internet |
3 | 计算优化的跑步者,用于训练无法访问互联网的超级秘密神经网络 | linux , compute , ml (用于机器学习) |
4 | 位于本地数据中心 DMZ 后面的运行器,用于执行代码/基础设施部署 | linux , deploy ,datacenter |
通过此设置,同时具有 Java 和 .NET 代码的 monorepo 项目可以声明以下.gitlab-ci.yml
文件:
stages:
- build
- test
- deploy
# this job declares no tag: will be executed by default runner (#1)
java-build:
image: maven:3.8-openjdk-11
stage: build
script:
- Java build code here
dotnet-build:
image: mcr.microsoft.com/dotnet/sdk:5.0
stage: build
tags:
- windows
- general
script:
- .NET build code here
# this job declares no tag: will be executed by default runner (#1)
java-test:
image: maven:3.8-openjdk-11
stage: test
script:
- Java test code here
dotnet-test:
image: mcr.microsoft.com/dotnet/sdk:5.0
stage: test
tags:
- windows
- general
script:
- .NET test code here
deploy:
stage: deploy
tags:
- deploy
- datacenter
script:
- deployment code here
Run Code Online (Sandbox Code Playgroud)
引用官方文档:
跑步者的可用取决于您想要访问的人:
我花了很多时间尝试在 Gitlab CI 上设置我们的 Java 项目。我让它在某种程度上取得了成功。正如 rolve 所提到的,最直接的解决方案是使用官方 repo 中的图像:https ://hub.docker.com/_/maven
但是,我们有一个公司代理,它导致我的构建在获取项目的依赖项时收到超时请求。我尝试了很多解决方案,最后看到了这篇文章:https : //gitlab.com/gitlab-org/gitlab-ce/issues/15167。
该帖子本身是关于设置 maven 以在本地存储库中缓存下载的依赖项,该存储库可在构建之间访问。这个想法是你可以在.gitlab-ci.yml 中编写一个本地 maven 配置文件来设置你的缓存目录和你的代理。
before_script:
-echo '<settings
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/cache/.m2</localRepository>
<proxies>
<proxy>
<active>true</active>
<protocol>'$PROXY_PROTOCOL'</protocol>
<host>'$PROXY_HOST'</host>
<port>'$PROXY_PORT'</port>
</proxy>
</proxies>
</settings>' > $HOME/.m2/settings.xml
build_debug1:
stage: build
script: "echo $PROXY_HOST"
build_debug2:
stage: build
script: "cat $HOME/.m2/settings.xml"
build_maven:
stage: build
script: "mvn $MAVEN_CLI_OPTS package"
artifacts:
paths:
- target/*.jar
deploy_debug1:
stage: package
script: "ls target/"
Run Code Online (Sandbox Code Playgroud)
请注意,构建调试作业仅用于查看代理设置是否被正确注入。您可以通过转到 Project -> Settings -> CI/CD Pipelines -> Secret Variables 使用 Gitlab 将代理环境变量设置为机密。
最后一项deploy_debug
工作是查看目标目录中生成的内容。
我想在这里补充一点信息。首先,让我们清除有关共享跑步者和特定跑步者的一些困惑。
共享运行程序: 顾名思义,共享运行程序是构建流程实例,可用于执行已启用允许共享运行程序选项的已安装gitlab实例中每个项目的作业。要做到这一点,您将需要管理权限。根据当前的gitlab文档,只有具有管理权限的用户才能定义共享运行器。
特定的运行程序这种运行程序仅执行一个项目的工作。
同样,在为项目选择运行器时,这些要点也要牢记。
现在为项目选择合适的执行器,非常重要的一点是,让我们对gitlabRunner的所有可用执行器具有鸟瞰图。通过在此处提供出色的文档说明,Gitlab可以使我们轻松完成这项工作,这些文档说明了使用不同执行程序可以得到哪些不同的选择。
如果您想了解更多有关跑步者和不同执行者的信息,建议您从本文开始, Gitlab Runner
该文档描述了用于控制构建的 YAML 语法:
那么为什么不尝试从以下开始呢?
job1:
script: "mvn package"
Run Code Online (Sandbox Code Playgroud)
据推测,这只有在已经安装 Maven 的情况下才有效,因此您需要一个支持此功能的运行程序。
我没有使用过 GitLab,但文档建议您可以进一步自定义它以使用官方 Maven Docker 映像来执行构建。看起来很有趣,但我同意文档中缺少 Java 示例。
归档时间: |
|
查看次数: |
57949 次 |
最近记录: |