LoginSignup
9
9

More than 3 years have passed since last update.

GitLab CEで使えるようになったパッケージレジストリへ、Mavenパッケージを登録してみる

Last updated at Posted at 2020-12-08

毎度、ググっても出てこない小ネタを取り扱っております。
本記事は個人的な見解であり、筆者の所属するいかなる団体にも関係ございません。

0. はじめに

今年の5月に、これまでGitLabで有料版だった機能を無料版(CE:Community Edition)でも使えるようにするよ、というアナウンスがされました。

18 GitLab features are moving to open source | GitLab
https://about.gitlab.com/blog/2020/03/30/new-features-to-core/

その中で、各種パッケージを入れるリポジトリーも無料になるよということでした。

image.png

Maven (Java) repositoryは、社内でも取り扱いが多いので手ぐすね引いて待っておりました。
それの検証記事になります。

1. ドキュメント

基本的に以下のページに書いてあります。
Maven packages in the Package Repository | GitLab
https://docs.gitlab.com/ee/user/packages/maven_repository/index.html#create-maven-packages-with-gitlab-cicd

こちらの日本語サイトもそのうち翻訳されるのではないでしょうか。
GitLab Maven Repository | GitLab
https://gitlab-docs.creationline.com/ee/user/packages/maven_repository/

今回やってみたかったのは、CI/CDでの操作なので、以下を参照しました。

Maven packages in the Package Repository | GitLab
https://docs.gitlab.com/ee/user/packages/maven_repository/index.html#create-maven-packages-with-gitlab-cicd

2. 必要なファイル

以下の3つのファイルの修正が必要です。

  1. .gitlab-ci.ymlファイルの修正
  2. MavenのCI用設定ファイル(ci_settings.xml)
  3. pom.xmlファイルの修正

それぞれ中身を見てみます。

2-1. .gitlab-ci.ymlファイルの修正

mvn deployコマンドを追加して、ci_settings.xmlを参照するように変更します。
imageは、お好きに変更してください。

.gitlab-ci.yml
deploy:
  image: maven:3.6-jdk-11
  script:
    - 'mvn deploy -s ci_settings.xml'
  only:
    - master

2-2. MavenのCI用設定ファイル(ci_settings.xml)

ci_settings.xmlを以下の内容で作成します。

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>

2-3. pom.xmlファイルの修正

pom.xmlに以下の項目を追記します。

pom.xml
<repositories>
  <repository>
    <id>gitlab-maven</id>
    <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url>
  </repository>
</repositories>
<distributionManagement>
  <repository>
    <id>gitlab-maven</id>
    <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url>
  </repository>
  <snapshotRepository>
    <id>gitlab-maven</id>
    <url>${env.CI_SERVER_URL}/api/v4/projects/${env.CI_PROJECT_ID}/packages/maven</url>
  </snapshotRepository>
</distributionManagement>

3. それぞれのファイルの相関関係図

図解してみました。
image.png

4. 実行確認

こんな感じに入るはず。

image.png

5. まとめ

GitLab CI/CDでだけ影響のある部分を、ci_settings.xmlで書き出すのがポイントでしょうか。
Artifactsで保存するのも良いですが、期限が切れたら削除されてしまったり、場所が分かりにくかったりするので、レジストリーに保存するのはお勧めです。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9