From 891c32aecc986599417ff2404f83eaff66ee4400 Mon Sep 17 00:00:00 2001
From: Guilhem Bonnefille <guilhem.bonnefille@c-s.fr>
Date: Fri, 10 Dec 2021 08:34:51 +0000
Subject: [PATCH] feat(publish): configure the GitLab Packages registry as
 default Python registry for publish

---
 README.md                      |  8 +++++---
 kicker.json                    |  7 ++++---
 templates/gitlab-ci-python.yml | 11 +++++++++--
 3 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index b75d6e2..25ddb6d 100644
--- a/README.md
+++ b/README.md
@@ -270,13 +270,15 @@ It is bound to the `publish` stage, applies only on git tags and uses the follow
 
 | Name                   | description                                              | default value     |
 | ---------------------- | -------------------------------------------------------- | ----------------- |
-| `TWINE_REPOSITORY_URL` | Where to publish your Python project                     | _none_ (disabled) |
-| `TWINE_USERNAME`       | Username credential to publish to \$TWINE_REPOSITORY_URL | _none_ (disabled) |
-| `TWINE_PASSWORD`       | Password credential to publish to \$TWINE_REPOSITORY_URL | _none_            |
+| `PYTHON_PUBLISH_ENABLED`| Set to `true` to enable the publish job                 | _none_ (disabled) |
+| `TWINE_REPOSITORY_URL` | Where to publish your Python project                     | GitLab Project's Pypi Packages registry |
+| `TWINE_USERNAME`       | Username credential to publish to \$TWINE_REPOSITORY_URL | `gitlab-ci-token` |
+| `TWINE_PASSWORD`       | Password credential to publish to \$TWINE_REPOSITORY_URL | `$CI_JOB_TOKEN` |
 
 More info:
 
 * [Python Packaging User Guide](https://packaging.python.org/)
+* [PyPI packages in the Package Registry](https://docs.gitlab.com/ee/user/packages/pypi_repository/)
 
 If you want to automatically create tag and publish your Python package, please have a look [here](#release-python)
 
diff --git a/kicker.json b/kicker.json
index 1ef8d24..008b443 100644
--- a/kicker.json
+++ b/kicker.json
@@ -163,24 +163,25 @@
       "id": "publish",
       "name": "Publish",
       "description": "Publish your code to a [Twine](https://pypi.org/project/twine/) repository",
+      "enable_with": "PYTHON_PUBLISH_ENABLED",
       "variables": [
         {
           "name": "TWINE_REPOSITORY_URL",
           "type": "url",
           "description": "Twine repository url to publish you python project",
-          "mandatory": true
+          "default": "${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/packages/pypi"
         },
         {
           "name": "TWINE_USERNAME",
           "description": "Twine repository username credential",
           "secret": true,
-          "mandatory": true
+          "default": "gitlab-ci-token"
         },
         {
           "name": "TWINE_PASSWORD",
           "description": "Twine repository password credential",
           "secret": true,
-          "mandatory": true
+          "default": "$CI_JOB_TOKEN"
         }
       ]
     },
diff --git a/templates/gitlab-ci-python.yml b/templates/gitlab-ci-python.yml
index fe5af4a..ca72514 100644
--- a/templates/gitlab-ci-python.yml
+++ b/templates/gitlab-ci-python.yml
@@ -45,6 +45,13 @@ variables:
 
   RELEASE_VERSION_PART: "minor"
 
+  # By default, publish on the Packages registry of the project
+  # https://docs.gitlab.com/ee/user/packages/pypi_repository/#authenticate-with-a-ci-job-token
+  TWINE_REPOSITORY_URL: ${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/packages/pypi
+  TWINE_USERNAME: 'gitlab-ci-token'
+  TWINE_PASSWORD: $CI_JOB_TOKEN
+
+
 .python-scripts: &python-scripts |
   # BEGSCRIPT
   set -e
@@ -610,8 +617,8 @@ py-publish:
     - assert_defined "$TWINE_PASSWORD" 'Missing required env $TWINE_PASSWORD'
     - _publish
   rules:
-    # on tags with $TWINE_USERNAME set
-    - if: '$TWINE_USERNAME && $CI_COMMIT_TAG'
+    # on tags with $PYTHON_PUBLISH_ENABLED set
+    - if: '$PYTHON_PUBLISH_ENABLED == "true" && $CI_COMMIT_TAG'
 
 
 
-- 
GitLab