Issue in caching dependencies in github actions - npm

I am trying to use the shard option in jest to run my tests in parallel across multiple runners.
But before I can do this on CI - I have to do some other steps like checkout codebase, install dependencies in the specific job running the test.
Problem statement - I want to avoid installing dependencies for each shard of test running.
I searched around the internet and found that I cannot do this in another preceding job because each job starts fresh in its own container.
Hence I wrote something like this:
run_tests:
runs-on: [self-hosted]
name: Run Tests
continue-on-error: false
strategy:
matrix:
shard: [1, 2, 3, 4]
steps:
- name: Checkout Codebase
uses: actions/checkout-action#v2
- name: Use Node v14.15.1
uses: actions/setup-node#v1
with:
node-version: 14.15.1
- name: Cache NPM dependencies
id: npmCacheStep
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
if: steps.npmCacheStep.outputs.cache-hit != 'true'
run: npm ci
- name: Run tests
run: npm run test -- --colors --coverage --shard=${{ matrix.shard }}/${{ strategy.job-total }}
To solve my problem statement, I'm saying that if it found a cache of npm dependencies - then don't install npm modules again as recommended at lots of places - https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows
But when I try to run this
the npm install step is skipped (as expected)
I get the following error on github actions workflow logs:
I am trying this solution: Is there a way to speedup npm ci using cache?

Related

npm version package-lock.json caching issue in GitHub Actions

My trigger in the GitHub Actions workflow is when we have a new tag, then deploy the tag to the desired environment.
So, running npm version prerelease will trigger the workflow.
The problem is when I try to use cache I see that the version in package.json and package-lock.json are always changed so I can't use real cache here.
How can I continue working with npm version and get the benefits of caching?
- uses: actions/checkout#v2
- name: Cache node modules
id: cache-nodemodules
uses: actions/cache#v2
env:
cache-name: cache-node-modules-preview
with:
# caching node_modules
path: node_modules
key: ${{ runner.os }}-preview-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-preview-${{ env.cache-name }}-
${{ runner.os }}-preview-
- name: Install Dependencies app
if: steps.cache-nodemodules.outputs.cache-hit != 'true'
run: npm ci --legacy-peer-deps
- name: build
run: npm run build

Increase the NPM Package version automatically

I am creating my own NPM packages for the first time. For each commit, 1/ The package version should increase on the NPM registry, 2/ Update the package.json file in the github repository.
.github/workflows/publish.yml
on:
push:
branches:
- main
env:
version: 0
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: AutoModality/action-clean#v1
- uses: actions/checkout#v2
with:
ref: 'main'
fetch-depth: 0
- uses: actions/setup-node#v2
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
scope: "#nandhirajan-tfi"
- run: echo "version=$(npm show #nandhirajan-tfi/my-package version)" >> $GITHUB_ENV
- run: npm version ${{env.version}} --no-git-tag-version --allow-same-version
- run: npm install
- run: npm build
- run: npm version patch -m "[RELEASE] %s" --no-git-tag-version --allow-same-version
- run: npm publish
env:
credentials: ${{secrets.GITHUB_TOKEN}}
GitHub Actions Output:
The above log says that the npm publish command has updated the NPM Version to 1.11.18. But the changes are not reflecting on the NPM registry.
Any help would be appreciated.
First of all you need to retrieve your package.json version. You can use the action ga-project-version for that.
Example:
name: release
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
# This is how you use the ga-project-version action
- name: Get version of the project
id: project-version
uses: 'euberdeveloper/ga-project-version#main'
# In this step the exposed version is used as tag of a github release to publish
- name: Add release
uses: "marvinpinto/action-automatic-releases#latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
# This is how you access the exposed version
automatic_release_tag: "${{ steps.project-version.outputs.version }}"
title: "Deploy"
files: |
backend.tar.gz
As you can see, you will have the version available in "${{ steps.project-version.outputs.version }}".
After that the question is with which logic you will augment your version, e.g. the simplest one would be increasing the last number, but it's not a good idea, it'd not be semver.
After that you can use your custom way to get the newer version by using the current one as input ("${{ steps.project-version.outputs.version }}"`).
After having the new version, just edit the package.json (e.g. sed command) to write the new version, and commit it (e.g. stefanzweifel/git-auto-commit-action action).
In the end you can publish your npm package from the github action (follow this link)

Gulp is missing but only for osx

This is a GitHub Action
on:
push:
branches:
- '*' # matches every branch that doesn't contain a '/'
- '*/*' # matches every branch containing a single '/'
- '**' # matches every branch
- '!master' # excludes master
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Install Node.js
uses: actions/setup-node#v1
with:
node-version: 16.x
- run: npm install
- run: gulp
- run: xvfb-run -a npm test
if: runner.os == 'Linux'
- run: npm test
if: runner.os != 'Linux'
This is the outcome from a push, which triggers it.
As you can see it works fine on Windows and Linux, but fails on OSX.
Inspecting the Run gulp step we find this message.
As you can see, gulp is not found. It's present in devDependencies, and my expectation was that the npm install step would install it, this being how it gets onto the other two platforms.
I tried explicitly installing gulp-cli but that didn't help.
Installing gulp globally (npm i gulp gulp-cli -g) resolves the problem, but I do not understand why this should be necessary for only MacOS. Perhaps the answer is one of differences between platforms but I do not like mysteries so if anyone can provide clarity that would be highly desirable.

Run a dev server in CI pipleine

I have a CI pipeline setup using Github Action/Workflows, where i would want to run Cypress Automated tests, However I am having some logical problems of how to run my dev server. let me show you my pipeline
name: Nuxt CI Pipeline
on:
push:
branches: [ CI-pipeline ]
# pull_request:
# branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
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'
- name: Make envfile
uses: SpicyPizza/create-envfile#v1
with:
envkey_ENV: staging
file_name: .env
- run: npm ci
- run: npm run dev
- run: |
cd e2e
ls -l
npm ci
npx cypress run
Now I want to spin up the devserver and run the tests on that port usually 3000 , however the problem is when the command npm run dev is executed, the pipeline keeps on waiting there and doesnt move forward , which makes sense as devserver doesn't return a response as other commands will , so its kinda stuck there. My knowledge of devops is bare minimum , can someone point out what am i missing?
I think the way of execution is not ideal, especially since so the node server is also not killed correctly in the end. Using a helper package like start-server-and-test should do the trick for you:
npm install --save-dev start-server-and-test
While I'm not sure what exactly is behind your scripts in your package.json, it could look something like this in the end:
"scripts": {
"start:ci": "<<start your dev server>>",
"cy:run": "cypress run --browser chrome --headless",
"cy:ci": "start-server-and-test start:ci http://localhost:3000 cy:run"
},
Then you can simply run this as a single command in your pipeline with npm run cy:ci. The script will take care of starting your dev server, waiting for the URL to be available, then executing the tests and after all tests are finished, it will shut down the server.

BitBucket pipeline breaks when new dependency is added to package.lock

Since there is already a saved cache for node_modules when the Pipeline is run, it does not make the npm install so new dependencies are not installed. Because of this, while the Pipeline is still running, it suddenly breaks as project does not find the corresponding package.
pipelines:
custom:
deploy-staging:
- step:
name: install dependencies
caches:
- node
script:
- npm install
- step:
name: deploy to all STAGING themes
deployment: staging
caches:
- node
- globalnode
script:
- npm run deployall -- -t staging
How could I fix that?