From 945fc8a8c9d298640de2860b15c7162b1ad33684 Mon Sep 17 00:00:00 2001
From: Guilhem Bonnefille <guilhem.bonnefille@c-s.fr>
Date: Mon, 15 Nov 2021 10:46:13 +0000
Subject: [PATCH] feat: move packaging to a separate stage

Move the packaging instruction from publish job to package job.
Conforming to
https://to-be-continuous.gitlab.io/doc/understand/#generic-pipeline-stages
---
 README.md                      | 14 +++++++++++++-
 kicker.json                    | 12 ++++++++++++
 templates/gitlab-ci-python.yml | 21 ++++++++++++++++++++-
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index edb7410..6591f46 100644
--- a/README.md
+++ b/README.md
@@ -227,6 +227,18 @@ It is bound to the `test` stage, and uses the following variables:
 This job outputs a **textual report** in the console, and in case of failure also exports a JSON report in the `reports/`
 directory _(relative to project root dir)_.
 
+### Package jobs
+
+#### `py-package` job
+
+This job is performs a packaging of your Python code.
+
+It is bound to the `package-build` stage, applies only on git tags and uses the following variables:
+
+| Name            | description                                          | default value |
+| --------------- | ---------------------------------------------------- | ------------- |
+| `PYTHON_FORCE_PACKAGE` | Force the packaging even if not on tag related event | _none_        |
+
 ### Publish jobs
 
 #### `py-release` job
@@ -251,7 +263,7 @@ It is bound to the `publish` stage, applies only on master branch and uses the f
 
 #### `py-publish` job
 
-This job is **disabled by default** and performs a packaging and publication of your Python code.
+This job is **disabled by default** and performs a publication of your Python code.
 
 It is bound to the `publish` stage, applies only on git tags and uses the following variables:
 
diff --git a/kicker.json b/kicker.json
index 33ebec6..ae86c1e 100644
--- a/kicker.json
+++ b/kicker.json
@@ -147,6 +147,18 @@
         }
       ]
     },
+    {
+      "id": "package",
+      "name": "package",
+      "description": "Packaging of your Python code",
+      "variables": [
+        {
+          "name": "PYTHON_FORCE_PACKAGE",
+          "description": "Force the packaging even if not on tag related event",
+          "type": "boolean"
+        }
+      ]
+    },
     {
       "id": "publish",
       "name": "Publish",
diff --git a/templates/gitlab-ci-python.yml b/templates/gitlab-ci-python.yml
index 13b523e..15347f6 100644
--- a/templates/gitlab-ci-python.yml
+++ b/templates/gitlab-ci-python.yml
@@ -337,6 +337,7 @@ variables:
 stages:
   - build
   - test
+  - package-build
   - publish
 
 ###############################################################################################
@@ -560,6 +561,25 @@ py-safety:
       allow_failure: true
 
 
+###############################################################################################
+#                                      pakage stage                                           #
+###############################################################################################
+
+# (on tag creation): create packages as artifacts
+py-package:
+  extends: .python-base
+  stage: package-build
+  script:
+    - python setup.py sdist bdist_wheel
+  artifacts:
+    paths:
+      - $PYTHON_PROJECT_DIR/dist/*.tar.gz
+      - $PYTHON_PROJECT_DIR/dist/*.whl
+  rules:
+    # on tags
+    - if: '$CI_COMMIT_TAG'
+    - if: '$PYTHON_FORCE_PACKAGE == "true"'
+
 ###############################################################################################
 #                                      publish stage                                           #
 ###############################################################################################
@@ -573,7 +593,6 @@ py-publish:
     - assert_defined "$TWINE_PASSWORD" 'Missing required env $TWINE_PASSWORD'
     - pip install -U twine setuptools
     - pip list
-    - python setup.py sdist bdist_wheel
     - twine upload --verbose dist/*.tar.gz
     - twine upload --verbose dist/*.whl
   rules:
-- 
GitLab