16

I'm trying to set up Poetry to deploy packages to our internal Gitlab Package Registry. According to other sources online the repository ID should be https://gitlab.com/api/v4/projects/<project id>/packages/pypi, but no matter what I try, Poetry returns

[UploadError]
HTTP Error 404: Not Found

Anyone got this working ?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Jakob Vad Nielsen
  • 722
  • 1
  • 7
  • 13

2 Answers2

24

If you are attempting to deploy from GitLab CI, GitLab automatically creates a user and token combination that can be used to authenticate in the CI context under the user gitlab-ci-token and the password in the $CI_JOB_TOKEN variable.

All you need to do that is poetry specific is set the config values for poetry to know the package registry exists, then pass this for authentication. All of which can be done just in the CI config/script.

  script:
    - poetry build
    - poetry config repositories.gitlab "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/packages/pypi"
    - poetry config http-basic.gitlab gitlab-ci-token "$CI_JOB_TOKEN"
    - poetry publish --repository gitlab

If you are deploying from outside of GitLab CI, then you would need that access token and to provide values as used in the script above.

tplusk
  • 899
  • 7
  • 15
  • According to the [GitLab Docs](https://docs.gitlab.com/ee/user/packages/pypi_repository/index.html#authenticate-with-a-ci-job-token) the PyPi gitlab url should be `${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi` – LukeDev Jun 19 '23 at 15:45
17

I actually got this working myself, and the above url is correct. My problem was that I tried to publish to a group (with the group id) and not to an actual project (aka repository).

So here is how to do it:

  1. Add the repository to you poetry.toml

    [virtualenvs]
    in-project = true
    [repositories]
    [repositories.my-gitlab]
    url = "https://gitlab.com/api/v4/projects/<your project id>/packages/pypi"
    
  2. Generate a token in gitlab that can read and write to package repository.

  3. Publish the package

    poetry publish --repository my-gitlab -u <token-username> -p <token-password>
    
    
Jakob Vad Nielsen
  • 722
  • 1
  • 7
  • 13
  • 4
    You could use `https://gitlab.com/api/v4/projects/${env.CI_PROJECT_ID}/packages/pypi` to avoid hardcoding the project ID. – Saksham Nov 02 '21 at 10:04
  • 1
    "My problem was that I tried to publish to a group ..." Then why do groups have a section for packages. if you can't publish to them? – Lawrence I. Siden Apr 15 '22 at 17:56
  • 1
    @LawrenceI.Siden You can access all published packages from nested projects in a group registry without needing to know the particular project the package is in. Note: I've not done this in the API, just from the group package registry on the group webpage, so not sure any other limitations. – LightCC Jun 09 '22 at 16:31