简体   繁体   English

Gitlab CI/CD 在本地运行良好,但在 gitlab 上运行良好

[英]Gitlab CI/CD running fine on local but not on gitlab

I am trying to build a gitlab CI/CD pipeline on my java maven project.我正在尝试在我的 java maven 项目上构建 gitlab CI/CD 管道。 I tried all the stages on my local machine and they are passing successfully but when I try the same things on gitlab repo it is throwing error.我在本地机器上尝试了所有阶段,它们都成功通过了,但是当我在 gitlab 存储库上尝试相同的事情时,它会抛出错误。 I tried almost every solution and gone through numerous posts to resolve the errors but to no use.我尝试了几乎所有解决方案,并浏览了许多帖子来解决错误,但没有用。 Here is my yml content:这是我的 yml 内容:

image: maven:3.5-jdk-8

variables:
   MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
   MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

include:
    - template: Security/SAST.gitlab-ci.yml

cache:
  paths:
    - .m2/settings.xml

# Define stages
#   Stages group various steps into one block,
#   if any step fails, the entire stage fails
stages:
  - validate
  - compile
  - SonarQube
  - test
  
validate: 
  stage: validate
  script:
    - mvn validate

compile:
  stage: compile
  script:
    - mvn $MAVEN_CLI_OPTS compile
  
sonarqube-check:
  image: maven:3.6.3-jdk-11
  stage: SonarQube 
  variables:
    SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
  script:
    - mvn sonar:sonar \ -Dsonar.projectKey=XX.XX.XXX.XXX \ -Dsonar.host.url=http://XX.XX.XXX.XXX \ -Dsonar.login=XX.XX.XXX.XXX -X
  allow_failure: true

spotbugs-sast:
  variables:
    COMPILE: "false"
    SECURE_LOG_LEVEL: "debug"
  artifacts:
    reports:
      sast: gl-sast-report.json


#spotbugs-sast:
#  variables:
#    SECURE_LOG_LEVEL: "debug"
    #FAIL_NEVER: 1

test:
  stage: test
  script:
    - mvn $MAVEN_CLI_OPTS test -B

The stage validate and compile are running fine but I am getting warnings in sonar check and spotbugs sast.阶段验证和编译运行良好,但我在声纳检查和 spotbugs sast 中收到警告。 The same is running fine on my local.在我的本地也运行良好。 In the sonarqube check the error is:在 sonarqube 检查错误是:

Error resolving version for plugin ' -Dsonar.host.url=http://XX.XX.XXX.XXX' from the repositories [local (/builds/XX.XX.XXX.XXX/.m2/repository), central (https://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository -> [Help 1]
org.apache.maven.plugin.version.PluginVersionResolutionException: Error resolving version for plugin ' -Dsonar.host.url=http://XX.XX.XXX.XXX' from the repositories [local (/builds/XXXX/XXXXX/.m2/repository), central (https://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository

and for the spotbugs-sast warning the error is:对于 spotbugs-sast 警告,错误是:

[FATA] [Find Security Bugs] ▶ lstat /root/.m2/repository: no such file or directory

I have spent numerous hours resolving this and waiting for the project to compile successfully with all the stages but every time I was disappointed.我花了很多时间来解决这个问题并等待项目在所有阶段成功编译,但每次我都很失望。 Please help.请帮忙。 I appreciate your time.我很感激你的时间。

Maven recognized the String -Dsonar.host.url=http://XX.XX.XXX.XXX as a plugin/goal that it needs to resolve. Maven 将字符串-Dsonar.host.url=http://XX.XX.XXX.XXX识别为需要解析的插件/目标。
This is caused by the \ in your script.这是由脚本中的\引起的。

Instead of:代替:
mvn sonar:sonar \ -Dsonar.projectKey=XX.XX.XXX.XXX \ -Dsonar.host.url=http://XX.XX.XXX.XXX \ -Dsonar.login=XX.XX.XXX.XXX -X
try:尝试:
mvn sonar:sonar -Dsonar.projectKey=XX.XX.XXX.XXX -Dsonar.host.url=http://XX.XX.XXX.XXX -Dsonar.login=XX.XX.XXX.XXX -X

I assume that you copied that command from a format like this:我假设您从以下格式复制了该命令:

mvn sonar:sonar \
 -Dsonar.projectKey=XX.XX.XXX.XXX \
 -Dsonar.host.url=http://XX.XX.XXX.XXX \
 -Dsonar.login=XX.XX.XXX.XXX -X

In bash the \ must be at the end of the line if the command contains a linebreak so it's recognized as one command.在 bash 中,如果命令包含换行符,则\必须位于行尾,以便将其识别为一个命令。 But you need to remove it if you convert it to one line.但是,如果将其转换为一行,则需要将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM