From b999da726d1b269a77cd7309188f8b7d49e72249 Mon Sep 17 00:00:00 2001
From: Ralf Ulrich <ralf.ulrich@kit.edu>
Date: Mon, 6 Jul 2020 19:01:51 +0200
Subject: [PATCH] improve CI sequence,  make more robust.

---
 .gitlab-ci.yml | 395 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 260 insertions(+), 135 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6d4d52be1..18896cd7d 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,5 +1,6 @@
 variables:
   GIT_SSL_NO_VERIFY: "1"
+  GIT_DEPTH: 10
   ## Runtime options for sanitizers
   # (detect_leaks=0 because leak detection doesn't work in CI, but you can
   # try to test with leak detection locally by using detect_leaks=1)
@@ -9,47 +10,18 @@ variables:
 
 # normal multi-step pipeline for each commit
 stages:
-  - config
   - quality
+  - config
   - build
   - test
   - example
+  - install
   - optional
 
-# job/stage to just prepare cmake
-config-u-18_04:
-  image: corsika/devel:u-18.04
-  stage: config
-  tags:
-    - corsika
-  variables:
-    CORSIKA_DATA: "${CI_PROJECT_DIR}/corsika-data/"
-  script:
-    - mkdir build
-    - cd build
-    - cmake .. -DCMAKE_BUILD_TYPE=Debug -DWITH_PYTHIA=ON
-  artifacts:
-    expire_in: 1 day
-    paths:
-       - build
 
-# job/stage to just prepare cmake
-config-clang-8:
-  image: corsika/devel:clang-8
-  stage: config
-  tags:
-    - corsika
-  variables:
-    CORSIKA_DATA: "${CI_BUILDS_DIR}/AirShowerPhysics/corsika/corsika-data/"
-  script:
-    - mkdir build
-    - cd build
-    - cmake .. -DCMAKE_BUILD_TYPE=Debug -DWITH_PYTHIA=ON
-  artifacts:
-    expire_in: 1 day
-    paths:
-       - build
+####### CODE QUALITY CHECK ##############
 
+##########################################################
 check-copyrights:
   image: corsika/devel:u-18.04
   stage: quality
@@ -57,7 +29,13 @@ check-copyrights:
     - corsika
   script:
     - ./do-copyright.py
+  rules:
+    - if: $CI_MERGE_REQUEST_ID
+    - if: $CI_COMMIT_TAG
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+  allow_failure: true
 
+##########################################################
 check-clang-format:
   image: corsika/devel:u-18.04
   before_script:
@@ -67,30 +45,60 @@ check-clang-format:
     - corsika
   script:
     - ./do-clang-format.py --all
+  rules:
+    - if: $CI_MERGE_REQUEST_ID
+    - if: $CI_COMMIT_TAG
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+  allow_failure: true
 
-# normal pipeline for each commit
-build-u-18_04: 
-  image: corsika/devel:u-18.04
-  dependencies:
-    - config-u-18_04
-  stage: build
+
+
+####### CONFIG ##############
+
+##########################################################
+# the generic config template job
+# job/stage to just prepare cmake
+.config:
+  stage: config
   tags:
     - corsika
   variables:
     CORSIKA_DATA: "${CI_PROJECT_DIR}/corsika-data/"
   script:
+    - mkdir build
     - cd build
-    - cmake --build . -- -j4
-  artifacts:
-    expire_in: 1 hour
+    - cmake .. -DCMAKE_BUILD_TYPE=Debug -DWITH_PYTHIA=ON
+  rules:
+    - if: $CI_MERGE_REQUEST_ID
+    - if: $CI_COMMIT_TAG
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+  cache: 
     paths:
-      - build
+      - build/
+    policy: pull-push
 
-# normal pipeline for each commit
-build-clang-8: 
+# config for gcc
+config-u-18_04:
+  extends: .config
+  image: corsika/devel:u-18.04
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
+
+# config for clang
+config-clang-8:
+  extends: .config
   image: corsika/devel:clang-8
-  dependencies:
-    - config-clang-8
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-clang"
+
+
+
+####### BUILD  ##############
+
+##########################################################
+# the generic build template job
+# normal pipeline for each commit
+.build:
   stage: build
   tags:
     - corsika
@@ -99,41 +107,40 @@ build-clang-8:
   script:
     - cd build
     - cmake --build . -- -j4
-  artifacts:
-    expire_in: 1 hour
-    reports:
+  rules:
+    - if: $CI_MERGE_REQUEST_ID
+    - if: $CI_COMMIT_TAG
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+  cache: 
     paths:
-      - build
+      - build/
+    policy: pull-push
 
-# normal pipeline for each commit
-test-u-18_04: 
+# build for gcc
+build-u-18_04:
+  extends: .build
   image: corsika/devel:u-18.04
   dependencies:
-    - build-u-18_04
-  stage: test
-  tags:
-    - corsika
-  variables:
-    CORSIKA_DATA: "${CI_PROJECT_DIR}/corsika-data/"
-  script:
-    - git clone https://gitlab.ikp.kit.edu/AirShowerPhysics/corsika-data.git
-    - cd build
-    - set -o pipefail
-    - ctest -VV | gzip -v -9 > test.log.gz 
-  artifacts:
-    when: always
-    expire_in: 1 week
-    reports:
-      junit:
-        - build/test_outputs/junit*.xml
-    paths:
-      - build/test.log.gz
+    - config-u-18_04
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
 
-# normal pipeline for each commit
-test-clang-8: 
+# build for clang
+build-clang-8: 
+  extends: .build
   image: corsika/devel:clang-8
   dependencies:
-    - build-clang-8
+    - config-clang-8
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-clang"
+
+
+####### TEST  ##############
+
+##########################################################
+# generic test template job
+# normal pipeline for each commit
+.test:
   stage: test
   tags:
     - corsika
@@ -144,20 +151,49 @@ test-clang-8:
     - cd build
     - set -o pipefail
     - ctest -VV | gzip -v -9 > test.log.gz 
+  rules:
+    - if: $CI_MERGE_REQUEST_ID
+    - if: $CI_COMMIT_TAG
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
   artifacts:
     when: always
-    expire_in: 1 week
+    expire_in: 3 days
     reports:
       junit:
         - build/test_outputs/junit*.xml
     paths:
       - build/test.log.gz
+  cache: 
+    paths:
+      - build/
+    policy: pull
 
-# normal pipeline for each commit
-example-u-18_04: 
+# test for gcc
+test-u-18_04:
+  extends: .test
   image: corsika/devel:u-18.04
   dependencies:
     - build-u-18_04
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
+
+# test for clang
+test-clang-8:
+  extends: .test
+  image: corsika/devel:clang-8
+  dependencies:
+    - build-clang-8
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-clang"
+
+
+
+####### EXAMPLE  ##############
+
+##########################################################
+# generic example template job
+# normal pipeline for each commit
+.example:
   stage: example
   tags:
     - corsika
@@ -168,38 +204,89 @@ example-u-18_04:
     - cd build
     - set -o pipefail
     - make run_examples | gzip -v -9 > examples.log.gz
+  rules:
+    - if: $CI_MERGE_REQUEST_ID
+    - if: $CI_COMMIT_TAG
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
   artifacts:
     when: always
-    expire_in: 1 week
+    expire_in: 3 days
     paths:
       - build/examples.log.gz
+  cache: 
+    paths:
+      - build/
+    policy: pull
 
-# normal pipeline for each commit
-example-clang-8: 
+# example for gcc
+example-u-18_04: 
+  extends: .example
+  image: corsika/devel:u-18.04
+  dependencies:
+    - build-u-18_04
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
+
+# example for clang
+example-clang-8:
+  extends: .example
   image: corsika/devel:clang-8
   dependencies:
     - build-clang-8
-  stage: example
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-clang"
+
+
+####### INSTALL  ##############
+
+##########################################################
+# generic install template job
+# make install
+.install:
+  stage: install
   tags:
     - corsika
   variables:
     CORSIKA_DATA: "${CI_PROJECT_DIR}/corsika-data/"
   script:
-    - git clone https://gitlab.ikp.kit.edu/AirShowerPhysics/corsika-data.git
     - cd build
     - set -o pipefail
-    - make run_examples | gzip -v -9 > examples.log.gz
-  artifacts:
-    when: always
-    expire_in: 1 week
+    - make install
+  rules:
+    - if: $CI_MERGE_REQUEST_ID
+    - if: $CI_COMMIT_TAG
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+  cache: 
     paths:
-      - build/examples.log.gz
+      - build/
+    policy: pull
 
-# optional release pipeline for each commit
-release-full-u-18_04: 
+# install for gcc
+install-u-18_04: 
+  extends: .install
   image: corsika/devel:u-18.04
   dependencies:
-    - config-u-18_04
+    - build-u-18_04
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
+
+# install for clang
+install-clang-8:
+  extends: .install
+  image: corsika/devel:clang-8
+  dependencies:
+    - build-clang-8
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-clang"
+
+
+
+####### OPTIONAL ##############
+
+##########################################################
+# generic release template job
+# optional release pipeline for each commit
+.release:
   stage: optional
   tags:
     - corsika
@@ -213,46 +300,52 @@ release-full-u-18_04:
     - set -o pipefail
     - ctest -VV | gzip -v -9 > test.log.gz 
     - make run_examples
+  rules:
+    - if: '$CI_MERGE_REQUEST_LABELS =~ /Ready for code review/' # run on merge requests, if label 'Ready for code review' is set
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      when: manual
+      allow_failure: true
+    - if: $CI_MERGE_REQUEST_ID
+      when: manual
+      allow_failure: true
+    - if: $CI_COMMIT_TAG
+      when: manual
+      allow_failure: true
+  cache: 
+    paths:
+      - build/
+    policy: pull
   artifacts:
     when: always
-    expire_in: 1 week
+    expire_in: 3 days
     reports:
       junit:
         - build/test_outputs/junit*.xml
     paths:
       - build/test.log.gz
-  when: manual    
 
-# optional release pipeline for each commit
+# release for gcc
+release-full-u-18_04:
+  extends: .release
+  image: corsika/devel:u-18.04
+  dependencies:
+    - config-u-18_04
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
+
+# release for clang
 release-full-clang-8: 
+  extends: .release
   image: corsika/devel:clang-8
   dependencies:
     - config-clang-8
-  stage: optional
-  tags:
-    - corsika
-  variables:
-    CORSIKA_DATA: "${CI_PROJECT_DIR}/corsika-data/"
-  script:
-    - git clone https://gitlab.ikp.kit.edu/AirShowerPhysics/corsika-data.git
-    - cd build
-    - cmake .. -DCMAKE_BUILD_TYPE=Release
-    - cmake --build . -- -j4
-    - set -o pipefail
-    - ctest -VV | gzip -v -9 > test.log.gz
-    - make run_examples
-  artifacts:
-    when: always
-    expire_in: 1 week
-    reports:
-      junit:
-        - build/test_outputs/junit*.xml
-    paths:
-      - build/test.log.gz
-  when: manual    
+  cache:
+    key: "${CI_COMMIT_REF_SLUG}-clang"
+
 
-# the coverage generation should either run when manually requested, OR for all changes on the master
-.coverage_job: &coverage_job
+##########################################################
+# the coverage generation should either run when manually requested, OR always on the master
+coverage:
   image: corsika/devel:u-18.04
   dependencies:
     - config-u-18_04
@@ -269,24 +362,27 @@ release-full-clang-8:
     - cmake --build . --target coverage
     - tar czf coverage-report.tar.gz coverage-report
   coverage: '/^.*functions\.+:\s(.*\%)\s/'
+  rules:
+    - if: '$CI_MERGE_REQUEST_LABELS =~ /Ready for code review/' # run on merge requests, if label 'Ready for code review' is set
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+    - if: $CI_MERGE_REQUEST_ID
+      when: manual
+      allow_failure: true
+    - if: $CI_COMMIT_TAG
+      when: manual
+      allow_failure: true
   artifacts:
     expire_in: 1 year
     paths:
       - build/coverage-report.tar.gz
+  cache: 
+    paths:
+      - build/
+    policy: pull
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
 
-coverage: # special manual job to run after normal pipeline on branch finished 
-  <<: *coverage_job
-  when: manual    
-  except:
-    refs:
-      - master
-
-coverage_master: # special job to run on master banch changes
-  <<: *coverage_job
-  only:
-    refs:
-      - master
 
+##########################################################
 documentation:
   image: corsika/devel:u-18.04
   dependencies:
@@ -302,13 +398,29 @@ documentation:
     - mkdir .public
     - cp -r Documentation/Doxygen/html .public/
     - mv .public ../public
+  rules:
+    - if: '$CI_MERGE_REQUEST_LABELS =~ /Ready for code review/' # run on merge requests, if label 'Ready for code review' is set
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      when: manual
+      allow_failure: true
+    - if: $CI_MERGE_REQUEST_ID
+      when: manual
+      allow_failure: true
+    - if: $CI_COMMIT_TAG
+      when: manual
+      allow_failure: true
   artifacts:
     expire_in: 3 weeks
     paths:
       - public
-  when: manual
-  allow_failure: true
+  cache: 
+    paths:
+      - build/
+    policy: pull
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
+
 
+##########################################################
 sanity:
   image: corsika/devel:u-18.04
   dependencies:
@@ -322,6 +434,19 @@ sanity:
     - cd build
     - cmake .. -DWITH_CORSIKA_SANITIZERS_ENABLED=ON
     - cmake --build . -- -j4
-  when: manual
-  allow_failure: true
-
+  rules:
+    - if: '$CI_MERGE_REQUEST_LABELS =~ /Ready for code review/' # run on merge requests, if label 'Ready for code review' is set
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      when: manual
+      allow_failure: true
+    - if: $CI_MERGE_REQUEST_ID
+      when: manual
+      allow_failure: true
+    - if: $CI_COMMIT_TAG
+      when: manual
+      allow_failure: true
+  cache: 
+    paths:
+      - build/
+    policy: pull
+    key: "${CI_COMMIT_REF_SLUG}-gcc"
-- 
GitLab