From f2a23c962a1e3fee28b0083f2e24e640361b0780 Mon Sep 17 00:00:00 2001
From: Otto Richter <git@otto.splvs.net>
Date: Tue, 1 Oct 2024 17:45:38 +0200
Subject: [PATCH] ci: merge e2e workflow into testing.yml

ci: cache frontend build across jobs

ci: ensure caches are saved with zstd

work around https://github.com/actions/cache/issues/1169

ci: require unit tests for remote cacher

- prevents unnecessary runs in case the unit tests already fail
- starts the integration tests about 2 minutes earlier
- should give some overall speedup to the CI run, because the long integration tests are run and finish earlier, and the cacher tests should still usually finish in time
- does not save any computing resources, just provides quicker results when runners are not under high load
---
 .../build-backend/action.yaml                 |  2 +-
 .../workflows-composite/setup-env/action.yaml |  3 +-
 .forgejo/workflows/e2e.yml                    | 37 ------------------
 .forgejo/workflows/testing.yml                | 38 ++++++++++++++++++-
 4 files changed, 40 insertions(+), 40 deletions(-)
 delete mode 100644 .forgejo/workflows/e2e.yml

diff --git a/.forgejo/workflows-composite/build-backend/action.yaml b/.forgejo/workflows-composite/build-backend/action.yaml
index 193ff911e1..ada372b834 100644
--- a/.forgejo/workflows-composite/build-backend/action.yaml
+++ b/.forgejo/workflows-composite/build-backend/action.yaml
@@ -6,7 +6,7 @@ runs:
     - uses: actions/cache@v4
       id: cache-backend
       with:
-        path: '/workspace/forgejo/forgejo/gitea'
+        path: ${{github.workspace}}/gitea
         key: backend-build-${{ github.sha }}
     - if: steps.cache-backend.outputs.cache-hit != 'true'
       run: |
diff --git a/.forgejo/workflows-composite/setup-env/action.yaml b/.forgejo/workflows-composite/setup-env/action.yaml
index 65ffa8341c..d40ee52d20 100644
--- a/.forgejo/workflows-composite/setup-env/action.yaml
+++ b/.forgejo/workflows-composite/setup-env/action.yaml
@@ -4,7 +4,8 @@ runs:
     - name: setup user and permissions
       run: |
         git config --add safe.directory '*'
-        adduser --quiet --comment forgejo --disabled-password forgejo
+        # ignore if the user already exists (like with the playwright image)
+        adduser --quiet --comment forgejo --disabled-password forgejo || true
         chown -R forgejo:forgejo .
     - uses: https://codeberg.org/fnetx/setup-cache-go@b2214eaf6fb44c7e8512c0f462a2c3ec31f86a73
       with:
diff --git a/.forgejo/workflows/e2e.yml b/.forgejo/workflows/e2e.yml
deleted file mode 100644
index 7e27765513..0000000000
--- a/.forgejo/workflows/e2e.yml
+++ /dev/null
@@ -1,37 +0,0 @@
-name: e2e
-
-on:
-  pull_request:
-    paths:
-      - Makefile
-      - playwright.config.js
-      - .forgejo/workflows/e2e.yml
-      - tests/e2e/**
-      - web_src/js/**
-      - web_src/css/form.css
-      - templates/webhook/shared-settings.tmpl
-      - templates/org/team/new.tmpl
-
-jobs:
-  test-e2e:
-    if: ${{ !startsWith(vars.ROLE, 'forgejo-') }}
-    runs-on: docker
-    container:
-      image: 'code.forgejo.org/oci/playwright:latest'
-    steps:
-      - uses: https://code.forgejo.org/actions/checkout@v4
-      - uses: https://code.forgejo.org/actions/setup-go@v5
-        with:
-          go-version-file: "go.mod"
-      - run: |
-          git config --add safe.directory '*'
-          chown -R forgejo:forgejo .
-      - run: |
-          su forgejo -c 'make deps-frontend frontend deps-backend'
-      - run: |
-          su forgejo -c 'make backend'
-      - run: |
-          su forgejo -c 'make generate test-e2e-sqlite'
-        timeout-minutes: 40
-        env:
-          USE_REPO_TEST_DIR: 1
diff --git a/.forgejo/workflows/testing.yml b/.forgejo/workflows/testing.yml
index 960f3d716e..f50b948bdf 100644
--- a/.forgejo/workflows/testing.yml
+++ b/.forgejo/workflows/testing.yml
@@ -36,6 +36,17 @@ jobs:
       - run: make checks-frontend
       - run: make test-frontend-coverage
       - run: make frontend
+      - name: Install zstd for cache saving
+        # works around https://github.com/actions/cache/issues/1169, because the
+        # consuming job has zstd and doesn't restore the cache otherwise
+        run: |
+          apt-get update -qq
+          apt-get -q install -qq -y zstd
+      - name: "Cache frontend build for playwright testing"
+        uses: actions/cache/save@v4
+        with:
+          path: ${{github.workspace}}/public/assets
+          key: frontend-build-${{ github.sha }}
   test-unit:
     if: ${{ !startsWith(vars.ROLE, 'forgejo-') }}
     runs-on: docker
@@ -75,10 +86,35 @@ jobs:
           RACE_ENABLED: 'true'
           TAGS: bindata
           TEST_ELASTICSEARCH_URL: http://elasticsearch:9200
-  test-remote-cacher:
+  test-e2e:
     if: ${{ !startsWith(vars.ROLE, 'forgejo-') }}
     runs-on: docker
     needs: [backend-checks, frontend-checks]
+    container:
+      image: 'code.forgejo.org/oci/playwright:latest'
+    steps:
+      - uses: https://code.forgejo.org/actions/checkout@v4
+      - uses: ./.forgejo/workflows-composite/setup-env
+      - name: "Restore frontend build"
+        uses: actions/cache/restore@v4
+        id: cache-frontend
+        with:
+          path: ${{github.workspace}}/public/assets
+          key: frontend-build-${{ github.sha }}
+      - name: "Build frontend (if not cached)"
+        if: steps.cache-frontend.outputs.cache-hit != 'true'
+        run: |
+          su forgejo -c 'make deps-frontend frontend'
+      - uses: ./.forgejo/workflows-composite/build-backend
+      - run: |
+          su forgejo -c 'make generate test-e2e-sqlite'
+        timeout-minutes: 40
+        env:
+          USE_REPO_TEST_DIR: 1
+  test-remote-cacher:
+    if: ${{ !startsWith(vars.ROLE, 'forgejo-') }}
+    runs-on: docker
+    needs: [backend-checks, frontend-checks, test-unit]
     container:
       image: 'code.forgejo.org/oci/node:20-bookworm'
     strategy: