使用 maven 将工件推送到 Gitlab

Using maven to push an artifact to Gitlab

我有一个项目用于将工件推送到 Nexus,但我们现在需要更改它以将其推送到 Gitlab。当我的 CI/CD 管道尝试执行此部署时,出现以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy 
(default-deploy) on project foo: Failed to retrieve remote metadata foo:1.0.4-SNAPSHOT/maven-
metadata.xml: Could not transfer metadata foo:1.0.4-SNAPSHOT/maven-metadata.xml from/to foo-
snapshots (https://foo.nexus.com/repository/foo-snapshots/): Not authorized -> [Help 1]

请注意,它仍在尝试将其推送到 Nexus,而不是将其推送到 Gitlab。如何更改我的 gitlab-ci.yml 文件或我的 settings.xml 以将其推送到 Gitlab?

这是我的 gitlab-ci.yml 文件:

stages:
  - build
deploy:
  stage: build
  script:
    - mvn clean deploy -e -B -U -Ddist=true -s settings.xml

请注意,在将其推送到 Nexus 之前,这是脚本中的第一行代码: mvn-settings -u "${NEXUS_DEPLOY_USER}" -p "${NEXUS_DEPLOY_PASS}" --master-password "${NEXUS_MASTER_PASS}" -s foo-snapshots。但是我想我可以通过 -s settings.xml.

来覆盖它

在这个settings.xml中是我的gitlab token:

     <server>
       <id>foo</id>
       <configuration>
         <httpHeaders>
            <property>
              <name>Job-Token</name>
              <value>${env.CI_JOB_TOKEN}</value>

并在其下定义了一些分发存储库:

<profiles>
    <profile>
      <id>gitlab-foo</id>
      <repositories>
        <repository>
          <id>foo</id>
          <url>https://gitlab.foo/api/v4/projects/700/packages/maven</url>
        </repository>
      </repositories>
      <properties>
        <distribution.repository.id>foo-snapshots</distribution.repository.id>
        <url>https://gitlab.foo/api/v4/projects/700/packages/maven</url>

这里还有我的 pom.xml 文件的相关部分:

<distributionManagement>
        <repository>
            <id>foo-snapshots</id>
    <url>https://gitlab.foo/api/v4/projects/706/packages/maven</url>
        </repository>
        <snapshotRepository>
            <id>foo-snapshots</id>
    <url>https://gitlab.foo/api/v4/projects/706/packages/maven</url>
        </snapshotRepository>
</distributionManagement>

确保您已指定部署存储库。结合不同的方法:

  1. 确保你的 pom 中有一个 distributionManagement 部分指向 gitlab (docs here)
  2. 通过 maven 命令行指定参数覆盖部署插件的目标:-DaltDeploymentRepository=foo-snapshots::https://gitlab.foo/api/v4/projects/700/packages/maven (docs here)