From 37b021d31943d789ec2cb8861cd9c33a2a26365b Mon Sep 17 00:00:00 2001
From: Yoann Schneider <yschneider@teklia.com>
Date: Wed, 25 Oct 2023 20:28:43 +0000
Subject: [PATCH] Publish worker preprod

---
 .gitlab-ci.yml | 52 ++++++++++++++++++++++++++++++++++++++++++++++++--
 ci/build.sh    | 21 ++++++--------------
 2 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index fed8ebf..d0fa87c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -3,6 +3,15 @@ stages:
   - build
   - release
 
+# GitLab provides a template to ensure pipelines run only for branches and tags, not for merge requests
+# This prevents duplicate pipelines in merge requests.
+# See https://docs.gitlab.com/ee/ci/troubleshooting.html#job-may-allow-multiple-pipelines-to-run-for-a-single-action
+include:
+  - template: 'Workflows/Branch-Pipelines.gitlab-ci.yml'
+
+variables:
+  VERSION: $CI_COMMIT_SHA
+
 test:
   image: python:3.11
 
@@ -57,8 +66,19 @@ docker-build:
     DOCKER_DRIVER: overlay2
     DOCKER_HOST: tcp://docker:2375/
 
-  except:
-    - schedules
+  rules:
+    # Never run on scheduled pipelines
+    - if: '$CI_PIPELINE_SOURCE == "schedule"'
+      when: never
+    # Use -latest when running on default branch
+    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
+      variables:
+        VERSION: "latest"
+    # Use commit tag when running on tagged commit
+    - if: $CI_COMMIT_TAG
+      variables:
+        VERSION: $CI_COMMIT_TAG
+    - when: on_success
 
   script:
     - ci/build.sh
@@ -67,6 +87,7 @@ release-notes:
   stage: release
   image: registry.gitlab.teklia.com/infra/devops:latest
 
+  # Only run on tags
   only:
     - tags
 
@@ -82,3 +103,30 @@ bump-python-deps:
 
   script:
     - devops python-deps requirements.txt
+
+
+publish-worker:
+  stage: release
+  image: registry.gitlab.teklia.com/arkindex/cli:latest
+
+  script:
+    - arkindex -p "$ARKINDEX_INSTANCE" --gitlab-secure-file arkindex-cli.yaml worker publish "$CI_REGISTRY_IMAGE:$VERSION"
+
+  rules:
+    # Never run on scheduled pipelines
+    - if: '$CI_PIPELINE_SOURCE == "schedule"'
+      when: never
+    # Use -latest when running on default branch
+    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
+      variables:
+        VERSION: "latest"
+    # Use commit tag when running on tagged commit
+    - if: $CI_COMMIT_TAG
+      variables:
+        VERSION: $CI_COMMIT_TAG
+    - when: on_success
+
+  parallel:
+    matrix:
+      - ARKINDEX_INSTANCE:
+        - preprod
diff --git a/ci/build.sh b/ci/build.sh
index f29f50f..0f1d092 100755
--- a/ci/build.sh
+++ b/ci/build.sh
@@ -5,13 +5,9 @@
 # Will automatically login to a registry if CI_REGISTRY, CI_REGISTRY_USER and CI_REGISTRY_PASSWORD are set.
 # Will only push an image if $CI_REGISTRY is set.
 
-if [ -z "$VERSION" ]; then
-	VERSION=${CI_COMMIT_TAG:-latest}
-fi
-
 if [ -z "$VERSION" -o -z "$CI_PROJECT_DIR" -o -z "$CI_REGISTRY_IMAGE" ]; then
-	echo Missing environment variables
-	exit 1
+  echo Missing environment variables
+  exit 1
 fi
 
 IMAGE_TAG="$CI_REGISTRY_IMAGE:$VERSION"
@@ -19,14 +15,9 @@ IMAGE_TAG="$CI_REGISTRY_IMAGE:$VERSION"
 cd $CI_PROJECT_DIR
 docker build -f Dockerfile . -t "$IMAGE_TAG"
 
-# Publish the image on the main branch or on a tag
-if [ "$CI_COMMIT_REF_NAME" = "main" -o -n "$CI_COMMIT_TAG" ]; then
-  if [ -n "$CI_REGISTRY" -a -n "$CI_REGISTRY_USER" -a -n "$CI_REGISTRY_PASSWORD" ]; then
-    echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
-    docker push $IMAGE_TAG
-  else
-    echo "Missing environment variables to log in to the container registry…"
-  fi
+if [ -n "$CI_REGISTRY" -a -n "$CI_REGISTRY_USER" -a -n "$CI_REGISTRY_PASSWORD" ]; then
+  echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
+  docker push $IMAGE_TAG
 else
-  echo "The build was not published to the repository registry (only for main branch or tags)…"
+  echo "Missing environment variables to log in to the container registry…"
 fi
-- 
GitLab