|
1 | | -# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node |
2 | | -# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions |
3 | | - |
| 1 | +# Workflow for testing Node.js applications with clean dependency installation, caching, build, and tests |
4 | 2 | name: test |
5 | 3 |
|
| 4 | +# Trigger the workflow on push and pull requests to the main branch |
6 | 5 | on: |
7 | 6 | push: |
8 | 7 | branches: [ main ] |
9 | 8 | pull_request: |
10 | 9 | branches: [ main ] |
11 | 10 |
|
12 | | -jobs: |
13 | | - build: |
| 11 | +# Define permissions for security best practices |
| 12 | +permissions: |
| 13 | + contents: read |
14 | 14 |
|
| 15 | +jobs: |
| 16 | + test: |
15 | 17 | runs-on: ubuntu-latest |
16 | 18 |
|
17 | | - strategy: |
18 | | - matrix: |
19 | | - node-version: [16.x] |
20 | | - # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ |
21 | | - |
22 | 19 | steps: |
23 | | - - uses: actions/checkout@v2 |
24 | | - - name: Use Node.js ${{ matrix.node-version }} |
25 | | - uses: actions/setup-node@v2 |
26 | | - with: |
27 | | - node-version: ${{ matrix.node-version }} |
28 | | - cache: 'npm' |
29 | | - - run: npm ci |
30 | | - - run: npm run build --if-present |
31 | | - - run: npm test |
| 20 | + # Check out the repository code |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + # Set up Node.js with the latest available version |
| 25 | + - name: Set up Node.js (latest) |
| 26 | + uses: actions/setup-node@v4 |
| 27 | + with: |
| 28 | + node-version: 'latest' # Dynamically uses the latest Node.js version |
| 29 | + check-latest: true # Ensures the newest version is fetched (optional) |
| 30 | + cache: 'npm' |
| 31 | + cache-dependency-path: '**/package-lock.json' |
| 32 | + |
| 33 | + # Clean npm cache to prevent cache bloat |
| 34 | + - name: Clean npm cache |
| 35 | + run: npm cache clean --force |
| 36 | + |
| 37 | + # Install dependencies |
| 38 | + - name: Install dependencies |
| 39 | + run: npm ci |
| 40 | + |
| 41 | + # Build the project if a build script exists |
| 42 | + - name: Build project |
| 43 | + run: npm run build --if-present |
| 44 | + |
| 45 | + # Run tests |
| 46 | + - name: Run tests |
| 47 | + run: npm test |
| 48 | + |
| 49 | + # Upload artifacts for debugging if the job fails |
| 50 | + - name: Upload build artifacts (if build fails) |
| 51 | + if: failure() |
| 52 | + uses: actions/upload-artifact@v4 |
| 53 | + with: |
| 54 | + name: build-output |
| 55 | + path: | |
| 56 | + dist/ |
| 57 | + build/ |
| 58 | + retention-days: 7 |
| 59 | + |
| 60 | +# Enable debug logging for troubleshooting |
| 61 | +env: |
| 62 | + ACTIONS_STEP_DEBUG: true |
0 commit comments