Why do I always fail when I run jobs on github actions? even though I have installed the latest version of Node on my machine - node.js

I have installed the latest version of NodeJs (v18.14.0), but it still fails to do jobs, what should I do? this is the code from my workflow and the screenshot of the error.
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
name: Node.js CI
on:
push:
branches: ['main']
pull_request:
branches: ['*']
jobs:
quality:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
steps:
- uses: actions/checkout#v3
- uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
publish:
runs-on: ubuntu-latest
if: ${{ github.ref == 'refs/heads/main' }}
needs: [quality]
steps:
- uses: actions/checkout#v3
- uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run semantic-release
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
I've tried installing everything from scratch by removing node_modules and package.lock.json then I did an npm install, but the result is still the same. When pushed to the repository it always fails to run the job

The strategy is missing for the publish job. You have to define it under publish too.
Using ${{ matrix.node-version }} is invalid here without strategy:
publish:
runs-on: ubuntu-latest
# ...
steps:
# ...
- uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }} # invalid
You need to verify which version actions/setup-node installs under publish with that invalid value. Apparently, only the default preinstalled Node.js 16.19.0 is there.
NOTE: The default preinstalled NodeJS version will be set to 18 from tomorrow (Monday - February 13th, 2023).
Apart from that, the error in that image:
[semantic-release]: node version >=18 is required. Found v16.19.0.
means that this step can only run on NodeJS v18+. Installing other lower versions i.e. 14.x and 16.x would result in failures. This makes strategy completely redundant because you only need v18:
- uses: actions/setup-node#v3
with:
node-version: 18
You might want to lint your workflows with https://rhysd.github.io/actionlint/ to identify potential issues.

Related

Running multiple versions of Node.js only running latest

I have followed the guidelines for Running multiple versions of Node.js using a matrix strategy:
.github/workflows/build-test.yml:
jobs:
build_and_test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [10, 12, 14]
The workflow does initiate to test with the intended three different versions of Node:
However, each is running with the latest:
For example, for Node v14, the workflow instead used Node v16.14.2:
/usr/local/bin/node --version
v16.14.2
/usr/local/bin/npm --version
8.5.0
Why is this happening?
Full yaml
name: Build and Test
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
build_and_test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [10, 12, 14]
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
node-version: ${{ matrix.node-version }}
- name: Install project
run: npm install
- name: Build the project
run: npm run build --if-present
- name: Run tests
run: npm test
Taking the suggestion from #jonrsharpe to use the latest v3, this works:
.github/workflows/build-test.yml
name: Build and Test
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
build_and_test:
runs-on: ubuntu-latest
strategy:
matrix:
node: [12, 14]
steps:
- name: Checkout
uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node }}
cache: npm
- name: Install project
run: npm install
- name: Build the project
run: npm run build --if-present
- name: Run tests
run: npm test

Is there any way to leave a folder unchanged after github action push

I have a nodejs project that stores images locally in the same location as the code src/uploads. The problem arises when I try to trigger the cicd pipeline using githubaction. As it just simply dumps the code from github to my repo(digital ocean droplet). Since the digital ocean droplet repo contains images that's been uploaded by the users, when the pipeline gets triggered the photos get removed as the github repository does not have those images. How do I solve this issue
yaml file for workflow
on:
push:
branches: [ dev ]
pull_request:
branches: [ dev ]
jobs:
build:
runs-on: self-hosted
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm i
- run: npm run build
- run: pm2 restart project-api

Cache dependencies in GitHub Actions on Windows

I am having a Github action which runs on Windows OS. Then, for caching the dependencies, I use the actions/cache#v2. But, it is not working as expected. When I saw the debug logs, the size of the cache is only about 30 B.
Here is the code I am using:
name: Build
on: push
jobs:
build_on_win:
runs-on: windows-latest
if: "!contains(github.event.head_commit.message, 'skip-publish')"
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#master
with:
node-version: 15
- name: Cache NPM dependencies
uses: actions/cache#v2
with:
path: ~/.npm
key: ${{ runner.OS }}-npm-cache-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-npm-cache-
- name: install dependencies
run: npm install
rest of the code..
Any help is greatly appreciated !
Caching package dependencies is a feature that is already available in the actions/setup-node#v2. You don't have to configure actions/cache#v2 and instead of that what you can do is use the setup-node#v2 action like below and you just have to add cache: 'npm' and it will automatically detect and will cache the dependencies.
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v2
with:
node-version: '14'
cache: 'npm'
- run: npm install
- run: npm test
If you have monorepo then you can also define the path
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v2
with:
node-version: '14'
cache: 'npm'
cache-dependency-path: subdir/package-lock.json
- run: npm install
- run: npm test
You can read more about it in Caching packages dependencies.

How to specifiy path for actions/setup-node in Github

Note: I have already seen these two:
How do I run my CI steps in a specific folder in github action
How to specify node's path in Github action?
But I still cant get it to work, thats why I am asking how I am able to set the working directory for a uses command. My yaml currently looks as follows:
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
name: Java CI with Maven
on:
push:
branches: [ main, Create-.yml-file ]
pull_request:
branches: [ main, Create-.yml-file ]
jobs:
javatest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-java#v2
with:
java-version: '16'
distribution: 'adopt'
- name: Cache Maven packages
uses: actions/cache#v2
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
run: |
mvn -f ./backend/pom.xml -B test
#mvn -f ./notification/pom.xml -B test
- name: Generate JaCoCo Badge
uses: cicirello/jacoco-badge-generator#v2
with:
generate-branches-badge: true
on-missing-report: quiet
jacoco-csv-file: >
-backend/target/site/jacoco/jacoco.csv
- name: Log coverage percentage
run: |
echo "coverage = ${{ steps.jacoco.outputs.coverage }}"
echo "branch coverage = ${{ steps.jacoco.outputs.branches }}"
- name: Commit the badge (if it changed)
run: |
if [[ `git status --porcelain` ]]; then
git config --global user.name 'myusername'
git config --global user.email 'myId#users.noreply.github.com'
git add -A
git commit -m "Autogenerated JaCoCo coverage badge"
git push
fi
- name: Upload JaCoCo coverage report
uses: actions/upload-artifact#v2
with:
name: jacoco-report
path: target/site/jacoco/
nodejstest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
working-directory: ./frontend
- run: npm run build --if-present
working-directory: ./frontend
- run: npm test
working-directory: ./frontend
with the error occuring here:
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
And looking like this:
Run actions/setup-node#v2
/usr/local/bin/npm config get cache
/home/runner/.npm
Error: Dependencies lock file is not found in /home/runner/work/path/to/main/directory. Supported file patterns: package-lock.json,yarn.lock
My package-lock ist located in the .../path/to/main/directory/frontend so it is obvious that it can not be found but according to the other two solutions this snippet should work shouldn't it? I also already tried combining the last three run statements into as well as move the working-directory setting to different places. All with varying amounts of failure
The support for custom path (relative to repository root) was added in version 2.4:
https://github.com/actions/setup-node/releases/tag/v2.4.0
You can try specifying '**/package-lock.json' in the
cache-dependency-path (it didn't work for me with other patterns).
Also you can try setting the working directory (either for all jobs or your nodejstest specific job) to point to your frontend folder.
nodejstest:
runs-on: ubuntu-latest
defaults:
run:
working-directory: 'frontend' # Here the path to the folder where package-lock.json is located.
strategy:
matrix:
node-version: [16.x] # Are you are missing this specification?
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: '**/package-lock.json' # THIS PATTERN did the trick for me.
setup_build_environment:
runs-on: ubuntu-latest
defaults:
run:
shell: bash
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
cache-dependency-path: ./node-app/yarn.lock
- name: run npm commands
working-directory: node-app
run: |
yarn && CI=true
# yarn build --if-present
# yarn test
node test.js
Reference : https://github.com/actions/setup-node#caching-packages-dependencies
The action defaults to search for the dependency file (package-lock.json or yarn.lock) in the repository root, and uses its hash as a part of the cache key. Use cache-dependency-path for cases when multiple dependency files are used, or they are located in different subdirectories.
I have used yarn, and the node app resides in /node-app directory, so in cache-dependency-path I use the lock file from yarn. And using working-directory for running yarn commands in node-app directory.
You can break down Node setup and cache into two steps like below
nodejstest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v2
with:
node-version: ${{ matrix.node-version }}
- name : Cache
uses: actions/cache#v2
with:
path: "frontend/node_modules"
key: node-modules-${{ hashFiles('frontend/package.json') }}
- run: npm ci
working-directory: ./frontend
- run: npm run build --if-present
working-directory: ./frontend
- run: npm test
working-directory: ./frontend

GitHub Actions for NodeJS Project that is dependent on using MicroPhone

I am trying to execute my automated NodeJS test scripts using Github Actions. Some of my tests are dependent on using Microphones from the system as there is a voice recorder in my application. The tests that use the system microphone are not working on cloud in Github actions. Does anyone if there is any way to enable mic on any OS provided by Github - Mac, Windows, Ubuntu
name: Node.js CI
on: [push]
jobs:
build:
runs-on: macos-latest
strategy:
matrix:
node-version: [10.16.3]
steps:
- uses: actions/checkout#v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v1
with:
browser: chrome
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run test:e2e
My issue was resolved after I added --use-fake-device-for-media-stream in ChromeOptions

Resources