Skip to main content

Managing Package Registry

1. Publishing npm Packages

Property Value Description
package.name "my-npm-package" Name of the npm package
package.version "1.0.0" Version of the package
package.publishConfig.registry "https://registry.npmjs.org/" Registry URL to publish to

Example: Publishing to npm registry

npm publish --registry https://registry.npmjs.org/

Example: Using GitLab CI to publish

publish-npm:
  stage: deploy
  script:
    - npm config set registry https://registry.npmjs.org/
    - npm publish
  only:
    - main

2. Publishing Maven Artifacts

Property Description Example
maven_artifact_id Artifact ID for the Maven package my-app
maven_group_id Group ID for the Maven package com.example
maven_version Version of the Maven package 1.0.0
maven_repository_url URL to the Maven repository (e.g., GitLab Package Registry) https://gitlab.example.com/api/v4/projects/1234/packages/maven

Example: Publishing to GitLab Package Registry

mvn deploy -DaltDeploymentRepository=gitlab-maven:<REPO_URL>/com/example/my-app/1.0.0
Note: Ensure that the groupId, artifactId, and version match the project's Maven configuration.
Warning: Make sure the GitLab CI job has proper CI_REGISTRY_USER and CI_REGISTRY_PASSWORD credentials to access the package registry.

3. Publishing Python Packages

Example: Publishing to GitLab Package Registry

pip install twine
python setup.py sdist bdist_wheel
twine upload --repository-url https://gitlab.example.com/api/v4/projects/1234/packages/pypi dist/*
Field Description Notes
repository-url URL to GitLab Package Registry endpoint Must include project ID and package type
username GitLab username or CI job token Required for authentication
password Personal access token or CI job token Must have package registry permissions
Note: Ensure your project has the packages scope enabled in CI job token permissions.
Warning: GitLab Package Registry for Python packages is limited to pypi format. Use twine for publishing.

Key Steps

  • Create Python package distribution files (sdist, bdist_wheel)
  • Install twine for secure publishing
  • Use GitLab's API endpoint with CI job token or personal access token
  • Upload to https://gitlab.example.com/api/v4/projects/<project_id>/packages/pypi

4. Publishing Go Modules

Property Value Description
go.mod module example.com/myproject Defines the module path in Go
go.sum example.com/myproject v1.0.0 Checksums for dependencies

Example: Publishing to GitLab Package Registry

go mod init example.com/myproject
go get example.com/myproject
go mod tidy
git add .
git commit -m "Initialize Go module"
git push origin main

# Configure GitLab CI to publish

Example: GitLab CI Configuration

publish-go-module:
  stage: deploy
  script:
    - go install github.com/go-task/go-task/cmd/task@latest
    - go mod tidy
    - |
      if [ "$CI_COMMIT_BRANCH" = "main" ]; then
        go build -o ./bin/myapp
        # Publish to GitLab Package Registry
        export GOPROXY=direct
        go mod vendor
      fi
  only:
    - main
Note: Ensure your .gitlab-ci.yml includes proper permissions and environment variables for publishing to GitLab Package Registry.
Warning: Go modules must be versioned with semantic versioning (v1.0.0) to be published successfully.

5. Using Generic Package Registry

Feature Description Usage
Package Type Generic packages support any type of package (e.g., .tar.gz, .zip, .deb, .rpm) Upload any file as a generic package
Storage Limit Up to 10GB per package Configurable in project settings
Access Control Read and write access controlled via project members Set permissions in project settings

Example: Uploading a Generic Package

curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
     --upload-file ./my-package.tar.gz \
     "https://gitlab.example.com/api/v4/projects/123/packages/generic/my-package/latest/my-package.tar.gz"

Example: Downloading a Generic Package

curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" \
     "https://gitlab.example.com/api/v4/projects/123/packages/generic/my-package/latest/my-package.tar.gz" \
     --output my-package.tar.gz
Note: The package name and version must be URL-encoded when used in the API endpoint.

6. Publishing Composer Packages

Property Value Description
package.name vendor/package-name Name of the Composer package
package.version 1.0.0 Version of the package
package.description Package description Description of the package
package.type library Type of package (e.g., library, project)
package.authors [{name: "Author Name", email: "author@example.com"}] List of package authors
package.require {"php": ">=7.4"} Dependencies required by the package

Example: Publishing to GitLab Package Registry

composer config repositories.gitlab git+https://gitlab.example.com/api/v4/projects/12345/packages/composer
composer config http-basic.gitlab.com <username> <token>
composer require vendor/package-name
Note: Ensure the composer.json file is properly configured with all required metadata before publishing.
Warning: The GitLab Package Registry requires a valid composer.json file with proper versioning and metadata.

7. Publishing NuGet Packages

Property Value Description
nuget.config <configuration><packageSources><clear /><add key="gitlab" value="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/nuget/index.json" /></packageSources></configuration> Configuration file to register GitLab as a NuGet source
nuget push nuget push -Source gitlab -ApiKey ${CI_JOB_TOKEN} -Version 1.0.0 package.nupkg Command to publish NuGet package to GitLab registry

Example: Publishing a NuGet Package

nuget push -Source gitlab -ApiKey ${CI_JOB_TOKEN} -Version 1.0.0 package.nupkg
Note: Ensure CI_JOB_TOKEN has appropriate permissions to publish packages.
Warning: NuGet package versions must be unique; overwriting existing packages is not allowed.

8. Using Conan Packages

Property Description Example
conanfile.py File defining package requirements and build settings
from conan import ConanFile

class MyLib(ConanFile):
    name = "mylib"
    version = "1.0"
    requires = "zlib/1.2.11"
conan install Command to download and install dependencies conan install . --build=missing
conan create Builds and packages the project conan create . mylib/1.0@user/channel

Example: Using Conan in GitLab CI

stages:
  - build

build_job:
  stage: build
  image: python:3.9
  script:
    - pip install conan
    - conan install . --build=missing
    - conan create . mylib/1.0@user/channel
Note: Ensure the Conan registry is configured in your GitLab CI environment with proper credentials.

9. Configuring Package Registry Authentication

Setting Description Value/Example
CI_REGISTRY_USER Username for GitLab Package Registry authentication gitlab-ci-token
CI_REGISTRY_PASSWORD Password or personal access token for GitLab Package Registry $CI_JOB_TOKEN or $GITLAB_USER_TOKEN
CI_REGISTRY Registry URL for GitLab Package Registry registry.gitlab.com

Example: Using GitLab CI/CD with Package Registry

docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
docker push $CI_REGISTRY_IMAGE
Note: Ensure CI_REGISTRY_USER and CI_REGISTRY_PASSWORD are properly set in your CI/CD variables.
Warning: Never expose CI_REGISTRY_PASSWORD in logs or public repositories.

10. Managing Package Versions and Cleanup

Version Release Date Notes
v1.0.0 2023-01-15 Initial release with basic package management features
v1.2.0 2023-04-20 Added support for package cleanup and version pinning
v2.0.0 2024-01-10 NEW Major update with improved cleanup automation

Example: Configuring Package Cleanup

stages:
  - build
  - cleanup

cleanup_packages:
  stage: cleanup
  script:
    - echo "Cleaning up old package versions"
    - gitlab-cli package cleanup --keep-last 5
  only:
    - main
Note: The gitlab-cli package cleanup command can be configured to keep a specified number of recent versions while removing older ones.
Warning: Ensure that package cleanup policies do not inadvertently remove packages needed for production deployments.
Option Description Default Value
--keep-last Number of latest package versions to retain 3
--dry-run Simulate cleanup without making changes false
--force Force cleanup without confirmation false

Was this article helpful?