From ceb9ec288e36af95926032aa42eb27c533b94bec Mon Sep 17 00:00:00 2001 From: Dave Reid Date: Sun, 15 Nov 2026 16:57:27 -0600 Subject: [PATCH] feat: add a cacheDependencies input Caches Composer, npm and Yarn downloads with actions/cache, keyed on the lock files. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/main.yml | 41 +++++++++++++ README.md | 16 +++++ action.yml | 34 +++++++++++ tests/fixtures/ddevProj2/.ddev/config.yaml | 4 ++ tests/fixtures/ddevProj2/composer.json | 7 +++ tests/fixtures/ddevProj2/composer.lock | 70 ++++++++++++++++++++++ tests/fixtures/ddevProj2/package-lock.json | 22 +++++++ tests/fixtures/ddevProj2/package.json | 7 +++ 8 files changed, 201 insertions(+) create mode 100644 tests/fixtures/ddevProj2/.ddev/config.yaml create mode 100644 tests/fixtures/ddevProj2/composer.json create mode 100644 tests/fixtures/ddevProj2/composer.lock create mode 100644 tests/fixtures/ddevProj2/package-lock.json create mode 100644 tests/fixtures/ddevProj2/package.json diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7e21108..bbf6b0e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -76,3 +76,44 @@ jobs: - name: "index.html: expected output" run: test 'index.html test output' = "$(curl --silent https://setup-ddev-proj1.test)" + + cache-save: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v5 + with: + persist-credentials: false + - uses: ./ + with: + ddevDir: tests/fixtures/ddevProj2 + cacheDependencies: true + + - name: Install dependencies to fill the cache + working-directory: tests/fixtures/ddevProj2 + run: | + ddev composer install + ddev npm ci + + cache-restore: + needs: cache-save + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v5 + with: + persist-credentials: false + - uses: ./ + id: ddev + with: + ddevDir: tests/fixtures/ddevProj2 + cacheDependencies: true + + - name: Cache restored + env: + CACHE_HIT: ${{ steps.ddev.outputs.cache-hit }} + run: test "$CACHE_HIT" = 'true' + + - name: Install dependencies without network access + working-directory: tests/fixtures/ddevProj2 + run: | + ddev exec 'COMPOSER_DISABLE_NETWORK=1 composer install' + ddev npm ci --offline diff --git a/README.md b/README.md index 260d86f..4594185 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,22 @@ Example with custom script source: version: "v1.24.8" ``` +### cacheDependencies + +Caches Composer, npm and Yarn downloads between workflow runs with [`actions/cache`](https://github.com/actions/cache), keyed on `composer.lock`, `package-lock.json` and `yarn.lock`. The caches are stored in `.ddev/.setup-ddev-cache` in the project, and a generated `.ddev/config.zz-setup-ddev-cache.yaml` points the package managers at them. The `cache-hit` output is `true` when the lock files matched a saved cache exactly. + +default: `false` + +```yaml + - uses: ddev/github-action-setup-ddev@v1 + with: + cacheDependencies: true + - run: ddev composer install + - run: ddev npm ci +``` + +Pull requests can only restore caches saved on their own branch or the default branch, so run the workflow on pushes to the default branch too. Caching DDEV's Docker images doesn't help: restoring them takes longer than pulling them ([#15](https://github.com/ddev/github-action-setup-ddev/issues/15)). + ## Common recipes ### SSH keys diff --git a/action.yml b/action.yml index fc1b60f..0c23e54 100644 --- a/action.yml +++ b/action.yml @@ -23,6 +23,16 @@ inputs: required: true default: 'https://ddev.com/install.sh' + cacheDependencies: + description: 'Cache Composer, npm and Yarn downloads between workflow runs, keyed on the lock files.' + required: false + default: 'false' + +outputs: + cache-hit: + description: 'Whether cacheDependencies restored an exact match for the lock files.' + value: ${{ steps.cache.outputs.cache-hit }} + runs: using: 'composite' steps: @@ -57,6 +67,30 @@ runs: /tmp/install_ddev.sh ${VERSION_ARG} ddev config global --instrumentation-opt-in=false --omit-containers=ddev-ssh-agent + # DDEV's own caches live in a Docker volume that actions/cache can't reach, + # so point the package managers at a directory inside the project instead. + - name: Point package caches at the project + if: inputs.cacheDependencies == 'true' + shell: bash + working-directory: ${{ inputs.ddevDir }} + run: | + mkdir -p .ddev/.setup-ddev-cache + cat > .ddev/config.zz-setup-ddev-cache.yaml <<'EOF' + web_environment: + - COMPOSER_CACHE_DIR=/var/www/html/.ddev/.setup-ddev-cache/composer + - npm_config_cache=/var/www/html/.ddev/.setup-ddev-cache/npm + - YARN_CACHE_FOLDER=/var/www/html/.ddev/.setup-ddev-cache/yarn + EOF + + - name: Cache dependencies + id: cache + if: inputs.cacheDependencies == 'true' + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ inputs.ddevDir }}/.ddev/.setup-ddev-cache + key: setup-ddev-deps-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles(format('{0}/composer.lock', inputs.ddevDir), format('{0}/package-lock.json', inputs.ddevDir), format('{0}/yarn.lock', inputs.ddevDir)) }} + restore-keys: setup-ddev-deps-${{ runner.os }}-${{ runner.arch }}- + - name: Start DDEV project shell: bash working-directory: ${{ inputs.ddevDir }} diff --git a/tests/fixtures/ddevProj2/.ddev/config.yaml b/tests/fixtures/ddevProj2/.ddev/config.yaml new file mode 100644 index 0000000..bc6ace4 --- /dev/null +++ b/tests/fixtures/ddevProj2/.ddev/config.yaml @@ -0,0 +1,4 @@ +name: setup-ddev-proj2 +type: php +docroot: "" +omit_containers: [db] diff --git a/tests/fixtures/ddevProj2/composer.json b/tests/fixtures/ddevProj2/composer.json new file mode 100644 index 0000000..a0dcd72 --- /dev/null +++ b/tests/fixtures/ddevProj2/composer.json @@ -0,0 +1,7 @@ +{ + "name": "ddev/setup-ddev-cache-test", + "type": "project", + "require": { + "psr/log": "3.0.2" + } +} diff --git a/tests/fixtures/ddevProj2/composer.lock b/tests/fixtures/ddevProj2/composer.lock new file mode 100644 index 0000000..809d42f --- /dev/null +++ b/tests/fixtures/ddevProj2/composer.lock @@ -0,0 +1,70 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "60a1d3660aabfd5fdcf84359dd135448", + "packages": [ + { + "name": "psr/log", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "support": { + "source": "https://github.com/php-fig/log/tree/3.0.2" + }, + "time": "2024-09-11T13:17:53+00:00", + "published-time": "2024-09-11T13:21:14+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": {}, + "prefer-stable": false, + "prefer-lowest": false, + "platform": {}, + "platform-dev": {}, + "plugin-api-version": "2.11.0" +} diff --git a/tests/fixtures/ddevProj2/package-lock.json b/tests/fixtures/ddevProj2/package-lock.json new file mode 100644 index 0000000..a960b2a --- /dev/null +++ b/tests/fixtures/ddevProj2/package-lock.json @@ -0,0 +1,22 @@ +{ + "name": "setup-ddev-cache-test", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "setup-ddev-cache-test", + "dependencies": { + "is-number": "7.0.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "license": "MIT", + "engines": { + "node": ">=0.12.0" + } + } + } +} diff --git a/tests/fixtures/ddevProj2/package.json b/tests/fixtures/ddevProj2/package.json new file mode 100644 index 0000000..b84e36a --- /dev/null +++ b/tests/fixtures/ddevProj2/package.json @@ -0,0 +1,7 @@ +{ + "name": "setup-ddev-cache-test", + "private": true, + "dependencies": { + "is-number": "7.0.0" + } +}