[英]How to use Gitlab CI to build a Java Maven project?
我一直在嘗試沒有任何成功,我正在運行一個托管在 Linux 上的 Gitlab,並試圖了解 CI 功能。
根據 Gitlab 文檔,您只需要創建一個.gitlab-ci.yml
文件,即 Travis-CI 的 Gitlab 實現。 現在從外觀上看,您可以使用.gitlab-ci.yml
完成很多工作,但是很多文檔都引用了 Ruby 和其他語言。 沒有提及如何構建 Java Maven 項目。
如何在 Java 中構建一個簡單的應用程序? 我可以使用共享運行器,還是應該使用特定的運行器,在這種情況下,我應該選擇什么或哪個運行器實現:ssh、docker 或 shell? 那么,我至少應該在.gitlab-ci.yml
文件中放入什么來使用 Maven 構建項目?
注冊一個 Docker 運行器並使用官方 Maven Docker 映像之一,例如maven:3-jdk-11
在您的.gitlab-ci.yml
文件中:
image: maven:3-jdk-11
build:
script: "mvn install -B"
注意-B
標志,建議用於非交互式使用。
據我了解,跑步者是共享的還是特定的都沒有關系。
這里有幾個問題。
我將開始回答 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
這個新版本:
.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"
在 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"
從這一步開始,構建工作仍然可以進一步增強,例如代碼覆蓋率計算和集成,但這需要更多的代碼。
另一種以更少努力實現最先進管道的方法是使用 GitLab CI/CD 模板。 例如:
to be Continuous是一個開源項目,它提供了一系列即用型、可配置、可擴展、可組合的模板。
GitLab 架構非常通用,具有Runners的概念。 Runners 是基本的執行器池,可以執行 GitLab CI/CD 作業。
關於跑步者需要了解的 2 件事
使用 GitLab,您可以注冊多種跑步者,用於特殊和互補目的。
為了隔離它們,GitLab 支持標簽的概念。 注冊跑步者時,應將它們與功能標簽名稱相關聯,這將有助於開發人員在其.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
引用官方文檔:
跑步者可根據您想要訪問的人提供:
我想在這里添加一些信息。 首先讓我們澄清一些關於共享和特定跑步者的困惑。
Shared Runner:顧名思義,shared runners 是構建流程實例,可用於在已安裝的 gitlab 實例中執行每個項目的作業,並啟用Allowed Shared runners選項。 當然,要做到這一點,您需要管理權限。 根據當前的 gitlab 文檔,只有使用管理權限才能定義共享運行器。
特定的跑步者這種跑步者只執行一個項目的工作。
此外,在為您的項目選擇跑步者時,這些是需要牢記的幾個要點。
現在要為項目選擇正確的執行器,我們對 gitlab runner 的所有可用執行器有鳥瞰圖非常重要。 Gitlab 在這里提供了很好的文檔,解釋了使用不同的執行器可以獲得哪些不同的選項,從而使這項工作對我們來說很容易。
如果您想了解更多關於 runner 和不同 executor 的信息,我建議您從這篇文章開始, Gitlab Runner
我花了相當多的時間嘗試在 Gitlab CI 上設置我們的 Java 項目。 我得到了一定程度的成功。 正如 rolve 所提到的,最直接的解決方案是使用來自官方 repo 的圖像: https ://hub.docker.com/_/maven
但是,我們有一個公司代理,導致我的構建在獲取項目的依賴項時收到超時請求。 我嘗試了很多解決方案,最后看到了這篇文章: https ://gitlab.com/gitlab-org/gitlab-ce/issues/15167。
這篇文章本身是關於設置 maven 以在本地 repo 中緩存下載的依賴項,該 repo 可以在構建之間訪問。 這個想法是你可以通過在.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/"
請注意,構建調試作業只是為了查看代理設置是否被正確注入。 您可以使用 Gitlab 將代理環境變量設置為秘密,方法是轉到項目 -> 設置 -> CI/CD 管道 -> 秘密變量。
最后一個deploy_debug
作業是查看目標目錄中生成的內容。
該文檔描述了用於控制構建的 YAML 語法:
那么為什么不嘗試從以下開始呢?:
job1:
script: "mvn package"
據推測,這僅在已安裝 Maven 的情況下才有效,因此您需要一個支持此功能的運行器。
我沒有使用 GitLab,但文檔建議您可以進一步自定義它以使用官方 Maven Docker 映像來執行構建。 看起來很有趣,但我同意文檔缺少 Java 示例。
我使用這個命令,但一般來說,關於 java/maven 構建的文檔似乎很少見
maven-package:
script: "mvn install -B"
借助如何使用 GitLab CI/CD 將 Maven 項目部署到 Artifactory
您只需將.gitlab-ci.yml
文件添加到存儲庫的根目錄,即可編譯您的 java maven 項目,其中包含以下內容:
image: maven:latest
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
paths:
- .m2/repository/
- target/
build:
stage: build
script:
- mvn compile
只需檢查是否允許或在 mvn build/test 命令之后添加步驟。 我正在檢查是否有在構建后運行腳本的選項,而不管構建狀態如何。
像下面這樣的東西。 無論 mvn 測試狀態如何,我都想在哪里運行。 有什么想法嗎? 我知道一種將其作為后腳本運行的方法。 但試圖避免這種情況。
build:
stage: build
script:
- mvn test
- ./extract_data.sh
#-----------Base image that contains required maven and jdk--------#
image: basebuildimage
variables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
stages:
- build
#----------Build code-----------#
build:
stage: build
script:
- mvn $MAVEN_CLI_OPTS clean install
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.