这里有几个问题。
我将开始回答 Java 构建问题,然后是 Runners 问题。
Java 构建
我将从最基本的 Java 构建配置开始,逐步添加功能。
1。基本 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
2。带有工件、缓存和推荐的 Maven 选项
这个新版本:
- 将 Maven 构建输出声明为 GitLab 工件(供以后在下游管道中使用),
- 利用GitLab's cache 缓存本地Maven 存储库(在
.m2/repository 中),
- 还强制执行一些推荐的 Maven 选项以在 CI/CD 上下文中使用。
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"
3。带有单元测试
在 CI/CD 管道中集成单元测试时有两种选择:
- 在与构建相同的作业中运行它们
- 在单独的作业中运行它们
出于管道执行速度和绿色 IT 考虑,我绝对更喜欢选项 1,但我承认人们可能更喜欢第二个。
这是.gitlab-ci.yml文件的新版本,同样实现了GitLab unit tests integration:
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"
4。走得更远
从这一步开始,构建作业仍然可以进一步增强,例如代码覆盖率计算和integration,但这需要更多代码。
另一种以更少工作量实现最先进管道的方法是使用 GitLab CI/CD 模板。例如:
to be continuous 是一个开源项目,它提供了一系列即用型、可配置、可扩展、可组合的模板。
关于跑步者
GitLab 架构非常通用,具有Runners 的概念。
Runners 是基本的executors pool,可以执行 GitLab CI/CD 作业。
关于跑步者需要了解的 2 件事
1。你可以让你的跑步者专业化
使用 GitLab,您可以注册多种跑步者,用于特殊和互补目的。
为了隔离它们,GitLab 支持Tags 的概念。注册跑步者时,应将它们与 functional 标签名称相关联,这将有助于开发人员在其.gitlab-ci.yml 文件中选择最合适的名称。
例如,假设您有 4 名跑步者:
| # |
Description |
Proposed Tags |
| 1 |
Linux-based general purpose runners for building code, running tests, ... with transparent access to the internet |
linux, general, internet you should also allow this one to run untagged jobs (makes it kind of default runner) |
| 2 |
Microsoft-based general purpose runners for building your .NET code |
windows, general, internet
|
| 3 |
compute-optimized runners, made for - say - training super-secret neural networks with no access to the internet |
linux, compute, ml (for machine learning) |
| 4 |
runners located behind your DMZ in your on-premises datacenter, used to perform code/infrastructure deployments |
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
2。跑步者有不同的范围
引用official documentation:
根据您想要访问的人,可以使用跑步者: