From b0226393b9b4286c88c40d84465292807941e5a2 Mon Sep 17 00:00:00 2001
From: Anton Joubert <ajoubert@ska.ac.za>
Date: Thu, 2 Jul 2020 07:48:54 +0200
Subject: [PATCH] Change CI to use 'rules' not 'only/except'

This is recommended by gitlab, as the `only/except` clauses are
superseded by `rules`.

Also hoping to prevent publishing the Python package to Nexus when
not tagged.  This has happened recently.

pip `--extra-index-url` no longer needed as container's `pip.conf`
includes this.
---
 .gitlab-ci.yml | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 96670350..9ccf88d0 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -40,20 +40,20 @@ build wheel for publication: # Executed on a tag
     - docker-executor
   script:
     - python3 setup.py egg_info -b+$CI_COMMIT_SHORT_SHA sdist bdist_wheel
-  only:
-    - tags
+  rules:
+    - if: '$CI_COMMIT_TAG'
   artifacts:
     paths:
       - ./dist/
 
-build wheel for development: # Executed on a commit
+build wheel for development: # Executed on non-tagged commit
   stage: build
   tags:
     - docker-executor
   script:
     - python3 setup.py egg_info -b+dev.$CI_COMMIT_SHORT_SHA sdist bdist_wheel
-  except:
-    - tags
+  rules:
+    - if: '$CI_COMMIT_TAG == null || $CI_COMMIT_TAG == ""'
   artifacts:
     paths:
       - ./dist/
@@ -67,7 +67,7 @@ unit tests:
     - docker-executor
   script:
     - echo $(ls -d ./dist/*.whl | grep $CI_COMMIT_SHORT_SHA)
-    - python3 -m pip install -U $(ls -d ./dist/*.whl | grep $CI_COMMIT_SHORT_SHA) --extra-index-url https://nexus.engageska-portugal.pt/repository/pypi/simple
+    - python3 -m pip install -U $(ls -d ./dist/*.whl | grep $CI_COMMIT_SHORT_SHA)
     - make test
     - scripts/validate-metadata.sh
   artifacts:
@@ -83,7 +83,7 @@ linting:
     - docker-executor
   script:
     - echo $(ls -d ./dist/*.whl | grep $CI_COMMIT_SHORT_SHA)
-    - python3 -m pip install -U $(ls -d ./dist/*.whl | grep $CI_COMMIT_SHORT_SHA) --extra-index-url https://nexus.engageska-portugal.pt/repository/pypi/simple
+    - python3 -m pip install -U $(ls -d ./dist/*.whl | grep $CI_COMMIT_SHORT_SHA)
     - make lint
   artifacts:
     paths:
@@ -101,12 +101,9 @@ publish to nexus:
     - scripts/validate-metadata.sh
     - python3 -m pip install twine
     - twine upload --repository-url $PYPI_REPOSITORY_URL dist/*
-  only:
-    variables:
-      # Confirm tag message exists
-      - $CI_COMMIT_MESSAGE =~ /^.+$/
-      # Confirm semantic versioning of tag
-      - $CI_COMMIT_TAG =~ /^((([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)$/
+  rules:
+    # PyPI package release only on tags, and confirm semantic versioning of tag
+    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG =~ /^((([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)$/'
 
 release docker image:
   stage: release
@@ -118,11 +115,11 @@ release docker image:
   script:
     - make build
     - make push
-  when: manual
-  only:
-    refs:
-      - master
-
+  rules:
+    # Images only allow manual run on tags, and confirm semantic versioning of tag
+    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG =~ /^((([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)$/'
+      when: manual
+      allow_failure: true
 
 pages:
   stage: pages
-- 
GitLab