Home > Enterprise >  How to access Gitlab Package Registry from CI/CD Dockerfile?
How to access Gitlab Package Registry from CI/CD Dockerfile?

Time:03-30

I set up a Package Registry in gitlab that contains a my-commons.jar, making use of it in my project as follows:

pom.xml:

<repositories>
    <repository>
        <id>gitlab-maven</id>
        <url>https://git.my-company.com/api/v4/projects/295/packages/maven</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>com.my.company</groupId>
        <artifactId>my-commons</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

This also requires the use of a Private-Token for access, that is placed into the ~/.m2/settings.xml:

<settings>
  <servers>
    <server>
      <id>gitlab-maven</id>
      <configuration>
        <httpHeaders>
          <property>
            <name>Private-Token</name>
            <value>MY_TOKEN</value>
          </property>
        </httpHeaders>
      </configuration>
    </server>
  </servers>
</settings>

This works fine locally.

Question: how can I reuse this in a docker file, and especially when it comes to the gitlab CI/CD pipeline. Should I commit a custom settings.xml to gitlab that includes the private token, so that the gitlab pipeline knows how to access the package repository?

Dockerfile:

FROM $BUILD_IMAGE as dependencies
COPY pom.xml .
COPY src src
RUN mvn package
#RUN mvn package private_settings.xml #is that reasonable?

CodePudding user response:

I would put the token into a gitlab variable like $MAVEN_TOKEN, then use $MAVEN_TOKEN in the settings.xml and commit the settings.xml to the repository.

CodePudding user response:

You can pass it as an environment variable.
settings.xml

<settings>
  <servers>
    <server>
      <id>gitlab-maven</id>
      <configuration>
        <httpHeaders>
          <property>
            <name>Private-Token</name>
            <value>${env.MY_TOKEN}</value>
          </property>
        </httpHeaders>
      </configuration>
    </server>
  </servers>
</settings>

Dockerfile

FROM $BUILD_IMAGE as dependencies
COPY pom.xml .
COPY src src
COPY settings.xml settings.xml

ARG MY_TOKEN
ENV MY_TOKEN=${MY_TOKEN}

RUN mvn package
RUN mvn package -s settings.xml

Then, use docker build

docker build --build-arg MY_TOKEN=$MY_TOKEN -t image-name:tag .
  • Related