From a02adbdcb482cc85be0253cf68dba20a78d54339 Mon Sep 17 00:00:00 2001
From: Raphael Defosseux <raphael.defosseux@eurecom.fr>
Date: Mon, 28 Mar 2022 12:16:39 +0200
Subject: [PATCH] feat(ci): mechanism to create multiple unique tags per week  
 -- In case we do multiple merges per week   -- Also use the proper week
 numbering (ISO week number, with Monday as first day of week (01..53))

Signed-off-by: Raphael Defosseux <raphael.defosseux@eurecom.fr>
---
 ci-scripts/Jenkinsfile-push-registry |  4 ++-
 ci-scripts/provideUniqueImageTag.py  | 54 ++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 ci-scripts/provideUniqueImageTag.py

diff --git a/ci-scripts/Jenkinsfile-push-registry b/ci-scripts/Jenkinsfile-push-registry
index 91d6136e0a5..f5847ff0aab 100644
--- a/ci-scripts/Jenkinsfile-push-registry
+++ b/ci-scripts/Jenkinsfile-push-registry
@@ -55,7 +55,9 @@ pipeline {
     stage ("Push to DockerHub") {
       steps {
         script {
-          WEEK_TAG = sh returnStdout: true, script: 'date +"%Y.w%U"'
+          WEEK_REF = sh returnStdout: true, script: 'date +"%Y.w%V"'
+          WEEK_REF = WEEK_REF.trim()
+          WEEK_TAG = sh returnStdout: true, script: 'python3 ./ci-scripts/provideUniqueImageTag.py --start_tag ' + WEEK_REF
           WEEK_TAG = WEEK_TAG.trim()
 
           withCredentials([
diff --git a/ci-scripts/provideUniqueImageTag.py b/ci-scripts/provideUniqueImageTag.py
new file mode 100644
index 00000000000..ee8306402fb
--- /dev/null
+++ b/ci-scripts/provideUniqueImageTag.py
@@ -0,0 +1,54 @@
+import argparse
+import os
+import re
+import subprocess
+import sys
+
+AUTH_SERVICE = 'registry.docker.io'
+AUTH_SCOPE   = 'repository:rdefosseoai/oai-enb:pull'
+
+def main() -> None:
+    args = _parse_args()
+
+    cmd = 'curl -fsSL "https://auth.docker.io/token?service=' + AUTH_SERVICE + '&scope=' + AUTH_SCOPE + '" | jq --raw-output ".token"'
+    token = subprocess.check_output(cmd, shell=True, universal_newlines=True)
+    token = str(token).strip()
+    cmd = 'curl -fsSL -H "Authorization: Bearer ' + token + '" "https://index.docker.io/v2/rdefosseoai/oai-enb/tags/list" | jq .'
+    listOfTags = subprocess.check_output(cmd, shell=True, universal_newlines=True)
+
+    foundTag = False
+    for tag in listOfTags.split('\n'):
+        if re.search('"' + args.start_tag + '"', tag) is not None:
+            foundTag = True
+
+    if not foundTag:
+        print (args.start_tag)
+        sys.exit(0)
+
+    proposedVariants = ['a', 'b', 'c', 'd']
+    for variant in proposedVariants:
+        foundTag = False
+        currentVariant = variant
+        for tag in listOfTags.split('\n'):
+            if re.search('"' + args.start_tag + variant + '"', tag) is not None:
+                foundTag = True
+                break
+        if not foundTag:
+            break
+
+    if not foundTag:
+        print (args.start_tag + currentVariant)
+
+def _parse_args() -> argparse.Namespace:
+    parser = argparse.ArgumentParser(description='Provides an unique new image tag for DockerHub')
+
+    parser.add_argument(
+        '--start_tag', '-st',
+        action='store',
+        required=True,
+        help='Proposed Starting Tag',
+    )
+    return parser.parse_args()
+
+if __name__ == '__main__':
+    main()
-- 
GitLab