前提: 有一个部署在 AWS ec2 实例上的 gitlab ,并有管理这个账号的相关权限。


开启 Package Registry, 并使用 S3 bucket

################################################################################
## Package repository
##! Docs: https://docs.gitlab.com/ee/administration/packages/
################################################################################

# gitlab_rails['packages_enabled'] = true
# gitlab_rails['packages_storage_path'] = "/var/opt/gitlab/gitlab-rails/shared/packages"
# gitlab_rails['packages_object_store_enabled'] = false
# gitlab_rails['packages_object_store_proxy_download'] = false
# gitlab_rails['packages_object_store_remote_directory'] = "packages"
# gitlab_rails['packages_object_store_connection'] = {
#   'provider' => 'AWS',
#   'region' => 'eu-west-1',
#   'aws_access_key_id' => 'AWS_ACCESS_KEY_ID',
#   'aws_secret_access_key' => 'AWS_SECRET_ACCESS_KEY',
#   # # The below options configure an S3 compatible host instead of AWS
#   # 'host' => 's3.amazonaws.com',
#   # 'aws_signature_version' => 4, # For creation of signed URLs. Set to 2 if provider does not support v4.
#   # 'endpoint' => 'https://s3.amazonaws.com', # default: nil - Useful for S3 compliant services such as DigitalOcean Spaces
#   # 'path_style' => false # Use 'host/bucket_name/object' instead of 'bucket_name.host/object'
# }
gitlab_rails['packages_enabled'] = true
gitlab_rails['packages_object_store_enabled'] = true
gitlab_rails['packages_object_store_remote_directory'] = "your-s3-bucket-name"
gitlab_rails['packages_object_store_connection'] = {
  'provider' => 'AWS',
  'region' => 'ap-east-1',
  'use_iam_profile' => true,
}

gitlab_rails[‘packages_object_store_remote_directory’] 对象存储的名称,并且不需要指定 path ,至少目前没有这个参数。


Use Amazon instance profiles

使用角色向 Amazon EC2 实例上运行的应用程序授予权限。

To set up an instance profile:

  1. Create an IAM role with the necessary permissions. The following example is a role for an S3 bucket named test-bucket:

    JSONCopy to clipboard

    {
       "Version": "2012-10-17",
       "Statement": [
           {
               "Effect": "Allow",
               "Action": [
                   "s3:PutObject",
                   "s3:GetObject",
                   "s3:DeleteObject"
               ],
               "Resource": "arn:aws:s3:::test-bucket/*"
           },
           {
               "Effect": "Allow",
               "Action": [
                   "s3:ListBucket"
               ],
               "Resource": "arn:aws:s3:::test-bucket"
           }
       ]
    }
  2. Attach this role to the EC2 instance hosting your GitLab instance.

  3. Set the use_iam_profile GitLab configuration option to true.


加载配置并重启 gitlab

# 加载配置
sudo gitlab-ctl reconfigure

# 重启


Package registry

gitlab支持每个仓库独立的Package管理,但是便于package查找,按照mavennpm类别, 分别创建两个仓库管理对应的包。

  1. 创建一个内部的package-registry

  2. 生成一个用于包发布的Group Access Token

    Settings -> Access Token -> 选择scope中的api -> 生成token并复制

  3. 分别创建maven-packagesnpm-packages两个仓库,并记录对应的项目id


maven

settings.xml配置

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


maven 上传 jar 包,上传已有的 jar

$ touch your-library-1.0.jar

$ mvn  deploy:deploy-file \
-Dfile=your-library-1.0.jar \
-Dpackaging=jar \
-DgroupId=com.example \
-DartifactId=your-library \
-Dversion=1.0.0 \
-Durl=https://gitlab.example.com/api/v4/projects/<project_id>/packages/maven \
-DrepositoryId=gitlab-maven

为什么还要配置 settings.xml ? 因为 mvn deploy 命令不方便直接指定用户名和密码。 否认执行 deploy 命令会报 status code 401 的异常

在 Maven 中,-DgroupId-DartifactId-Dversion 是用于定义 Maven 项目的坐标,这些是你需要根据你的项目来定义的。以下是每个参数的具体意义:

  1. *-DgroupId*: 这个参数指定了构件的组 ID,通常使用反向域名命名法来表示组织或项目的唯一标识。例如,如果你的公司域名是 example.com,那么一个常见的组 ID 可能是 com.example
  2. *-DartifactId*: 这个参数指定了构件的工件 ID,通常是项目的名称。例如,如果你的项目叫做 qcsdk,那就可以使用这个作为 artifactId。
  3. *-Dversion*: 这个参数指定了构件的版本号,这帮助你管理和区分项目的不同版本。你可以使用例如 1.0.01.0.12.0.0-SNAPSHOT 之类的版本号格式。


pom.xml 配置

<!-- 获取包仓库配置 -->
<repositories>
  <repository>
    <id>gitlab-maven</id>
    <url><your_endpoint_url></url>
  </repository>
</repositories>
<!-- 发布包仓库配置 -->  
<distributionManagement>
  <repository>
    <id>gitlab-maven</id>
    <url>https://gitlab.example.com/api/v4/projects/<project_id>/packages/maven</url>
  </repository>
  <snapshotRepository>
    <id>gitlab-maven</id>
    <url>https://gitlab.example.com/api/v4/projects/<project_id>/packages/maven</url>
  </snapshotRepository>
</distributionManagement>
  • The id is what you defined in settings.xml.
  • The <your_endpoint_url> depends on which endpoint you choose.
  • Replace gitlab.example.com with your domain name.
Last modified: April 24, 2025

Author

Comments

Write a Reply or Comment

Your email address will not be published.