From 1007a84d19ba1c661ee3000d5187ca56374cbac6 Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.anjos@idiap.ch>
Date: Wed, 21 Sep 2016 16:00:50 +0200
Subject: [PATCH] Add deploy to PyPI step

---
 ci/pypi.sh                          | 71 +++++++++++++++++++++++++++++
 templates/ci-for-cxx-extensions.yml | 37 ++++++++++++++-
 templates/ci-for-python-only.yml    | 48 +++++++++++++++++--
 3 files changed, 149 insertions(+), 7 deletions(-)
 create mode 100755 ci/pypi.sh

diff --git a/ci/pypi.sh b/ci/pypi.sh
new file mode 100755
index 0000000..471937e
--- /dev/null
+++ b/ci/pypi.sh
@@ -0,0 +1,71 @@
+#!/usr/bin/env bash
+# Wed 21 Sep 2016 13:08:05 CEST
+
+# Functions for coloring echo commands
+red_echo() {
+  echo -e "\033[1;31m${@}\033[0m"
+}
+
+green_echo() {
+  echo -e "\033[1;32m${@}\033[0m"
+}
+
+if [ -z "${PYPIUSER}" ] || [ -z "${PYPIPASS}" ]; then
+  red_echo "PYPIUSER and/or PYPIPASS undefined - aborting...";
+  exit 1
+fi
+
+if [ -z "${CI_BUILD_TAG}" ]; then
+  red_echo "CI_BUILD_TAG undefined - can only work with tags, aborting...";
+  exit 1
+fi
+
+PYPISERVER=`[ -z "${PYPISERVER}" ] || https://pypi.python.org`
+TESTSERVER=https://testpypi.python.org
+
+cat <<EOT >> ~/.pypirc
+[distutils]
+index-servers =
+    production
+    staging
+
+[production]
+repository: ${PYPISERVER}
+username: ${PYPIUSER}
+password: ${PYPIPASS}
+
+[staging]
+repository: https://testpypi.python.org/
+username: ${PYPIUSER}
+password: ${PYPIPASS}
+EOT
+chmod 600 ~/.pypirc
+
+setup() {
+  local cmd=python setup.py "${@}"
+  green_echo "[>>] Running ${cmd}..."
+  local status=$?
+  if [ ${status} != 0 ]; then
+    red_echo "Command \"${cmd}\" failed - aborting..."
+    rm -f ~/.pypirc
+    exit ${status}
+  fi
+}
+
+green_echo "[>>] Uploading first to ${TESTSERVER} on behalf of ${PYPIUSER}..."
+setup check sdist --formats zip upload --repository staging
+green_echo "[<<] Test finished, proceeding..."
+
+# if that worked, uploads documentation to pythonhosted
+green_echo "[>>] Uploading documentation on behalf of ${PYPIUSER}..."
+setup upload_docs --upload-dir sphinx
+green_echo "[<<] Documentation uploaded, proceeding..."
+
+# if that worked, uploads source package to the production index
+green_echo "[>>] Uploading package to ${PYPISERVER} on behalf of ${PYPIUSER}..."
+setup check sdist --formats zip upload --repository production
+green_echo "[<<] Package deployed, goodbye."
+
+# cleanup
+rm -f ~/.pypirc
+exit 0
diff --git a/templates/ci-for-cxx-extensions.yml b/templates/ci-for-cxx-extensions.yml
index f5fb06b..e03e345 100644
--- a/templates/ci-for-cxx-extensions.yml
+++ b/templates/ci-for-cxx-extensions.yml
@@ -14,6 +14,7 @@ stages:
   - test
   - docs
   - wheels
+  - deploy
 
 
 # Global variables
@@ -88,9 +89,10 @@ variables:
 # architectures (Linux and Mac OSX 64-bit)
 .wheels_template: &wheels_job
   stage: wheels
+  environment: intranet
   only:
     - master
-    - tags
+    - /^v\d+\.\d+\.\d+([abc]\d*)?$/  # PEP-440 compliant version (tags)
   before_script:
     - curl --silent https://gitlab.idiap.ch/bob/bob.admin/raw/master/ci/wheels.sh > wheels.sh
     - chmod 755 wheels.sh
@@ -102,6 +104,7 @@ variables:
 # Only one real job needs to do this
 .docs_template: &docs_job
   stage: docs
+  environment: intranet
   only:
     - master
   before_script:
@@ -111,6 +114,29 @@ variables:
     - ./docs.sh
 
 
+# Template for the pypi stage - re-install from uploaded wheels
+# Needs to run on a single architecture
+.deploy_template: &deploy_job
+  stage: deploy
+  environment: pypi
+  only:
+    - /^v\d+\.\d+\.\d+([abc]\d*)?$/  # PEP-440 compliant version (tags)
+  except:
+    - branches
+  before_script:
+    - ./bootstrap.sh ${CONDA_FOLDER} ${PYTHON_VER} ${CONDA_PREFIX}
+    - source ${CONDA_FOLDER}/bin/activate ${CONDA_PREFIX}
+    - pip install --use-wheel --no-index --pre dist/*.whl
+    - curl --silent https://gitlab.idiap.ch/bob/bob.admin/raw/master/ci/pypi.sh > pypi.sh
+    - chmod 755 pypi.sh
+  script:
+    - source ${CONDA_FOLDER}/bin/activate ${CONDA_PREFIX}
+    - ./pypi.sh
+  after_script:
+    - rm -rf ~/.pypirc
+    - rm -rf ${CONDA_PREFIX}
+
+
 # 2) Package specific instructions (you may tune this if needed)
 # --------------------------------------------------------------
 
@@ -164,7 +190,7 @@ wheels_linux_34:
     - conda-linux
 
 
-# Linux + Python 3.5: Builds and tests
+# Linux + Python 3.5: Builds, tests and deploys
 build_linux_35:
   <<: *linux_build_job
   variables: &linux_35_build_variables
@@ -195,6 +221,13 @@ docs_linux_35:
   tags:
     - conda-linux
 
+deploy_linux_35:
+  <<: *deploy_job
+  dependencies:
+    - build_linux_35
+  tags:
+    - conda-linux
+
 
 # Mac OSX + Python 2.7: Builds, tests and uploads the wheel
 build_macosx_27:
diff --git a/templates/ci-for-python-only.yml b/templates/ci-for-python-only.yml
index fe750e7..f9935e1 100644
--- a/templates/ci-for-python-only.yml
+++ b/templates/ci-for-python-only.yml
@@ -14,6 +14,7 @@ stages:
   - test
   - docs
   - wheels
+  - deploy
 
 
 # Global variables
@@ -89,22 +90,52 @@ variables:
 # architectures (Linux and Mac OSX 64-bit)
 .wheels_template: &wheels_job
   stage: wheels
+  environment: intranet
   only:
     - master
-    - tags
+    - /^v\d+\.\d+\.\d+([abc]\d*)?$/  # PEP-440 compliant version (tags)
   before_script:
-    - curl --silent https://gitlab.idiap.ch/bob/bob.admin/raw/master/ci/docs.sh > docs.sh
-    - chmod 755 docs.sh
+    - curl --silent https://gitlab.idiap.ch/bob/bob.admin/raw/master/ci/wheels.sh > wheels.sh
+    - chmod 755 wheels.sh
   script:
-    - ./docs.sh
+    - ./wheels.sh
 
 
 # Template for (latest) documentation upload stage
 # Only one real job needs to do this
 .docs_template: &docs_job
   stage: docs
+  environment: intranet
   only:
     - master
+  before_script:
+    - curl --silent https://gitlab.idiap.ch/bob/bob.admin/raw/master/ci/docs.sh > docs.sh
+    - chmod 755 docs.sh
+  script:
+    - ./docs.sh
+
+
+# Template for the pypi stage - re-install from uploaded wheels
+# Needs to run on a single architecture
+.deploy_template: &deploy_job
+  stage: deploy
+  environment: pypi
+  only:
+    - /^v\d+\.\d+\.\d+([abc]\d*)?$/  # PEP-440 compliant version (tags)
+  except:
+    - branches
+  before_script:
+    - ./bootstrap.sh ${CONDA_FOLDER} ${PYTHON_VER} ${CONDA_PREFIX}
+    - source ${CONDA_FOLDER}/bin/activate ${CONDA_PREFIX}
+    - pip install --use-wheel --no-index --pre dist/*.whl
+    - curl --silent https://gitlab.idiap.ch/bob/bob.admin/raw/master/ci/pypi.sh > pypi.sh
+    - chmod 755 pypi.sh
+  script:
+    - source ${CONDA_FOLDER}/bin/activate ${CONDA_PREFIX}
+    - ./pypi.sh
+  after_script:
+    - rm -rf ~/.pypirc
+    - rm -rf ${CONDA_PREFIX}
 
 
 # 2) Package specific instructions (you may tune this if needed)
@@ -155,7 +186,7 @@ test_linux_34:
     - conda-linux
 
 
-# Linux + Python 3.5: Builds, tests, uploads wheel
+# Linux + Python 3.5: Builds, tests, uploads wheel and deploys
 build_linux_35:
   <<: *linux_build_job
   variables: &linux_35_build_variables
@@ -187,6 +218,13 @@ docs_linux_35:
   tags:
     - conda-linux
 
+deploy_linux_35:
+  <<: *deploy_job
+  dependencies:
+    - build_linux_35
+  tags:
+    - conda-linux
+
 
 # Mac OSX + Python 2.7: Builds and tests
 build_macosx_27:
-- 
GitLab