Skip to content

Commit

Permalink
fix(continuous-integration): support jest cache
Browse files Browse the repository at this point in the history
Signed-off-by: Emilien Escalle <emilien.escalle@escemi.com>
  • Loading branch information
neilime committed Mar 25, 2024
1 parent 8b1da56 commit 1f45da4
Show file tree
Hide file tree
Showing 7 changed files with 3,182 additions and 613 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/__test-action-has-installed-dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ jobs:
uses: ./actions/has-installed-dependencies
with:
dependencies: |
nx
gatsby
storybook
jest
nx
prettier
storybook
unknown
- name: Check "has-installed-dependencies" outputs
Expand All @@ -42,10 +43,11 @@ jobs:
}
const expectedResult = {
nx: true,
gatsby: true,
storybook: true,
jest: true,
nx: true,
prettier: true,
storybook: true,
unknown: false,
};
Expand Down
6 changes: 5 additions & 1 deletion actions/dependencies-cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ runs:
with:
dependencies: ${{ inputs.dependencies }}

- id: get-package-manager
if: inputs.package-manager == ''
uses: ./self-setup-node-action/get-package-manager

- name: ♻️ NX cache
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).nx == true
uses: actions/cache@v4.0.2
Expand Down Expand Up @@ -66,7 +70,7 @@ runs:
if: fromJson(steps.has-installed-dependencies.outputs.installed-dependencies).jest == true
shell: bash
run: |
JEST_CACHE_DIR=$(${{ steps.setup-node.outputs.run-script-command }} jest --showConfig | grep -oP '(?<="cacheDirectory": ")[^"]+(?=")')
JEST_CACHE_DIR=$(${{ steps.get-package-manager.outputs.run-script-command }} jest --showConfig | grep -oP '(?<="cacheDirectory": ")[^"]+(?=")')
echo "dir=$JEST_CACHE_DIR" >> "$GITHUB_OUTPUT"
- name: ♻️ Test cache
Expand Down
17 changes: 16 additions & 1 deletion actions/get-package-manager/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ branding:

outputs:
package-manager:
description: "The package manager used"
description: "The package manager used."
value: ${{ steps.get-package-manager.outputs.package-manager }}
cache-dependency-path:
description: "The path to the dependency file for cache management."
value: ${{ steps.get-package-manager.outputs.cache-dependency-path }}
install-command:
description: "The command to install dependencies."
value: ${{ steps.get-package-manager.outputs.install-command }}
run-script-command:
description: "The command to run a script in the package.json file."
value: ${{ steps.get-package-manager.outputs.run-script-command }}

runs:
using: "composite"
Expand All @@ -19,12 +28,18 @@ runs:
# Yarn
if [[ -f "yarn.lock" ]]; then
echo "package-manager=yarn" >> $GITHUB_OUTPUT
echo "cache-dependency-path=**/yarn.lock" >> $GITHUB_OUTPUT
echo "install-command=yarn install --frozen-lockfile" >> $GITHUB_OUTPUT
echo "run-script-command=yarn" >> $GITHUB_OUTPUT
exit 0
fi
# NPM
if [[ -f "package-lock.json" ]]; then
echo "package-manager=npm" >> $GITHUB_OUTPUT
echo "cache-dependency-path=**/package-lock.json" >> $GITHUB_OUTPUT
echo "install-command=npm ci" >> $GITHUB_OUTPUT
echo "run-script-command=npm run" >> $GITHUB_OUTPUT
exit 0
fi
Expand Down
28 changes: 5 additions & 23 deletions actions/setup-node/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ inputs:
outputs:
run-script-command:
description: "The command to run a script in the package.json file"
value: ${{ steps.prepare-variables.outputs.run-script-command }}
value: ${{ steps.get-package-manager.outputs.run-script-command }}
runs:
using: "composite"
steps:
Expand All @@ -26,27 +26,9 @@ runs:
if: inputs.package-manager == ''
uses: ./self-setup-node-action/get-package-manager

- id: prepare-variables
- id: get-node-version-file
shell: bash
run: |
PACKAGE_MANAGER="${{ steps.get-package-manager.outputs.package-manager }}"
# Yarn
if [[ "$PACKAGE_MANAGER" == "yarn" ]]; then
echo "cache-dependency-path=**/yarn.lock" >> $GITHUB_OUTPUT
echo "install-command=yarn install --frozen-lockfile" >> $GITHUB_OUTPUT
echo "run-script-command=yarn" >> $GITHUB_OUTPUT
exit 0
fi
# NPM
if [[ "$PACKAGE_MANAGER" == "npm" ]]; then
echo "cache-dependency-path=**/package-lock.json" >> $GITHUB_OUTPUT
echo "install-command=npm ci" >> $GITHUB_OUTPUT
echo "run-script-command=npm run" >> $GITHUB_OUTPUT
exit 0
fi
# Node version file
if [ -f ".nvmrc" ]; then
echo "node-version-file=.nvmrc" >> $GITHUB_OUTPUT
Expand All @@ -55,12 +37,12 @@ runs:
- uses: actions/setup-node@v4.0.2
with:
node-version-file: ${{ steps.get-package-manager.outputs.node-version-file }}
node-version-file: ${{ steps.get-node-version-file.outputs.node-version-file }}
cache: ${{ steps.get-package-manager.outputs.package-manager }}
cache-dependency-path: ${{ steps.prepare-variables.outputs.cache-dependency-path }}
cache-dependency-path: ${{ steps.get-package-manager.outputs.cache-dependency-path }}

- shell: bash
run: ${{ steps.prepare-variables.outputs.install-command }}
run: ${{ steps.get-package-manager.outputs.install-command }}

- if: inputs.dependencies-cache != ''
uses: ./self-setup-node-action/dependencies-cache
Expand Down
Loading

0 comments on commit 1f45da4

Please sign in to comment.