From a914933d18cb2004b3f4ff5a2112f59256ce35da Mon Sep 17 00:00:00 2001
From: Felix Ohms <felix.ohms@stud.h-da.de>
Date: Thu, 8 Jul 2021 09:23:19 +0000
Subject: [PATCH] Resolve "Prevent containerlab:destroy pipeline step from
 running if not needed"

---
 build/ci/.build-container.yml |  2 +-
 build/ci/.code-quality-ci.yml |  2 +-
 build/ci/.containerlab-ci.yml | 29 +++++++++++++++--------------
 3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/build/ci/.build-container.yml b/build/ci/.build-container.yml
index 68bc705e0..2fe12d7f2 100644
--- a/build/ci/.build-container.yml
+++ b/build/ci/.build-container.yml
@@ -40,4 +40,4 @@ build-docker:
    - docker tag $DOCKER_IMAGE_SHA $TAG
    - docker push $TAG
    - docker build --target installer --build-arg GITLAB_USER=$GO_MODULES_USER --build-arg GITLAB_TOKEN=$GO_MODULES_ACCESS_TOKEN --build-arg BUILDARGS=$BUILDARGS -t registry.code.fbi.h-da.de/cocsn/gosdn:testing_${CI_PIPELINE_ID} .
-   - docker push registry.code.fbi.h-da.de/cocsn/gosdn:testing_${CI_PIPELINE_ID}
\ No newline at end of file
+   - docker push registry.code.fbi.h-da.de/cocsn/gosdn:testing_${CI_PIPELINE_ID}
diff --git a/build/ci/.code-quality-ci.yml b/build/ci/.code-quality-ci.yml
index eacbfe4b6..a608dd15e 100644
--- a/build/ci/.code-quality-ci.yml
+++ b/build/ci/.code-quality-ci.yml
@@ -12,4 +12,4 @@ code-quality:
     reports:
       codequality: gl-code-quality-report.json
     paths:
-      - gl-code-quality-report.json
\ No newline at end of file
+      - gl-code-quality-report.json
diff --git a/build/ci/.containerlab-ci.yml b/build/ci/.containerlab-ci.yml
index f968e0a13..9a06ec3b3 100644
--- a/build/ci/.containerlab-ci.yml
+++ b/build/ci/.containerlab-ci.yml
@@ -1,4 +1,3 @@
-
 variables:
   CEOS_CONTAINER_IMAGE: "$CI_REGISTRY_IMAGE/ceos:latest"
   CLAB_INT1_TEMPLATE: "${CI_PROJECT_DIR}/test/containerlab/int01.clab.yml"
@@ -13,17 +12,6 @@ variables:
     - if: $CI_NIGHTLY
 
 
-.containerlab_template: &containerlab_template
-  tags:
-    - shell
-  before_script:
-    - cd ${CLAB_DIR}
-    - echo "$CI_REGISTRY_PASSWORD" | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
-    - echo $DOCKER_IMAGE_SHA
-    - docker pull $DOCKER_IMAGE_SHA
-    - docker pull ${CEOS_CONTAINER_IMAGE}
-
-
 containerlab:template:
   extends: .containerlab_rules
   image: alpine:latest
@@ -50,10 +38,17 @@ containerlab:template:
 
 containerlab:deploy:
   extends:
-    - .containerlab_template
     - .containerlab_rules
   stage: apply
   needs: ["containerlab:template", "build-docker"]
+  tags:
+    - shell
+  before_script:
+    - cd ${CLAB_DIR}
+    - echo "$CI_REGISTRY_PASSWORD" | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
+    - echo $DOCKER_IMAGE_SHA
+    - docker pull $DOCKER_IMAGE_SHA
+    - docker pull ${CEOS_CONTAINER_IMAGE}
   script:
     - sudo containerlab deploy --topo ${CI_PROJECT_DIR}/${CLAB_NAME}.clab.yml --reconfigure
     - echo "GOSDN_HTTP_PORT=$(docker inspect -f '{{ (index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort }}' clab-${CLAB_NAME}-gosdn)" >> ${CI_PROJECT_DIR}/build.env
@@ -66,6 +61,13 @@ containerlab:deploy:
       dotenv: ${CI_PROJECT_DIR}/build.env
 
 containerlab:destroy:
+  rules:
+    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == 'develop')
+      when: always
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      when: always
+    - if: $CI_NIGHTLY
+      when: always
   stage: .post
   tags:
     - shell
@@ -78,4 +80,3 @@ containerlab:destroy:
   allow_failure: true
   dependencies:
     - containerlab:template
-  when: always
-- 
GitLab