From 1f1d44595585ded9911724060ecb40e6d7f9a2de Mon Sep 17 00:00:00 2001
From: Pierre-Gilles LEVALLOIS <plevallois@grandlyon.com>
Date: Fri, 30 Sep 2022 16:05:08 +0000
Subject: [PATCH] Cicd

---
 .gitlab-ci.yml            | 79 +++++++++++++++++++++++++++++++++++++++
 Dockerfile                | 10 +++++
 README.md                 |  2 +-
 deployment/job-calqul.yml | 14 +++++++
 scripts/hello-world.py    |  6 +++
 scripts/requirements.txt  |  0
 6 files changed, 110 insertions(+), 1 deletion(-)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 Dockerfile
 create mode 100644 deployment/job-calqul.yml
 create mode 100644 scripts/hello-world.py
 create mode 100644 scripts/requirements.txt

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..ef9a421
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,79 @@
+#
+# Gitlab CI/CD : Build archive and Docker images and deploy on Openshift
+#
+
+variables:
+    TRIGRAMME: test #arb @TODO : Change after when the right namespace is available
+    REPLICA_ENV: 1
+    
+################################################################################
+#    Anchors
+################################################################################
+# Build and push anchor
+.build_and_push_docker_image: &build_and_push_docker_image
+  - docker build --pull --tag "$CI_REGISTRY_IMAGE/$IMAGE_NAME:latest" .
+  - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:latest"
+
+# Docker Authentification anchor
+.docker_login: &docker_login
+  - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
+
+# Deployment preparation anchor
+.prepare_deployment: &prepare_deployment
+  - export NO_PROXY=grandlyon.fr
+  - export KUBECONFIG=$KUBECONFIG_DEV
+  - echo Preparing Deployment on $NAMESPACE 
+  - sed -i "s/__NAMESPACE_ENV__/$NAMESPACE_ENV/g" deployment/job-calqul.yml
+
+# Applying openshift conf anchor
+.apply_confs: &apply_confs
+  - oc delete jobs --selector job-name=calqul-$NAMESPACE_ENV 
+  - oc create -f deployment/job-calqul.yml
+
+################################################################################
+#    CI/CD Stages
+################################################################################
+stages:
+  - Build Calqul
+  - Deploy Calqul
+
+# *********************************************************************************************
+# * BUILD BUILD BUILD BUILD BUILD BUILD BUILD BUILD BUILD BUILD BUILD BUILD BUILD BUILD BUILD *
+# *********************************************************************************************
+
+Build Calqul:
+  stage: Build Calqul
+  variables:
+    DOCKER_TLS_CERTDIR: ""
+    NAMESPACE_ENV: "d01"
+    IMAGE_NAME: calqul-$NAMESPACE_ENV
+  rules:
+    - if: $CI_COMMIT_BRANCH == "develop"
+    - changes: # build image only if sources have changed. 
+      - Dockerfile # Image definition has changed 
+  services:
+    - docker:dind
+  script:
+    - *docker_login
+    - *build_and_push_docker_image
+  tags:
+    - build-push-to-registry
+
+# *********************************************************************************************
+# * DEPLOY DEPLOY DEPLOY DEPLOY DEPLOY DEPLOY DEPLOY DEPLOY DEPLOY DEPLOY DEPLOY DEPLOY DEPLO *
+# *********************************************************************************************
+
+Deploy Calqul:
+  stage: Deploy Calqul
+  rules:
+    - if: $CI_COMMIT_BRANCH == "develop"
+  variables:
+    NAMESPACE_ENV: 'd01'
+    NAMESPACE: "ns-$TRIGRAMME-$NAMESPACE_ENV"
+  before_script:
+    - *prepare_deployment
+  script:
+   - *apply_confs
+  tags:
+    #- ns-arb-d01 @TODO : Change after when the right namespace is available  
+    - ns-test-d01
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..625448a
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,10 @@
+FROM python:3
+
+WORKDIR /usr/src/app
+
+COPY scripts/requirements.txt ./
+RUN pip install --no-cache-dir -r requirements.txt
+
+COPY ./scripts .
+
+CMD [ "python", "./hello-world.py" ]
\ No newline at end of file
diff --git a/README.md b/README.md
index e7249a7..007b39a 100644
--- a/README.md
+++ b/README.md
@@ -131,4 +131,4 @@ Doc :
  - https://docs.openshift.com/container-platform/4.11/nodes/jobs/nodes-nodes-jobs.html
 
 ### Suppression d'un job
- - https://access.redhat.com/documentation/en-us/openshift_container_platform/3.4/html/developer_guide/dev-guide-scheduled-jobs
+ - https://access.redhat.com/documentation/en-us/openshift_container_platform/3.4/html/developer_guide/dev-guide-scheduled-jobs
\ No newline at end of file
diff --git a/deployment/job-calqul.yml b/deployment/job-calqul.yml
new file mode 100644
index 0000000..bd8edab
--- /dev/null
+++ b/deployment/job-calqul.yml
@@ -0,0 +1,14 @@
+apiVersion: batch/v1
+kind: Job
+metadata:
+  name: calqul-__NAMESPACE_ENV__
+spec:
+  activeDeadlineSeconds: 144000 # maximum duration the job can run => 40h 
+  template:         
+    metadata:
+      name: calqul-__NAMESPACE_ENV__
+    spec:
+      containers:
+      - name: calqul-__NAMESPACE_ENV__
+        image: registry.forge.grandlyon.com/erasme/script-recalcul-calque/calqul-__NAMESPACE_ENV__
+      restartPolicy: Never #OnFailure
\ No newline at end of file
diff --git a/scripts/hello-world.py b/scripts/hello-world.py
new file mode 100644
index 0000000..868cb38
--- /dev/null
+++ b/scripts/hello-world.py
@@ -0,0 +1,6 @@
+# docker run -it --rm --name my-running-script -v /c/Users/ICG4702/Documents/developpements/arb/tests:/usr/src/myapp -w /usr/src/myapp python:3 python hello-world.py
+
+import time
+
+print('Hello, World from Docker on OpenShift !')
+time.sleep(5)
\ No newline at end of file
diff --git a/scripts/requirements.txt b/scripts/requirements.txt
new file mode 100644
index 0000000..e69de29
-- 
GitLab