How to publish packages to gitlab package registry

How to publish packages to gitlab package registry

gitlab_registries

Here in this article we will try to compile and build a package for maven project and push the artifacts to gitlab package registry.

Test Environment

Fedora 39 server
gitlab-ee 16.9.2

If you are interested in watching the video. Here is the YouTube video on the same step by step procedure outlined below.

Procedure

Step1: Ensure Gitlab and Gitlab runner installed

For this demo, please ensure that you have gitlab and gitlab runner installed or available for use. In my case i have got both the gitlab server and gitlab runner installed on the same machine for this demo purpose.

admin@fedres:~$ sudo gitlab-ctl status
run: alertmanager: (pid 1131) 0s; run: log: (pid 1116) 0s
run: gitaly: (pid 1083) 0s; run: log: (pid 1078) 0s
run: gitlab-exporter: (pid 1129) 0s; run: log: (pid 1119) 0s
run: gitlab-kas: (pid 1086) 0s; run: log: (pid 1082) 0s
run: gitlab-pages: (pid 1135) 0s; run: log: (pid 1122) 0s
run: gitlab-workhorse: (pid 1118) 0s; run: log: (pid 1102) 0s
run: logrotate: (pid 1101) 0s; run: log: (pid 1091) 0s
run: nginx: (pid 1093) 0s; run: log: (pid 1092) 0s
run: node-exporter: (pid 1130) 0s; run: log: (pid 1121) 0s
run: postgres-exporter: (pid 1132) 0s; run: log: (pid 1115) 0s
run: postgresql: (pid 1085) 0s; run: log: (pid 1080) 0s
run: prometheus: (pid 1134) 0s; run: log: (pid 1133) 0s
run: puma: (pid 1097) 0s; run: log: (pid 1089) 0s
run: redis: (pid 1081) 0s; run: log: (pid 1077) 0s
run: redis-exporter: (pid 1120) 0s; run: log: (pid 1114) 0s
run: sidekiq: (pid 1094) 0s; run: log: (pid 1087) 0s
admin@fedres:~$ sudo gitlab-runner status
Runtime platform                                    arch=amd64 os=linux pid=1827 revision=b92ee590 version=17.4.0
gitlab-runner: Service is running

Step2: Ensure maven and jdk installed on runner

Here in this step we will be installing maven and java jdk package as shown below.

admin@fedres:~$ sudo dnf install maven java-17-openjdk-devel

Step3: Add the CA cert to java trust store cacerts on gitlab runner

If you have gitlab server running on secure port using self signed certificate. You need to trust the self sign certificate by adding it to the java trust store cacerts on gitlab runner so that the communication from gitlab runner to gitlab server while uploading the artifacts does not fail PKI cert trust issues.

admin@fedres:~$ sudo keytool -trustcacerts -keystore "/usr/lib/jvm/jre-17-openjdk/lib/security/cacerts" -storepass changeit -importcert -alias fedres.stack -file /home/admin/middleware/certs/fedres.stack.com.pem

NOTE: fedres.stack.com.pem is the self signed cert for my gitlab instance

Ensure that the trust certificate has been added to the cacerts.

admin@fedres:~$ keytool -list -cacerts | grep fedres
Enter keystore password:  changeit
fedres.stack, 11-Oct-2024, trustedCertEntry, 

Step4: Create a personal access token for gitlab registry access

In my case i have a user “dev” for which i am going to create a personl access token.

Go To – Your Profile – Preferences – Access Tokens and create you own personal access token.

Step5: Update maven settings with gitlab registry server details on gitlab runner

The default installation of maven generates a “settings.xml” file in “/etc/maven/”. We need to update the following file with the gitlab registry details as shown below. In the servers section, i have added the sub section “server” with id “gitlab-maven” with the username and the personal access token we just created in the last step.

admin@fedser:~$ cat /etc/maven/settings.xml
...
  <servers>
    <server>
      <id>gitlab-maven</id>
      <username>dev</username>
      <password>glpat-PRvts5M7GVWF-RBvyQtN</password>
      <configuration>
        <authenticationInfo>
          <userName>dev</userName>
          <password>glpat-PRvts5M7GVWF-RBvyQtN</password>
        </authenticationInfo>
      </configuration>
    </server>
  </servers>
...

Step6: Create a blank project in your namespace

Here i am going to create a blank project name “hellomaven” as shown below.

Step7: Clone blank project

admin@fedres:~$ git clone git@fedres.stack.com:stack/hellomaven.git
admin@fedres:~/hellomaven$ git switch –create main
admin@fedres:~/hellomaven$ touch README.md
admin@fedres:~/hellomaven$ git add README.md
admin@fedres:~/hellomaven$ git commit -m “add README”
admin@fedres:~/hellomaven$ git push -u origin main

Step8: Create a maven archetype project

Here we are going to create a maven based archetype project by providing the necessary details as shown below.

admin@fedres:~/hellomaven$ mvn archetype:generate
...
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 2189: 
Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
7: 1.3
8: 1.4
9: 1.5
Choose a number: 9: 
[INFO] Using property: javaCompilerVersion = 17
[INFO] Using property: junitVersion = 5.11.0
Define value for property 'groupId': com.stack
Define value for property 'artifactId': hellomaven
Define value for property 'version' 1.0-SNAPSHOT: 
Define value for property 'package' com.stack: com.stack
Confirm properties configuration:
javaCompilerVersion: 17
junitVersion: 5.11.0
groupId: com.stack
artifactId: hellomaven
version: 1.0-SNAPSHOT
package: com.stack
 Y: Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.5
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.stack
[INFO] Parameter: artifactId, Value: hellomaven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.stack
[INFO] Parameter: packageInPathFormat, Value: com/stack
[INFO] Parameter: junitVersion, Value: 5.11.0
[INFO] Parameter: package, Value: com.stack
[INFO] Parameter: groupId, Value: com.stack
[INFO] Parameter: artifactId, Value: hellomaven
[INFO] Parameter: javaCompilerVersion, Value: 17
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[WARNING] Don't override file /home/admin/hellomaven/hellomaven/src/main/java/com/stack
[WARNING] Don't override file /home/admin/hellomaven/hellomaven/src/test/java/com/stack
[WARNING] CP Don't override file /home/admin/hellomaven/hellomaven/.mvn
[INFO] Project created from Archetype in dir: /home/admin/hellomaven/hellomaven
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:17 min
[INFO] Finished at: 2024-10-15T17:14:19+05:30
[INFO] ------------------------------------------------------------------------

Move the content of hellomaven folder to the root hellomaven folder as shown below.

admin@fedres:~/hellomaven$ mv hellomaven/* .
admin@fedres:~/hellomaven$ rm -rf hellomaven

admin@fedres:~/hellomaven$ ls -ltr
total 4
-rw-r--r--. 1 admin admin    0 Oct 15 17:11 README.md
-rw-r--r--. 1 admin admin 3101 Oct 15 17:14 pom.xml
drwxr-xr-x. 4 admin admin   30 Oct 15 17:14 src

Step9: Update pom.xml with the gitlab registry endpoint in the project

Here in this step we will need to update our pom.xml with the repositories section and distrubitionManagement section as shown below. Maven will connecto the gitlab repoisitory endpoint and upload the packages to the respective repositories for release and snapshot packages.

admin@fedres:~/hellomaven$ cat pom.xml 
...
  <repositories>
    <repository>
      <id>gitlab-maven</id>
      <url>https://fedres.stack.com/api/v4/projects/21/packages/maven</url>
    </repository>
  </repositories>
  <distributionManagement>
    <repository>
      <id>gitlab-maven</id>
      <url>https://fedres.stack.com/api/v4/projects/21/packages/maven</url>
    </repository>
    <snapshotRepository>
      <id>gitlab-maven</id>
      <url>https://fedres.stack.com/api/v4/projects/21/packages/maven</url>
    </snapshotRepository>
  </distributionManagement>

Step10: Create a .gitlab-ci.yml as shown below

Here is our very basic gitlab cicd pipeline where in we will build the package, test it and publish it to the the gitlab artifacts registry when a commit is pushed to the main branch of the repository.

admin@fedres:~/hellomaven$ cat .gitlab-ci.yml 
#image: maven:3.8.5-openjdk-17

#variables:
#  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
#  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
#
#cache:
#  paths:
#    - .m2/repository/
#    - target/

stages:
  - build
  - test
  - publish

build:
  stage: build
  script:
    - mvn compile

test:
  stage: test
  script:
    - mvn test

publish:
  stage: publish
  script:
    - mvn deploy
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

Push the changes to your gitlab repository

admin@fedres:~/hellomaven$ git add .

admin@fedres:~/hellomaven$ git commit -m "update maven project"
[main 8767b82] update maven project
 4 files changed, 151 insertions(+)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/stack/App.java
 create mode 100644 src/test/java/com/stack/AppTest.java
 
admin@fedres:~/hellomaven$ git push -u origin main
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (15/15), 2.12 KiB | 2.12 MiB/s, done.
Total 15 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To fedres.stack.com:stack/hellomaven.git
   833c815..8767b82  main -> main
branch 'main' set up to track 'origin/main'.

Step11: Validate the build artifacts

Once the changes are pushed to the main branch, gitlab cicd pipeline will trigger and execute our build, test and publish stages as shown below.

Now Go To – hellomaven project – Deploy – Package registry and you should be able to the see the artifacts that are pushed to gitlab registry

Hope you enjoyed reading this article. Thank you..