Gitlab项目无法解析同一个gitlab组中另一个项目的maven依赖关系

yc0p9oo0  于 2023-03-01  发布在  Maven
关注(0)|答案(3)|浏览(225)

在我的依赖项项目中,我有一个ci_settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
    <servers>
        <server>
            <id>gitlab-maven</id>
            <configuration>
                <httpHeaders>
                    <property>
                        <name>Job-Token</name>
                        <value>${env.CI_JOB_TOKEN}</value>
                    </property>
                </httpHeaders>
            </configuration>
        </server>
    </servers>
</settings>

我已经在pom.xml中配置了注册表:

<repositories>
        <repository>
            <id>gitlab-maven</id>
            <!--suppress UnresolvedMavenProperty -->
            <url>${env.CI_API_V4_URL}/projects/${env.CI_PROJECT_ID}/packages/maven</url>
        </repository>
    </repositories>

    <distributionManagement>
        <repository>
            <id>gitlab-maven</id>
            <!--suppress UnresolvedMavenProperty -->
            <url>${CI_API_V4_URL}/projects/${env.CI_PROJECT_ID}/packages/maven</url>
        </repository>
        <snapshotRepository>
            <id>gitlab-maven</id>
            <!--suppress UnresolvedMavenProperty -->
            <url>${CI_API_V4_URL}/projects/${env.CI_PROJECT_ID}/packages/maven</url>
        </snapshotRepository>
    </distributionManagement>

当我使用以下.gitlab-ci.yml运行CI管道时:

deploy:
  image: maven:3.6-jdk-11
  script:
    - mvn deploy -s ci_settings.xml
  only:
    - main

依赖关系正在成功部署到程序包资料档案库。

Uploading to gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/0.0.1-SNAPSHOT/model-0.0.1-20211216.121731-6.jar
Uploaded to gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/0.0.1-SNAPSHOT/model-0.0.1-20211216.121731-6.jar (35 kB at 61 kB/s)
Uploading to gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/0.0.1-SNAPSHOT/model-0.0.1-20211216.121731-6.pom
Uploaded to gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/0.0.1-SNAPSHOT/model-0.0.1-20211216.121731-6.pom (3.8 kB at 6.3 kB/s)
Downloading from gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/maven-metadata.xml
Downloaded from gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/maven-metadata.xml (283 B at 1.6 kB/s)
Uploading to gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded to gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/0.0.1-SNAPSHOT/maven-metadata.xml (773 B at 1.3 kB/s)
Uploading to gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/maven-metadata.xml
Uploaded to gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/maven-metadata.xml (283 B at 455 B/s)

现在,当我尝试在同一组中的另一个gitlab项目中使用该依赖项时,问题出现了,该依赖项使用的是部署所使用的相同的ci_settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>de.group.my</groupId>
        <artifactId>parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>app</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        ...
        
        <dependency>
            <groupId>de.group.my</groupId>
            <artifactId>model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        ...
    </dependencies>

    <repositories>
        <repository>
            <id>gitlab-maven</id>
            <url>https://gitlab.my.de/api/v4/projects/255/packages/maven</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <distributionManagement>
        <repository>
            <id>gitlab-maven</id>
            <url>https://gitlab.my.de/api/v4/projects/255/packages/maven</url>
        </repository>

        <snapshotRepository>
            <id>gitlab-maven</id>
            <url>https://gitlab.my.de/api/v4/projects/255/packages/maven</url>
        </snapshotRepository>
    </distributionManagement>

</project>

但是当我运行CI管道时,它总是以下列结果结束:

[ERROR] Failed to execute goal on project app: Could not resolve dependencies for project de.group.my:app:jar:0.0.1-SNAPSHOT: Could not find artifact de.group.my:model:jar:0.0.1-SNAPSHOT in gitlab-maven (https://gitlab.my.de/api/v4/projects/255/packages/maven) -> [Help 1]
...
Caused by: org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact de.group.my:model:jar:0.0.1-SNAPSHOT in gitlab-maven (https://gitlab.fronetic.de/api/v4/projects/255/packages/maven

这种情况发生在

  • 元数据. xml
  • pom.xml文件
  • jar

但是将metadata.xml的链接复制到浏览器中时,它会被下载:

<?xml version="1.0" encoding="UTF-8"?>
<metadata modelVersion="1.1.0">
  <groupId>de.group.my</groupId>
  <artifactId>model</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <versioning>
    <snapshot>
      <timestamp>20211216.121731</timestamp>
      <buildNumber>6</buildNumber>
    </snapshot>
    <lastUpdated>20211216121731</lastUpdated>
    <snapshotVersions>
      <snapshotVersion>
        <extension>jar</extension>
        <value>0.0.1-20211216.121731-6</value>
        <updated>20211216121731</updated>
      </snapshotVersion>
      <snapshotVersion>
        <extension>pom</extension>
        <value>0.0.1-20211216.121731-6</value>
        <updated>20211216121731</updated>
      </snapshotVersion>
    </snapshotVersions>
  </versioning>
</metadata>

在它尝试下载的日志中:

Downloading from gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/de/group/my/model/0.0.1-SNAPSHOT/model-0.0.1-SNAPSHOT.jar

但是这个工件不存在。它应该下载model-0.0.1-20211216.121731-6,但是它没有。
其他依赖项可以下载(镜像)

[INFO] Downloading from gitlab-maven: https://gitlab.my.de/api/v4/projects/255/packages/maven/io/micrometer/micrometer-core/1.5.1/micrometer-core-1.5.1.pom
[DEBUG] Writing tracking file /root/.m2/repository/io/micrometer/micrometer-core/1.5.1/micrometer-core-1.5.1.pom.lastUpdated
[DEBUG] Using transporter WagonTransporter with priority -1.0 for https://repo.maven.apache.org/maven2
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://repo.maven.apache.org/maven2
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/io/micrometer/micrometer-core/1.5.1/micrometer-core-1.5.1.pom
[INFO] Downloaded from central: https://repo.maven.apache.org/maven2/io/micrometer/micrometer-core/1.5.1/micrometer-core-1.5.1.pom (8.2 kB at 91 kB/s)
dgiusagp

dgiusagp1#

这个问题困扰了我和整个团队一段时间,然后我们才明白根本原因和解决方案。
因此,我们在GitLab中的同一子组中有2个repos,例如SubGroup1Repo1是指发布到build. gradle文件中Repo2的包注册表的依赖项,如下所示

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url 'https://gitlab.example.com/api/v4/projects/**projectID**/packages/maven'
        credentials(HttpHeaderCredentials) {
            name = project.findProperty("gitlab.token.name") ?: "Job-Token"
            value = project.findProperty("gitlab.token.key") ?: System.getenv("CI_JOB_TOKEN")
        }
        authentication {
            header(HttpHeaderAuthentication)
        }
    }
}

现在问题发生了,因为少数用户的推送构建失败,错误为

> Could not resolve all files for configuration ':compileClasspath'.
   > Could not find com.example.group:artifact-name:versionname.
     Required by:
         project :

显然,当很少有其他用户只是简单地重新触发失败的任务时,它就像一个魔咒,但重新触发不会帮助推动更改的用户,所以它看起来像是在某个地方发生了一些魔法。
从GitLab令牌文档中了解了问题的根本原因。https://docs.gitlab.com/ee/ci/jobs/ci_job_token.html

    • 以下是问题摘要**在排除CI/CD作业令牌身份验证问题时,请注意:启用CI/CD作业令牌限制,并且作业令牌用于访问其他项目时:

1.执行作业的用户必须是正在访问的项目的成员。
1.用户必须具有执行该操作的权限。
1.目标项目必须为作业令牌范围限制的allowlist。
在对构建失败的成员进行故障排除时,我们发现它们作为直接成员添加到Repo1中,而不是Repo2的一部分。
因此,理想的解决方案是,将它们添加为SubGroup1中的成员,这样就可以访问Repo1Repo2,因为它们位于同一个组中,并且访问权限是从组访问权限继承的。如果向组中添加新的存储库,成员将自动继承访问权限,从而减少维护,这也会很有帮助。

ubby3x7f

ubby3x7f2#

discussion之后,OP Mulgard确认:
我在本地解决了。
我需要将我的gitlab用户作为维护者添加到项目中。将其添加到组中是不够的。
但那只是本地的。
gitlab构建仍然无法运行,因为我无法在Docker中获取CI_JOB_TOKEN
但我想那是另一个主题了。
从项目成员权限来看,package registry发布至少需要Developer
CI_JOB_TOKEN问题类似于this issue
我们在gitlab-ce环境中也遇到了同样的问题,任何试图将"CI_JOB_TOKEN"作为管道的一部分推送的尝试都会导致:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) 
 on project <our project>: 

 Failed to deploy artifacts: Could not find artifact com.<our namespace>.spring.ext:<our jar>:0.0.1-20190207.112431-1 
 in gitlab-maven (https://git.<our tld>/api/v4/projects/19/packages/maven) -> [Help 1]

我怀疑这些问题与尝试通过API运行相同的命令有关,对于未经授权的用户,直接返回404(参见:issue 42586)。
修改我们的settings.xml以使用仅启用"api"作用域的个人令牌是可行的。

erhoui1w

erhoui1w3#

    • 原因**:默认情况下,CI_JOB_TOKEN无权访问其他项目的包注册表
    • 溶液**:{项目}〉设置〉配置项\CD〉令牌访问

1.确保已启用"允许使用CI_JOB_TOKEN访问此项目
1.在"允许来自以下项目的配置项作业令牌访问此项目"下
输入依赖项目路径:{用户名}/{依赖项目}
示例:jdoe/hello

相关问题