From 6b02c8333d263f0851f757a197c909f332f2c1ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9my=20Coutable?= <remy@rymai.me>
Date: Wed, 3 Jul 2024 10:57:58 +0200
Subject: [PATCH] Use the repo from artifacts in frontend jobs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Rémy Coutable <remy@rymai.me>
---
 .gitlab/ci/frontend.gitlab-ci.yml     | 143 ++++++++++++++------------
 .gitlab/ci/global.gitlab-ci.yml       |   3 +-
 .gitlab/ci/rails.gitlab-ci.yml        |   1 -
 .gitlab/ci/rails/shared.gitlab-ci.yml |   5 +-
 4 files changed, 81 insertions(+), 71 deletions(-)

diff --git a/.gitlab/ci/frontend.gitlab-ci.yml b/.gitlab/ci/frontend.gitlab-ci.yml
index 5cc3c9984091..2c644195222b 100644
--- a/.gitlab/ci/frontend.gitlab-ci.yml
+++ b/.gitlab/ci/frontend.gitlab-ci.yml
@@ -1,8 +1,17 @@
+.with-fixtures-needs:
+  needs:
+    - "rspec-all frontend_fixture"
+
+.with-graphql-schema-dump-needs:
+  needs:
+    - "graphql-schema-dump"
+
 .compile-assets-base:
   extends:
     - .default-retry
     - .default-before_script
     - .assets-compile-cache
+    - .repo-from-artifacts
   image: ${REGISTRY_HOST}/${REGISTRY_GROUP}/gitlab-build-images/${BUILD_OS}-${OS_VERSION}-ruby-${RUBY_VERSION}-node-${NODE_VERSION}:rubygems-${RUBYGEMS_VERSION}-git-2.33-lfs-2.9-yarn-1.22-graphicsmagick-1.3.36
   variables:
     SETUP_DB: "false"
@@ -12,7 +21,6 @@
     BROWSERSLIST_IGNORE_OLD_DATA: "true"
     WEBPACK_COMPILE_LOG_PATH: "tmp/webpack-output.log"
   stage: prepare
-  needs: []
   script:
     - yarn_install_script
     - export GITLAB_ASSETS_HASH=$(bin/rake gitlab:assets:hash_sum)
@@ -63,6 +71,7 @@ follow-up:compile-production-assets:
     - compile-production-assets
     - .qa:rules:follow-up-e2e
   needs:
+    - !reference [compile-production-assets, needs]
     - manual:e2e-test-pipeline-generate
 
 compile-test-assets:
@@ -111,9 +120,9 @@ retrieve-frontend-fixtures:
     SETUP_DB: "false"
   extends:
     - .default-retry
+    - .repo-from-artifacts
     - .frontend:rules:default-frontend-jobs
   stage: prepare
-  needs: []
   script:
     - source scripts/utils.sh
     - source scripts/gitlab_component_helpers.sh
@@ -136,8 +145,13 @@ retrieve-frontend-fixtures:
     - .default-before_script
     - .ruby-cache
     - .use-pg14
+    - .repo-from-artifacts
   stage: fixtures
-  needs: ["setup-test-env", "retrieve-tests-metadata", "retrieve-frontend-fixtures"]
+  needs:
+    - "setup-test-env"
+    - "retrieve-tests-metadata"
+    - "retrieve-frontend-fixtures"
+    - !reference [.repo-from-artifacts, needs]
   variables:
     # Don't add `CRYSTALBALL: "false"` here as we're enabling Crystalball for scheduled pipelines (in `.gitlab-ci.yml`), so that we get coverage data
     # for the `frontend fixture RSpec files` that will be added to the Crystalball mapping in `update-tests-metadata`.
@@ -178,18 +192,20 @@ rspec-all frontend_fixture:
 # Uploads FOSS fixtures in the FOSS project.
 upload-frontend-fixtures:
   extends:
-    - .frontend-fixtures-base
+    - .default-retry
+    - .default-before_script
+    - .repo-from-artifacts
     - .frontend:rules:upload-frontend-fixtures
   stage: fixtures
-  needs: ["rspec-all frontend_fixture"]
+  needs:
+    - !reference [.repo-from-artifacts, needs]
+    - !reference [.with-fixtures-needs, needs]
   script:
-    - source scripts/utils.sh
     - source scripts/gitlab_component_helpers.sh
     - export_fixtures_sha_for_upload
     - 'fixtures_archive_doesnt_exist || { echoinfo "INFO: Exiting early as package exists."; exit 0; }'
     - run_timed_command "create_fixtures_package"
     - run_timed_command "upload_fixtures_package"
-  artifacts: {}
 
 graphql-schema-dump:
   variables:
@@ -198,9 +214,9 @@ graphql-schema-dump:
     - .default-retry
     - .ruby-cache
     - .default-before_script
+    - .repo-from-artifacts
     - .frontend:rules:default-frontend-jobs
   stage: fixtures
-  needs: []
   script:
     - bundle exec rake gitlab:graphql:schema:dump
   artifacts:
@@ -214,6 +230,7 @@ graphql-schema-dump:
   extends:
     - .default-retry
     - .yarn-cache
+    - .repo-from-artifacts
   variables:
     # Disable warnings in browserslist which can break on backports
     # https://github.com/browserslist/browserslist/blob/a287ec6/node.js#L367-L384
@@ -225,22 +242,10 @@ graphql-schema-dump:
     - yarn_install_script
   stage: test
 
-.vue3:
-  variables:
-    VUE_VERSION: 3
-    NODE_OPTIONS: --max-old-space-size=7680
-  allow_failure: true
-
-.jest-base:
-  extends: .frontend-test-base
-  script:
-    - run_timed_command "yarn jest:ci:without-fixtures"
-
 jest-build-cache:
   extends:
     - .frontend-test-base
     - .frontend:rules:jest
-  needs: []
   artifacts:
     name: jest-cache
     expire_in: 12h
@@ -258,18 +263,29 @@ jest-build-cache:
     # they exit with 1, so as not to break master and other pipelines.
     exit_codes: 1
 
+.vue3:
+  variables:
+    VUE_VERSION: 3
+    NODE_OPTIONS: --max-old-space-size=7680
+  allow_failure: true
+
+.with-jest-build-cache-vue3-needs:
+  needs:
+    - job: jest-build-cache-vue3
+      optional: true
+
 jest-build-cache-vue3:
   extends:
     - jest-build-cache
     - .frontend:rules:jest-vue3
     - .vue3
 
-jest-with-fixtures:
+jest:
   extends:
-    - .jest-base
+    - .frontend-test-base
     - .frontend:rules:jest
   needs:
-    - "rspec-all frontend_fixture"
+    - !reference [.frontend-test-base, needs]
     - job: jest-build-cache
       optional: true
   artifacts:
@@ -282,47 +298,38 @@ jest-with-fixtures:
       - tmp/tests/frontend/
     reports:
       junit: junit_jest.xml
-  parallel: 2
+  parallel: 11
   script:
-    - run_timed_command "yarn jest:ci:with-fixtures"
+    - run_timed_command "yarn jest:ci:without-fixtures"
 
-jest:
+jest-with-fixtures:
   extends:
-    - .jest-base
+    - jest
     - .frontend:rules:jest
   needs:
-    - job: jest-build-cache
-      optional: true
-  artifacts:
-    name: coverage-frontend
-    expire_in: 31d
-    when: always
-    paths:
-      - coverage-frontend/
-      - junit_jest.xml
-      - tmp/tests/frontend/
-    reports:
-      junit: junit_jest.xml
-  parallel: 11
+    - !reference [jest, needs]
+    - !reference [.with-fixtures-needs, needs]
+  parallel: 2
+  script:
+    - run_timed_command "yarn jest:ci:with-fixtures"
 
-jest-with-fixtures vue3:
+jest vue3:
   extends:
-    - jest-with-fixtures
+    - jest
     - .frontend:rules:jest-vue3
     - .vue3
   needs:
-    - "rspec-all frontend_fixture"
-    - job: jest-build-cache-vue3
-      optional: true
+    - !reference [.repo-from-artifacts, needs]
+    - !reference [.with-jest-build-cache-vue3-needs, needs]
 
-jest vue3:
+jest-with-fixtures vue3:
   extends:
-    - jest
+    - jest-with-fixtures
     - .frontend:rules:jest-vue3
     - .vue3
   needs:
-    - job: jest-build-cache-vue3
-      optional: true
+    - !reference ["jest vue3", needs]
+    - !reference [.with-fixtures-needs, needs]
 
 jest predictive:
   extends:
@@ -350,13 +357,18 @@ jest-integration:
     - .frontend:rules:jest-integration
   script:
     - run_timed_command "yarn jest:integration --ci"
-  needs: ["rspec-all frontend_fixture", "graphql-schema-dump"]
+  needs:
+    - !reference [.frontend-test-base, needs]
+    - !reference [.with-fixtures-needs, needs]
+    - !reference [.with-graphql-schema-dump-needs, needs]
 
 jest-snapshot-vue3:
   extends:
-    - .jest-base
+    - .frontend-test-base
     - .frontend:rules:jest-snapshot-vue3
-  needs: ["rspec-all frontend_fixture"]
+  needs:
+    - !reference [.frontend-test-base, needs]
+    - !reference [.with-fixtures-needs, needs]
   variables:
     VUE_VERSION: 3
     JEST_REPORT: jest-test-report.json
@@ -379,7 +391,6 @@ jest-snapshot-vue3:
         echo 'All snapshot tests passed! Exiting 0...'
         exit 0
       fi
-
   artifacts:
     name: snapshot_tests
     expire_in: 31d
@@ -393,8 +404,10 @@ coverage-frontend:
     - .default-retry
     - .default-utils-before_script
     - .yarn-cache
+    - .repo-from-artifacts
     - .frontend:rules:coverage-frontend
   needs:
+    - !reference [.repo-from-artifacts, needs]
     - job: "jest"
       optional: true
     - job: "jest-with-fixtures"
@@ -425,9 +438,9 @@ webpack-dev-server:
     - .default-retry
     - .default-utils-before_script
     - .yarn-cache
+    - .repo-from-artifacts
     - .frontend:rules:default-frontend-jobs
   stage: test
-  needs: []
   variables:
     WEBPACK_MEMORY_TEST: "true"
     WEBPACK_VENDOR_DLL: "true"
@@ -446,10 +459,12 @@ bundle-size-review:
     - .default-retry
     - .default-utils-before_script
     - .assets-compile-cache
+    - .repo-from-artifacts
     - .frontend:rules:bundle-size-review
   image: ${REGISTRY_HOST}/${REGISTRY_GROUP}/gitlab-build-images:danger
   stage: test
-  needs: []
+  needs:
+    - !reference [.repo-from-artifacts, needs]
   script:
     - yarn_install_script
     - scripts/bundle_size_review
@@ -460,25 +475,21 @@ bundle-size-review:
     paths:
       - bundle-size-review/
 
-.compile-storybook-base:
+compile-storybook:
   extends:
     - .frontend-test-base
     - .storybook-yarn-cache
-  script:
-    - yarn_install_script_storybook
-    - run_timed_command "yarn run storybook:build"
-  needs: ["graphql-schema-dump"]
-
-compile-storybook:
-  extends:
-    - .compile-storybook-base
     - .frontend:rules:compile-storybook
   needs:
-    - !reference [.compile-storybook-base, needs]
-    - job: "rspec-all frontend_fixture"
+    - !reference [.repo-from-artifacts, needs]
+    - !reference [.with-fixtures-needs, needs]
+    - !reference [.with-graphql-schema-dump-needs, needs]
   artifacts:
     name: storybook
     expire_in: 31d
     when: always
     paths:
       - storybook/public
+  script:
+    - yarn_install_script_storybook
+    - run_timed_command "yarn run storybook:build"
diff --git a/.gitlab/ci/global.gitlab-ci.yml b/.gitlab/ci/global.gitlab-ci.yml
index 73a0e7926ecd..761a8a307182 100644
--- a/.gitlab/ci/global.gitlab-ci.yml
+++ b/.gitlab/ci/global.gitlab-ci.yml
@@ -28,7 +28,8 @@
   needs:
     # If the job extending this also defines `needs`, make sure to update
     # its `needs` to include `clone-gitlab-repo` because it'll be overridden.
-    - clone-gitlab-repo
+    - job: clone-gitlab-repo
+      optional: true # Optional so easier to switch in between
 
 .production:
   variables:
diff --git a/.gitlab/ci/rails.gitlab-ci.yml b/.gitlab/ci/rails.gitlab-ci.yml
index 1d0adfa2ade7..b5153ae668c3 100644
--- a/.gitlab/ci/rails.gitlab-ci.yml
+++ b/.gitlab/ci/rails.gitlab-ci.yml
@@ -1367,7 +1367,6 @@ fail-pipeline-early:
   stage: test
   needs:
     - !reference [.rspec-base-needs, needs]
-    - job: "compile-test-assets"
     - job: "detect-previous-failed-tests"
   script:
     - !reference [.base-script, script]
diff --git a/.gitlab/ci/rails/shared.gitlab-ci.yml b/.gitlab/ci/rails/shared.gitlab-ci.yml
index e9c976db7fce..9221edba9545 100644
--- a/.gitlab/ci/rails/shared.gitlab-ci.yml
+++ b/.gitlab/ci/rails/shared.gitlab-ci.yml
@@ -68,10 +68,10 @@ include:
 
 .rspec-base-needs:
   needs:
-    - job: "clone-gitlab-repo"
-      optional: true  # Optional so easier to switch in between
+    - !reference [.repo-from-artifacts, needs]
     - job: "setup-test-env"
     - job: "retrieve-tests-metadata"
+    - job: "compile-test-assets"
 
 .rspec-base:
   extends:
@@ -89,7 +89,6 @@ include:
     EVENT_PROF: "sql.active_record"
   needs:
     - !reference [.rspec-base-needs, needs]
-    - job: "compile-test-assets"
     - job: "detect-tests"
       optional: true
   script:
-- 
GitLab