From b198c882fa806caa13637ad53d1d4b0754f5831e Mon Sep 17 00:00:00 2001
From: alvicsam <alvicsam@gmail.com>
Date: Tue, 13 Dec 2022 18:25:45 +0100
Subject: [PATCH] [ci] add job switcher

---
 .gitlab-ci.yml                         | 9 ++++++++-
 scripts/ci/gitlab/pipeline/build.yml   | 3 +++
 scripts/ci/gitlab/pipeline/publish.yml | 1 +
 scripts/ci/gitlab/pipeline/test.yml    | 2 ++
 4 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 25d61cf349615..dcc0cbb7c9693 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -81,8 +81,14 @@ default:
     paths:
       - artifacts/
 
+.job-switcher:
+  before_script:
+    - if echo "$CI_DISABLED_JOBS" | grep -xF "$CI_JOB_NAME"; then echo "The job has been cancelled in CI settings"; exit 0; fi
+
 .kubernetes-env:
   image:                           "${CI_IMAGE}"
+  before_script:
+    - !reference [.job-switcher, before_script]
   tags:
     - kubernetes-parity-build
 
@@ -95,6 +101,7 @@ default:
 
 .pipeline-stopper-vars:
   script:
+    - !reference [.job-switcher, before_script]
     - echo "Collecting env variables for the cancel-pipeline job"
     - echo "FAILED_JOB_URL=${CI_JOB_URL}" > pipeline-stopper.env
     - echo "FAILED_JOB_NAME=${CI_JOB_NAME}" >> pipeline-stopper.env
@@ -110,6 +117,7 @@ default:
   before_script:
     # TODO: remove unset invocation when we'll be free from 'ENV RUSTC_WRAPPER=sccache' & sccache itself in all images
     - unset RUSTC_WRAPPER
+    - !reference [.job-switcher, before_script]
     - !reference [.rust-info-script, script]
     - !reference [.rusty-cachier, before_script]
     - !reference [.pipeline-stopper-vars, script]
@@ -300,7 +308,6 @@ rusty-cachier-notify:
     PR_NUM:                        "${PR_NUM}"
   trigger:
     project:                       "parity/infrastructure/ci_cd/pipeline-stopper"
-    branch:                        "as-improve"
 
 remove-cancel-pipeline-message:
   stage: .post
diff --git a/scripts/ci/gitlab/pipeline/build.yml b/scripts/ci/gitlab/pipeline/build.yml
index 2f8cff7b3ffa6..ba529569d0fc1 100644
--- a/scripts/ci/gitlab/pipeline/build.yml
+++ b/scripts/ci/gitlab/pipeline/build.yml
@@ -62,6 +62,7 @@ build-linux-substrate:
     - job:                         test-linux-stable
       artifacts:                   false
   before_script:
+    - !reference [.job-switcher, before_script]
     - mkdir -p ./artifacts/substrate/
     - !reference [.rusty-cachier, before_script]
     # tldr: we need to checkout the branch HEAD explicitly because of our dynamic versioning approach while building the substrate binary
@@ -94,6 +95,7 @@ build-linux-substrate:
     # this variable gets overriden by "rusty-cachier environment inject", use the value as default
     CARGO_TARGET_DIR:              "$CI_PROJECT_DIR/target"
   before_script:
+    - !reference [.job-switcher, before_script]
     - mkdir -p ./artifacts/subkey
     - !reference [.rusty-cachier, before_script]
   script:
@@ -118,6 +120,7 @@ build-subkey-macos:
   # duplicating before_script & script sections from .build-subkey hidden job
   # to overwrite rusty-cachier integration as it doesn't work on macos
   before_script:
+    - !reference [.job-switcher, before_script]
     - mkdir -p ./artifacts/subkey
   script:
     - cd ./bin/utils/subkey
diff --git a/scripts/ci/gitlab/pipeline/publish.yml b/scripts/ci/gitlab/pipeline/publish.yml
index 381a1bc420ef3..6a0d6d6341304 100644
--- a/scripts/ci/gitlab/pipeline/publish.yml
+++ b/scripts/ci/gitlab/pipeline/publish.yml
@@ -12,6 +12,7 @@
     DOCKERFILE:                    $PRODUCT.Dockerfile
     IMAGE_NAME:                    docker.io/$IMAGE_PATH
   before_script:
+    - !reference [.job-switcher, before_script]
     - cd ./artifacts/$PRODUCT/
     - VERSION="$(cat ./VERSION)"
     - echo "${PRODUCT} version = ${VERSION}"
diff --git a/scripts/ci/gitlab/pipeline/test.yml b/scripts/ci/gitlab/pipeline/test.yml
index ea1240af8bd57..a468a7b04caeb 100644
--- a/scripts/ci/gitlab/pipeline/test.yml
+++ b/scripts/ci/gitlab/pipeline/test.yml
@@ -65,6 +65,7 @@ cargo-check-benches:
   before_script:
     # perform rusty-cachier operations before any further modifications to the git repo to make cargo feel cheated not so much
     - !reference [.rust-info-script, script]
+    - !reference [.job-switcher, before_script]
     - !reference [.rusty-cachier, before_script]
     - !reference [.pipeline-stopper-vars, script]
     # merges in the master branch on PRs
@@ -414,6 +415,7 @@ cargo-check-each-crate-macos:
     - .collect-artifacts
     - .pipeline-stopper-artifacts
   before_script:
+    - !reference [.job-switcher, before_script]
     - !reference [.rust-info-script, script]
     - !reference [.pipeline-stopper-vars, script]
   variables: