heroku deployment with ci/cd - node.js

I am trying to build a CI/CD pipeline with github actions in order to build and deploy my app to heroku. I used following YAML file. But it shows error in github action. Could anyone please help me to solve this problem. My reository is bigshopcicd.My project structure is
bigshop
|-backend
|-frontend
|-package.json
Error-
Run npm run build
sh: 1: react-scripts: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! frontend#0.1.0 build: react-scripts build
npm ERR! spawn ENOENT
bigshop#1.0.0 build /home/runner/work/bigshopcicd/bigshopcicd
cd frontend && npm run build
frontend#0.1.0 build /home/runner/work/bigshopcicd/bigshopcicd/frontend
react-scripts build
npm ERR!
npm ERR! Failed at the frontend#0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! /home/runner/.npm/_logs/2021-12-23T08_13_25_954Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! bigshop#1.0.0 build: cd frontend && npm run build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the bigshop#1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/runner/.npm/_logs/2021-12-23T08_13_25_975Z-debug.log
Error: Process completed with exit code 1.
pipeline.yml-
name: Deployment pipeline
on:
push:
branches:
- main
pull_request:
branches: [main]
types: [opened, synchronize]
jobs:
simple_deployment_pipeline:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: '12.x'
- name: npm install
run: npm install
- name: build
run: npm run build
- name: deployment
uses: akhileshns/heroku-deploy#v3.12.12
if: ${{ github.event_name == 'push' && !contains(join(github.event.commits.*.message, ' ,'), '#skip') }}
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: bigshopcicd
heroku_email: fakirsumon78#gmmail.com
healthcheck: 'https://bigshopcicd.herokuapp.com/health'
checkstring: 'ok'
rollbackonhealthcheckfailed: true
- uses: actions/checkout#v2
- name: Bump version and push tag
uses: anothrNick/github-tag-action#eca2b69f9e2c24be7decccd0f15fdb1ea5906598
if: ${{ github.event_name == 'push' && !contains(join(github.event.commits.*.message, ' ,'), '#skip') }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WITH_V: true
DEFAULT_BUMP: patch
RELEASE_BRANCHES: main
package.json file-
"scripts": {
"start": "node backend/server.js",
"dev": "set NODE_ENV=DEVELOPMENT& nodemon backend/server",
"prod": "set NODE_ENV=PRODUCTION& nodemon backend/server",
"seeder": "node backend/utils/seeder.js",
"build": "cd frontend && npm run build",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false && npm install --prefix frontend && npm run build --prefix frontend"
},

Your npm install command runs the install in your root package.json which doesn't contain react dependencies. Then the build step goes into ./frontend and tries to use those react script that were'nt fetched.
You could use npm's preinstall target to install the frontend dependencies.
Also try to see if it is possible to split the project into two different ones because this structure seem fragile and you actually loose the advantages of having a separate backend/frontend. ( plus your scripts get overly complicated )
EDIT:
Preinstall isn't specifically made for installing node modules, it is simply a step you can add to your package.json which will be run by npm before the install step, it can contain arbitrary script commands. In your case maybe try :
"scripts": {
"preinstall": "cd frontend && npm install",
"start": "node backend/server.js",
"dev": "set NODE_ENV=DEVELOPMENT& nodemon backend/server",
"prod": "set NODE_ENV=PRODUCTION& nodemon backend/server",
"seeder": "node backend/utils/seeder.js",
"build": "cd frontend && npm run build"
}

Related

Posting to cyclic and it's asking for package.json to be in the root?

package.json not found at root level of repository.
Cyclic runs scripts defined "scripts" section to build, test and run your app.
But my project is set up like:
-final
--backend
---package.json(including npm start)
--frontend
---package.json(including npm start)
-package.json
^ includes:
"scripts": {
"frontend": "cd frontend; npm start",
"backend": "cd backend; npm start"
How do I merge these packages together and put them into the root directory so I can make this react application live?
I was thinking about something like this:
"scripts": {
"frontend": "cd frontend; npm start",
"backend": "cd backend; npm start"
but when I go to the root directory (final)
npm ERR! Missing script: "start"
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! npm star # Mark your favorite packages
npm ERR! npm stars # View packages marked as favorites
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run

Jest, command not found on GitLab

I'd like to execute my unit tests using JEST on GITLAB, but it seeem's not working.
It works on my local machine but not on GitLab.
The entire code of .gitlab-ci.yml :
image: node:16
cache:
paths:
- node_modules
install:
stage: build
script: npm ci
jest:
stage: test
script: npm run test:ci
artifacts:
when: always
reports:
junit:
- junit.xml
Package.json
"test": "jest",
"test:ci": "jest --config ./jest.config.js --ci --reporters=default --reporters=jest-junit"
Error :
npm ERR! code E401
npm ERR! Incorrect or missing password.
npm ERR! If you were trying to login, change your password, create an
npm ERR! authentication token or enable two-factor authentication then
npm ERR! that means you likely typed your password in incorrectly.
npm ERR! Please try again, or recover your password at:
npm ERR! https://www.npmjs.com/forgot
npm ERR!
npm ERR! If you were doing some other operation then your saved credentials are
npm ERR! probably out of date. To correct this please try logging in again with:
npm ERR! npm login
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-05-12T08_05_01_634Z-debug-0.log
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1
I found the issue by replacing the Node version (same version in my local machine) : image: node:14.
You need to have a step to install, better to check with GitLab CI document
image: node:16
cache:
paths:
- node_modules
install:
stage: build
script: npm ci
jest:
stage: test
script: npm run test:ci
artifacts:
when: always
reports:
junit:
- junit.xml

eslint not found in TravisCI Deploy

I am trying to get a site to deploy to Firebase using TravisCI. I keep running into an issue when the functions deploy and try to run lint. eslint doesn't seem to be there, even though I added a command to cd and npm install. I'm sure this is some kind of no-brainer. What am I missing?
Here's my .travis.yml:
language: node_js
node_js:
- 12
deploy:
provider: firebase
project: "*****"
message: "Deployed from Github by TravisCI"
token:
secure:
*******
env:
global:
secure:
*****
branches:
only:
- master
before_deploy:
- npm install -g gatsby-cli
- rm -rf node_modules/*/.git/
- npm install
- gatsby build
- cd functions && npm install --dev && cd ..
Here's the error in the Travis log:
Installing deploy dependencies
dpl.2
Preparing deploy
dpl.3
Deploying application
=== Deploying to 'covid19-reports'...
i deploying storage, firestore, functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions# lint /home/travis/build/amygroshek/covid19reports/functions
> eslint .
sh: 1: eslint: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! functions# lint: `eslint .`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the functions# lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! /home/travis/.npm/_logs/2020-03-23T00_34_17_951Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code1

Heroku recently started giving deploy error "npm ERR! code ELIFECYCLE" during Webpack build

Our deploy code hasn't changed, and our Webpack version ("webpack": "^4.41.0") hasn't changed, and deploys still work successfully for other (non-Heroku) servers.
However, just a couple days ago, Heroku deploy stopped working with this error:
2878 <s> [webpack.Progress] 70% building 4114/4114 modules 0 active
2879 npm ERR! code ELIFECYCLE
2880 npm ERR! errno 1
2881 npm ERR! client#0.1.0 build-heroku: 'npm run clean && webpack --progress --bail --env dist -p'
2882 npm ERR! Exit status 1
2883 npm ERR!
2884 npm ERR! Failed at the client#0.1.0 build-heroku script.`
Our /package.json has:
"heroku-postbuild": "cd frontend && npm install --only=dev && npm install && npm run build"
And our /frontend/package.json has:
"scripts": {
"clean": "rimraf dist/*",
"build": "npm run clean && webpack --progress --bail --env dist -p && rm -r prod 2>/dev/null || : && mv dist prod"
}
So why does Webpack build only get to 70% in Heroku now? Has anyone else seen this happen?
Turns out we weren't reading the log in full. There was errors in the middle of it which weren't appearing at the end. We literally had to search the raw log file for the word "error".
For example:
ModuleNotFoundError: Module not found: Error: Can't resolve 'dns' in '/tmp/build_e65fff75f8ffc7b45670e320e578bd35/frontend/node_modules/mongodb/lib/core'
The solution involved several steps:
Installing some of the modules (ie npm install x)
Adding this block to Webpack config:
node: {
fs: 'empty',
net: 'empty',
tls: 'empty',
dns: 'empty'
}
Stopping using the ruby image in our .gitlab-ci.yml file:

Gitlab - Update version package

We created a vuejs library proyect in Gitlab and created a simple pipelines that excecuted after when we pushed the commit.
We have a problem when last job execute the npm version patch (that update the patch in the project) but... it's not updated and it's doesn't work.
.gutlab-ci.yml
image: node:8.10.0-slim
cache:
paths:
- node_modules/
before_script:
- npm install
stages:
- lint
- test
- deploy
test:
stage: test
script:
- npm run peers:add && npm run test:unit
tags:
- docker
lint:
stage: lint
script:
- npm run lint
coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)/
publish:
stage: deploy
script:
- npm run peers:remove
- echo -e "//gitlab.com/api/v4/projects/<my-project>/packages/npm/:_authToken=${CI_NPM_TOKEN}" > ~/.npmrc
- npm login
- npm version patch
- npm publish
And package.json
[...]
"scripts": {
...
"build:dev": "npm run clean && webpack --config build/webpack.config.dev.js",
"version": "npm run build:dev && git add -A dist",
"postversion": "git push --follow-tags"
...
}
[...]
Jobs lint and test working but the publish not.
[...]
removed 4 packages in 9.428s
$ echo -e "//gitlab.com/api/v4/projects/<my-project>/packages/npm/:_authToken=${CI_NPM_TOKEN}" > ~/.npmrc
$ npm login
Username: npm ERR! cb() never called!
npm ERR! This is an error with npm itself. Please report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/...-debug.log
ERROR: Job failed: exit code 1
We need when the Merge Request it's accepted, automatically builed library and upload to npm repository with a new version (new patch, npm version patch). It's possible?
Thx.
npm login is an interactive command so doesn't work in CI very well. Try using the package npm-login-noninteractive to pass your credentials via command line. You can install it globally in your before_script:
before_script:
- npm i -g npm-login-noninteractive
Then call it in place of npm login in your publish script.

Resources