How to upload pypi packages to Artifactory using twine
Introduction
The Twine CLI is a tool used for securely uploading Python distributions to package repositories like PyPI or other binary repositories. It simplifies the process of distributing Python packages by handling authentication, package uploads, and interactions with repository servers.
Here's how to use it with Artifactory.
Direct CLI Arguments
You can upload files to a PyPi repository using a direct "twine upload" command:
This method is simple, but not ideal for security, as your password will appear in your shell history. If you want to avoid that, consider the next method.
This method is more secure than the first one because your credentials are not exposed in the command line or shell history.
twine upload --repository-url http://localhost:8081/artifactory/api/pypi/pypi-local/ -u admin -p password ./infra_pypipkg_test-0.0.1-py3-none-any.whl
This method is simple, but not ideal for security, as your password will appear in your shell history. If you want to avoid that, consider the next method.
Environment Variables
You can set two environment variables to upload packages a little more securely:export TWINE_USERNAME=admin export TWINE_PASSWORD=password twine upload --repository-url http://localhost:8081/artifactory/api/pypi/pypi-local/ ./infra_pypipkg_test-0.0.1-py3-none-any.whl
This method is more secure than the first one because your credentials are not exposed in the command line or shell history.
Using a .pypirc file:
In order to install twine run:
$ pip install twine
A secure method that does not use either environment variables or direct commands is to use a .pypirc file. Create a .pypirc file in the root like the following:
[distutils] index-servers = local [local] repository: http://<USER>:<PASS>@localhost:8081/artifactory/api/pypi/pypi
Artifactory's PyPi Set Me Up menu can help you create this file.
The twine upload command for this method is:
$ twine upload -r local <PATH_TO_THE_FILES> --config-file ~/.pypirc