Help with PyPI upload in pipeline

I’m having trouble getting authentication to PyPI working when uploading. TWINE_USERNAME and TWINE_PASSWORD are defined in the GitLab CI/CD variable settings. I can get this to work using the same commands locally but when using them in the pipeline it returns a 403 Forbidden error. I am using token authentication and setting the username as __token__ as described in the documentation. I know the credentials are good because when I try to use them locally the package uploads fine. Just not sure what else to try. Any help or ideas would be much appreciated.

Relevant gitlab-ci.yml portion:

deploy:
image: python:3.10
stage: deploy
only:
- tags
variables:
TWINE_USERNAME: $TWINE_USERNAME
TWINE_PASSWORD: $TWINE_PASSWORD
script:
- pip install twine build
- python -m build
- python -m twine upload dist/*

Error message:
HTTPError: 403 Forbidden from upload.pypi.org · PyPI
Invalid or non-existent authentication information. See
Help · PyPI for more information.

Managed to find the problem. The pipeline was set to trigger on tags and the tags were not protected. Went into settings and added a wildcard for future tags to be protected. After that the variables that I had set to protected were usable in the pipeline and the deployment succeeded.

What do you mean with ‘the tags were not protected’?