GitLab Package Registry#

You can also publish your distribution packages in the package registry of your GitLab project and use them with both Pip and twine.

Authentication#

To authenticate to the GitLab Package Registry, you can use one of the following methods:

… with a personal access token#

To authenticate yourself with a personal access token, you can add the following to the ~/.pypirc file, for example:

[distutils]
index-servers=
    gitlab

[gitlab]
repository = https://ce.cusy.io/api/v4/projects/{PROJECT_ID}/packages/pypi
username = {NAME}
password = {YOUR_PERSONAL_ACCESS_TOKEN}

… with a deploy token#

[distutils]
index-servers =
    gitlab

[gitlab]
repository = https://ce.cusy.io/api/v4/projects/{PROJECT_ID}/packages/pypi
username = {DEPLOY_TOKEN_USERNAME}
password = {DEPLOY_TOKEN}

… with a job token#

image: python:latest

run:
  script:
    - pip install build twine
    - python -m build
    - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python -m twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/*

… for access to packages within a group#

Use the GROUP_URL instead of the PROJECT_ID.

Publishing the distribution package#

You can publish your package with the help of twine:

python3 -m twine upload --repository gitlab dist/*

Note

If you try to publish a package that already exists with the same name and version, you will get the error 400 Bad Request; you will have to delete the existing package first.

Installing the package#

You can install the latest version of your package for example with

pip install --index-url https://{NAME}:{PERSONAL_ACCESS_TOKEN}@ce.cusy.io/api/v4/projects/{PROJECT_ID}/packages/pypi/simple --no-deps {PACKAGE_NAME}

… or from the group level with

pip install --index-url https://{NAME}:{PERSONAL_ACCESS_TOKEN}@ce.cusy.io/api/v4/groups/{GROUP_ID}/-/packages/pypi/simple --no-deps {PACKAGE_NAME}

… or in the requirements.txt file with

--extra-index-url https://ce.cusy.io/api/v4/projects/{PROJECT_ID}/packages/pypi/simple {PACKAGE_NAME}