generated from chill-viking/basic-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
100 lines (89 loc) · 3.24 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
# This is a composite action that installs npm dependencies or uses cached dependencies
# To improve performance, it uses the cache action to cache the node_modules folder
name: npm-ci
author: "Peter Jokumsen"
description: "Install npm dependencies or use cached dependencies"
branding:
icon: "arrow-down-circle"
color: "blue"
inputs:
working_directory:
description: "Working directory to use"
default: "./"
outputs:
restored_from_cache:
description: "True if dependencies were restored from cache"
value: ${{ steps.cache-node.outputs.cache-hit }}
runs:
using: "composite"
steps:
- name: Prepare working directory
id: working_directory
shell: pwsh
run: |
# Add trailing slash to path if not present
$input = "${{ inputs.working_directory }}"
if ($input -notlike "*/")
{
Write-Output "path=${input}/" >> $env:GITHUB_OUTPUT
}
else
{
Write-Output "path=${input}" >> $env:GITHUB_OUTPUT
}
- name: Create packages-only packge-lock
shell: pwsh
run: |
$packageLocks = Get-ChildItem -Path ${{ steps.working_directory.outputs.path }} -Filter 'package-lock.json' -Recurse
foreach ($packageLock in $packageLocks)
{
$lockDirectory = $packageLock.Directory.FullName
$lockPath = $packageLock.FullName
$lockContent = Get-Content -Path $lockPath -Raw `
| ConvertFrom-Json -AsHashTable
$packagesOnly = @{
packages = $lockContent.packages
}
Write-Output "$lockPath -> $lockDirectory/packages-only-lock.json"
$packagesOnly | ConvertTo-Json -Depth 100 `
| Set-Content -Path "$lockDirectory/packages-only-lock.json"
}
- name: Use node_modules from cache [npm]
id: cache-node
uses: actions/cache@v4
env:
cache-name: cache-node-modules
with:
# caching node_modules
path: ${{ steps.working_directory.outputs.path }}node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**//packages-only-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- shell: pwsh
name: Install dependencies
working-directory: ${{ steps.working_directory.outputs.path }}
if: steps.cache-node.outputs.cache-hit != 'true'
run: |
npm ci
- shell: pwsh
name: Clear package only lock files
run: |
Get-ChildItem -Path ${{ steps.working_directory.outputs.path }} -Filter 'packages-only-lock.json' -Recurse `
| Remove-Item
- shell: pwsh
name: Check path for cache
if: steps.cache-node.outputs.cache-hit != 'true'
run: |
# Check if node_modules is cached
if (Test-Path ${{ steps.working_directory.outputs.path }}node_modules)
{
Write-Output "node_modules found, should be cached"
}
else
{
Write-Output "::warning title=node_modules not found::node_modules not found using path '${{ steps.working_directory.outputs.path }}'"
}