Skip to content

Commit 8c3f7bb

Browse files
authored
Optimise CI workflow to use cache and only run what's needed (#918)
1 parent b6faf5d commit 8c3f7bb

File tree

6 files changed

+337
-70
lines changed

6 files changed

+337
-70
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: 'NPM Setup'
2+
description: 'Sets up Node.js and installs NPM dependencies with caching'
3+
4+
inputs:
5+
runner:
6+
description: 'Runner to use'
7+
required: true
8+
node-version:
9+
description: 'Node.js version to use'
10+
required: true
11+
workspace:
12+
description: 'Key for the cache'
13+
required: true
14+
15+
outputs:
16+
workspace_path:
17+
description: "Full path to the workspace directory"
18+
value: ${{ steps.set-env.outputs.workspace_path }}
19+
20+
runs:
21+
using: "composite"
22+
steps:
23+
- name: Install NodeJS ${{ inputs.node-version }}
24+
uses: actions/setup-node@v4
25+
with:
26+
node-version: ${{ inputs.node-version }}
27+
28+
- name: Set cache configuration
29+
shell: bash
30+
id: set-env
31+
run: |
32+
if [ "${{ inputs.workspace }}" = "testcontainers" ]; then
33+
echo "CACHE_PATHS<<EOF" >> $GITHUB_ENV
34+
echo "node_modules" >> $GITHUB_ENV
35+
echo "packages/testcontainers/node_modules" >> $GITHUB_ENV
36+
echo "EOF" >> $GITHUB_ENV
37+
echo "WORKSPACE_PATH=packages/testcontainers" >> $GITHUB_ENV
38+
echo "workspace_path=packages/testcontainers" >> "$GITHUB_OUTPUT"
39+
else
40+
echo "CACHE_PATHS<<EOF" >> $GITHUB_ENV
41+
echo "node_modules" >> $GITHUB_ENV
42+
echo "packages/testcontainers/node_modules" >> $GITHUB_ENV
43+
echo "packages/modules/${{ inputs.workspace }}/node_modules" >> $GITHUB_ENV
44+
echo "EOF" >> $GITHUB_ENV
45+
echo "WORKSPACE_PATH=packages/modules/${{ inputs.workspace }}" >> $GITHUB_ENV
46+
echo "workspace_path=packages/modules/${{ inputs.workspace }}" >> "$GITHUB_OUTPUT"
47+
fi
48+
49+
- uses: actions/cache/restore@v4
50+
id: npm-cache
51+
with:
52+
path: ${{ env.CACHE_PATHS }}
53+
key: ${{ inputs.runner }}-node-${{ inputs.node-version }}-${{ inputs.workspace }}-${{ hashFiles('package-lock.json', 'packages/testcontainers/package-lock.json', format('packages/modules/{0}/package-lock.json', inputs.workspace)) }}
54+
restore-keys: |
55+
${{ inputs.runner }}-node-${{ inputs.node-version }}-${{ inputs.workspace }}-
56+
${{ inputs.runner }}-node-${{ inputs.node-version }}-
57+
58+
- name: Install dependencies
59+
if: steps.npm-cache.outputs.cache-hit != 'true'
60+
shell: bash
61+
run: npm ci --workspace ${{ env.WORKSPACE_PATH }} --include-workspace-root
62+
63+
- name: Cache npm
64+
if: steps.npm-cache.outputs.cache-hit != 'true'
65+
uses: actions/cache/save@v4
66+
with:
67+
path: ${{ env.CACHE_PATHS }}
68+
key: ${{ steps.npm-cache.outputs.cache-primary-key }}

.github/scripts/changed-modules.sh

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env bash
2+
3+
# exit on error, unset variables, print commands, fail on pipe errors
4+
set -euxo pipefail
5+
6+
# How to test this script, run it with the required environment variables:
7+
# 1. A modified file from the root, but not the package.json or package-lock.json:
8+
# ALL_CHANGED_FILES="README.md" ./.github/scripts/changed-modules.sh
9+
# Expected output: [], as no module should be built
10+
#
11+
# 2. The package.json or package-lock.json are modified:
12+
# ALL_CHANGED_FILES="package.json" ./.github/scripts/changed-modules.sh
13+
# Expected output: all modules, as the dependencies have been modified
14+
# ALL_CHANGED_FILES="package-lock.json" ./.github/scripts/changed-modules.sh
15+
# Expected output: all modules, as the dependencies have been modified
16+
#
17+
# 3. A file in the testcontainers module is modified:
18+
# ALL_CHANGED_FILES="packages/testcontainers/a.txt" ./.github/scripts/changed-modules.sh
19+
# Expected output: all modules, as the core has been modified
20+
#
21+
# 4. A file in a module is modified:
22+
# ALL_CHANGED_FILES="packages/modules/arangodb/a.txt" ./.github/scripts/changed-modules.sh
23+
# Expected output: [arangodb], only
24+
#
25+
# 5. Three files in three different modules are modified:
26+
# ALL_CHANGED_FILES="packages/modules/arangodb/a.txt packages/modules/cassandra/b.txt packages/modules/chromadb/c.txt" ./.github/scripts/changed-modules.sh
27+
# Expected output: [arangodb, cassandra, chromadb]
28+
#
29+
# 6. Core files and module files are modified:
30+
# ALL_CHANGED_FILES="packages/testcontainers/a.txt packages/modules/chromadb/b.txt" ./.github/scripts/changed-modules.sh
31+
# Expected output: all modules, as the core has been modified
32+
#
33+
# 7. This script is modified:
34+
# ALL_CHANGED_FILES=".github/scripts/changed-modules.sh" ./.github/scripts/changed-modules.sh
35+
# Expected output: all modules, as the build script has been modified
36+
#
37+
# 8. A .github file is modified:
38+
# ALL_CHANGED_FILES=".github/release-drafter.yml" ./.github/scripts/changed-modules.sh
39+
# Expected output: []
40+
#
41+
# 9. A excluded module is modified:
42+
# ALL_CHANGED_FILES="packages/modules/couchbase/a.txt" ./.github/scripts/changed-modules.sh
43+
# Expected output: []
44+
#
45+
# There is room for improvement in this script. For example, it could detect if the changes applied to the docs or the .github dirs, and then do not include any module in the list.
46+
# But then we would need to verify the CI scripts to ensure that the job receives the correct modules to build.
47+
48+
# ROOT_DIR is the root directory of the repository.
49+
readonly ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
50+
51+
# define an array of directories that won't be included in the list
52+
readonly excluded_modules=(".devcontainer" ".vscode" ".husky" "docs" ".github/ISSUE_TEMPLATE")
53+
54+
# define an array of files that won't be included in the list
55+
# Get all files in the root directory except package.json and package-lock.json
56+
# Create array of excluded files by finding all files in root dir except package.json and package-lock.json
57+
excluded_files=("${ROOT_DIR}/.github/release-drafter.yml")
58+
while IFS= read -r file; do
59+
excluded_files+=("\"${file}\"")
60+
done < <(find "${ROOT_DIR}" -maxdepth 1 -type f -not -name "package.json" -not -name "package-lock.json")
61+
62+
# define an array of modules that won't be part of the build
63+
readonly no_build_modules=("couchbase")
64+
65+
# modules is an array that will store the paths of all the modules in the repository.
66+
modules=()
67+
68+
# Find all package.json files in the repository, building a list of all the available modules.
69+
# The list of modules is stored in the modules array, but the testcontainers-node module is excluded
70+
# as it is not a module.
71+
for packageJSONFile in $(find "${ROOT_DIR}" -name "package.json" -not -path "*/node_modules/*"); do
72+
name=$(basename "$(dirname "${packageJSONFile}")")
73+
if [[ "${name}" != "testcontainers-node" ]]; then
74+
modules+=("\"${name}\"")
75+
fi
76+
done
77+
78+
# sort modules array
79+
IFS=$'\n' modules=($(sort <<<"${modules[*]}"))
80+
unset IFS
81+
82+
# Get the list of modified files, retrieved from the environment variable ALL_CHANGED_FILES.
83+
# On CI, this value will come from a Github Action retrieving the list of modified files from the pull request.
84+
readonly modified_files=${ALL_CHANGED_FILES[@]}
85+
86+
# Initialize variables
87+
modified_modules=()
88+
89+
# Check the modified files and determine which modules to build, following these rules:
90+
# - if the modified files contain any file in the root module, include all modules in the list
91+
# - if the modified files only contain files in one of the modules, include that module in the list
92+
for file in $modified_files; do
93+
# check if the file is in one of the excluded files
94+
for exclude_file in ${excluded_files[@]}; do
95+
# Remove quotes from exclude_file for comparison
96+
clean_exclude_file=$(echo $exclude_file | tr -d '"')
97+
if [[ "${ROOT_DIR}/${file}" == "${clean_exclude_file}" ]]; then
98+
# if the file is in the excluded files, skip the rest of the loop.
99+
# Execution continues at the loop control of the 2nd enclosing loop.
100+
continue 2
101+
fi
102+
done
103+
104+
if [[ $file == packages/modules/* ]]; then
105+
module_name=$(echo $file | cut -d'/' -f3)
106+
if [[ ! " ${modified_modules[@]} " =~ " ${module_name} " ]]; then
107+
modified_modules+=("\"$module_name\"")
108+
fi
109+
else
110+
# a file from the core module (packages/testcontainers) is modified, so include all modules in the list and stop the loop
111+
# check if the file is in one of the excluded modules
112+
for exclude_module in ${excluded_modules[@]}; do
113+
if [[ $file == $exclude_module/* ]]; then
114+
# continue skips to the next iteration of an enclosing for, select, until, or while loop in a shell script.
115+
# Execution continues at the loop control of the nth enclosing loop, in this case two levels up.
116+
continue 2
117+
fi
118+
done
119+
120+
modified_modules=${modules[@]}
121+
break
122+
fi
123+
done
124+
125+
# print all modules with this format:
126+
# each module will be enclosed in double quotes
127+
# each module will be separated by a comma
128+
# the entire list will be enclosed in square brackets
129+
# the list will be sorted and unique
130+
sorted_unique_modules=($(echo "${modified_modules[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
131+
132+
# remove modules that won't be part of the build from the list
133+
filtered_modules=()
134+
for module in "${sorted_unique_modules[@]}"; do
135+
skip=false
136+
for no_build_module in "${no_build_modules[@]}"; do
137+
if [[ ${module} == \"${no_build_module}\" ]]; then
138+
skip=true
139+
break
140+
fi
141+
done
142+
if [[ $skip == false ]]; then
143+
filtered_modules+=(${module})
144+
fi
145+
done
146+
sorted_unique_modules=("${filtered_modules[@]}")
147+
148+
echo "["$(IFS=,; echo "${sorted_unique_modules[*]}" | sed 's/ /,/g')"]"

.github/workflows/lint.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

.github/workflows/test-template.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ jobs:
130130
- name: Code checkout
131131
uses: actions/checkout@v4
132132

133-
- name: Install NodeJS ${{ inputs.node-version }}
134-
uses: actions/setup-node@v4
133+
- name: Install Node ${{ inputs.node-version }} and Dependencies
134+
id: npm-install
135+
uses: ./.github/actions/npm-setup
135136
with:
137+
runner: ${{ inputs.runner }}
136138
node-version: ${{ inputs.node-version }}
137-
138-
- name: Install dependencies
139-
run: npm ci --omit=optional
139+
workspace: "${{ inputs.workspace }}"
140140

141141
- name: Run tests
142-
run: npm run test:ci -- ${{ inputs.workspace }}
142+
run: npm run test:ci -- ${{ steps.npm-install.outputs.workspace_path }}

0 commit comments

Comments
 (0)