From f94b0b809dfb7f10f1714bb5b0d432ac0a67b7fa Mon Sep 17 00:00:00 2001 From: Paul Belanger Date: Mon, 21 Aug 2017 15:53:09 -0400 Subject: [PATCH] Add upload-pypi role Create a role to upload python artificats using twine username and password are required, so remove them from defaults. Install twine in the homedir by default if the twine command is not found. Upload wheels if found first, then tarballs. Change-Id: I8857e2983e1175107164536d57e313a5b404bddb Signed-off-by: Paul Belanger --- roles/upload-pypi/README.rst | 32 +++++++++++++++++++++++ roles/upload-pypi/defaults/main.yaml | 4 +++ roles/upload-pypi/tasks/main.yaml | 36 ++++++++++++++++++++++++++ roles/upload-pypi/templates/.pypirc.j2 | 8 ++++++ 4 files changed, 80 insertions(+) create mode 100644 roles/upload-pypi/README.rst create mode 100644 roles/upload-pypi/defaults/main.yaml create mode 100644 roles/upload-pypi/tasks/main.yaml create mode 100644 roles/upload-pypi/templates/.pypirc.j2 diff --git a/roles/upload-pypi/README.rst b/roles/upload-pypi/README.rst new file mode 100644 index 000000000..819a21ceb --- /dev/null +++ b/roles/upload-pypi/README.rst @@ -0,0 +1,32 @@ +Upload python packages to PyPI + +**Role Variables** + +.. zuul:rolevar:: pypi_info + + Complex argument which contains the information about the PyPI + server as well as the authentication information needed. It is + expected that this argument comes from a `Secret`. + + .. zuul:rolevar:: username + + Username to use to log in to PyPI. + + .. zuul:rolevar:: password + + Password to use to log in to PyPI. + + .. zuul:rolevar:: repository + :default: pypi + + Name of the repository to upload to + + .. zuul:rolevar:: repository_url + :default: https://pypi.python.org/pypi + + URL of the PyPI repostory + +.. zuul:rolevar:: pypi_path + :default: src/{{ zuul.project.canonical_name }}/dist + + Path containing artifacts to upload. diff --git a/roles/upload-pypi/defaults/main.yaml b/roles/upload-pypi/defaults/main.yaml new file mode 100644 index 000000000..da44daffa --- /dev/null +++ b/roles/upload-pypi/defaults/main.yaml @@ -0,0 +1,4 @@ +--- +pypi_path: "src/{{ zuul.project.canonical_name }}/dist" +pypi_repository: "{{ pypi_info.repository|default('pypi') }}" +pypi_repository_url: "{{ pypi_info.repository_url|default('https://pypi.python.org/pypi') }}" diff --git a/roles/upload-pypi/tasks/main.yaml b/roles/upload-pypi/tasks/main.yaml new file mode 100644 index 000000000..e73a62e42 --- /dev/null +++ b/roles/upload-pypi/tasks/main.yaml @@ -0,0 +1,36 @@ +- name: Check for twine install + command: which twine + ignore_errors: yes + register: twine_command_which + +- name: Ensure twine is installed + command: pip install --user twine + when: not twine_command_which|succeeded + +- name: Install .pypirc configuration file + template: + dest: ~/.pypirc + mode: 0400 + src: .pypirc.j2 + +- name: Find wheels to upload + find: + paths: "{{ pypi_path }}" + patterns: "*.whl" + register: found_wheels + +- name: Upload wheel with twine before tarballs + command: "twine upload -r {{ pypi_repository }} {{ item }}" + with_items: "{{ found_wheels.files }}" + when: found_wheels.matched|bool + +- name: Find tarballs to upload + find: + paths: "{{ pypi_path }}" + patterns: "*.tar.gz" + register: found_tarballs + +- name: Upload tarballs with twine + command: "twine upload -r {{ pypi_repository }} {{ item }}" + with_items: "{{ found_tarballs.files }}" + when: found_tarballs.matched|bool diff --git a/roles/upload-pypi/templates/.pypirc.j2 b/roles/upload-pypi/templates/.pypirc.j2 new file mode 100644 index 000000000..3cc8ab2a2 --- /dev/null +++ b/roles/upload-pypi/templates/.pypirc.j2 @@ -0,0 +1,8 @@ +[distutils] +index-servers= + {{ pypi_repository }} + +[{{ pypi_repository }}] +repository:{{ pypi_repository_url }} +username:{{ pypi_info.username }} +password:{{ pypi_info.password }}