membersound
membersound

Reputation: 86747

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

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?

Upvotes: 2

Views: 2197

Answers (2)

quoc9x
quoc9x

Reputation: 2171

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>${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

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 .

docker will get $MY_TOKEN from env of OS.

Upvotes: 1

J Fabian Meier
J Fabian Meier

Reputation: 35805

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.

Upvotes: 1

Related Questions