How to make jenkins move to the next stage if its "terminal" has been blocked? - node.js

I'm trying to run http calls for testing a live web api that's going to run in the jenkins machine.
This is the pipeline script that's been used.
stage 'build'
node {
git url: 'https://github.com/juliomarcos/sample-http-test-ci/'
sh "npm install"
sh "npm start"
}
stage 'test'
node {
sh "npm test"
}
But jenkins won't move to the test step. How can I run npm test after the web app has fully started?

One approach is to start the web app with an & at the end so it will run in the background. i.e.
npm start &
You can try to redirect the output of npm start to a text file like this:
npm start > output.txt &
Then in the next step, loop until the "started" message is available, something like:
tail -f output.txt | while read LOGLINE
do
[[ "${LOGLINE}" == *"listening on port"* ]] && pkill -P $$ tail
done
Code not tested :)

Related

Is it possible to suppress NPM's echo of the commands it is running?

I've got a bash script that starts up a server and then runs some functional tests. It's got to happen in one script, so I'm running the server in the background. This all happens via 2 npm commands: start:nolog and test:functional.
All good. But there's a lot of cruft in the output that I don't care about:
✗ ./functional-tests/runInPipeline.sh
(... "good" output here)
> #co/foo#2.2.10 pretest:functional /Users/jcol53/Documents/work/foo
> curl 'http://localhost:3000/foo' -s -f -o /dev/null || (echo 'Website must be running locally for functional tests.' && exit 1)
> #co/foo#2.2.10 test:functional /Users/jcol53/Documents/work/foo
> npm run --prefix functional-tests test:dev:chromeff
> #co/foo-functional-tests#1.0.0 test:dev:chromeff /Users/jcol53/Documents/work/foo/functional-tests
> testcafe chrome:headless,firefox:headless ./tests/**.test.js -r junit:reports/functional-test.junit.xml -r html:reports/functional-test.html --skip-js-errors
That's a lot of lines that I don't need there. Can I suppress the #co/foo-functional-tests etc lines? They aren't telling me anything worthwhile...
npm run -s kills all output from the command, which is not what I'm looking for.
This is probably not possible but that's OK, I'm curious, maybe I missed something...

Make Nodejs script run in background in gitlab CI

Our dev project start by command npm run serve Is it possible to run it on background mode? I tried to use nohup, & in end of string. It works properly in shell, but when it start by CI on Gitlab, pipeline state always is "running" cause npm output permanently shows on screen
The clean way would be to run a container whose run command is "npm run serve"
I'm not certain running a non-blocking command through your pipeline is the right way but you should try using "&"
"npm run serve" will run the command in "detached mode.
I've faced the same problem using nohup and &. It was working well in shell, but not on Gitlab CI, It looks like npm start was not detached.
What worked for me is to call npm start inside a bash script and run it on before_script hook.
test:
stage: test
before_script:
- ./serverstart.sh
script:
- npm test
after_script:
- kill -9 $(ps aux | grep '\snode\s' | awk '{print $2}')
on the bash script serverstart.sh
# !/bin/bash
# start the server and send the console and error logs on nodeserver.log
npm start > nodeserver.log 2>&1 &
# keep waiting until the server is started
# (in my case wait for mongodb://localhost:27017/app-test to be logged)
while ! grep -q "mongodb://localhost:27017/app-test" nodeserver.log
do
sleep .1
done
echo -e "server has started\n"
exit 0
this allowed me to detach npm start and pass to next command while keeping npm startprocess alive

Run script command on parallel

i’ve bash script which I need to run on it two command in parallel
For example I’m executing a command of npm install which takes some time (20 -50 secs)
and I run it on two different folders in sequence first npm install on books folder and the second
is for orders folder, is there a way to run both in parallel in shell script ?
For example assume the script is like following:
#!/usr/bin/env bash
dir=$(pwd)
cd $tmpDir/books/
npm install
grunt
npm prune production
cd $tmpDir/orders/
npm install
grunt
npm prune production
You could use & to run the process in the background, for example:
#!/bin/sh
cd $HOME/project/books/
npm install &
cd $HOME/project/orders/
npm install &
# if want to wait for the processes to finish
wait
To run and wait for nested/multiple processes you could use a subshell () for example:
#!/bin/sh
(sleep 10 && echo 10 && sleep 1 && echo 1) &
cd $HOME/project/books/
(npm install && grunt && npm prune production ) &
cd $HOME/project/orders/
(npm install && grunt && npm prune production ) &
# waiting ...
wait
In this case, notice the that the commands are within () and using && that means that only the right side will be evaluated if the left size succeeds (exit 0) so for the example:
(sleep 10 && echo 10 && sleep 1 && echo 1) &
It creates a subshell putting things between ()
runs sleep 10 and if succeeds && then runs echo 10, if succeeds && then run sleep 1 and if succeeds && then runs echo 1
run all this in the background by ending the command with &

How to run another serve after node server has started using shell script?

I want to write a script which basically runs my node server first and after only node server has started I want to run another script. How can I implement this using shell script?
Right now I have done this so far
echo "Going inside NodeServer folder";
cd ./../Server-Node
echo "Starting Node Server";
npm start
echo 'Going inside Project Folder';
cd ./../ionicApp
ionic serve
A simple hack is to use npm start & add a sleep 15 on the line after it (or adjust accordingly to the avg time the start takes).
Note: to terminate the node process you might have to run a command to kill it stop all instances of node.js server
Otherwise you'll want to look at some stuff here NPM run parallel task, but wait until resource is available to run second task
I found out this later. adding modified script
echo "Going inside Server-Node";
cd ./../Server-Node
echo "Starting Node Server";
npm start & echo OK
echo 'Going inside ionic-Project';
cd ./../learn-ionic
echo 'Starting ionic server';
ionic serve

How to make Docker restart a Container after a period of time?

How to restart a Node JS application running inside a docker container after a period of time without user input (Automated)?
I have a Docker Container with an application whose underlying architecture showed to hang once in a while. The idea is to simply restart the application after a period of time. This should all happen automated. Consider the following Dockerfile.
FROM node:6.11.1
ENV HOME=/usr/src/app/
# Create app directory
RUN mkdir -p $HOME
WORKDIR $HOME
COPY npm-shrinkwrap.json $HOME
RUN npm install
# Bundle app source
COPY . $HOME
EXPOSE 3000
CMD ["npm", "start"]
After npm start the application either was successfull or not. In most cases it runs successull. So for the other cases I would like to simply restart the whole application after a period of time.
Install Node cron
$ npm install --save node-cron
Import node-cron and schedule a task:
var cron = require('node-cron');
cron.schedule('* * * * *', function(){
console.log('running a task every minute');
});
Combining the following from ivanvanderbyl.
COPY entrypoint.sh /entrypoint
RUN chmod +x /entrypoint
ENTRYPOINT ["/entrypoint", "node", "--harmony-async-await"]
And the official documentation on ENTRYPOINT, Run multiple services in a container and reading through a bunch of Bash tutorials I came up with the following solution.
#!/bin/bash
echo " # Starting Scraper"
node index.js -D
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start node: $status"
exit $status
fi
echo " # Init Restart-Routine. Beware, console will freeze!"
while :
do
echo "[1]/[4] Sleep"
sleep 5
echo "[2]/[4] Kill node process"
pkill -f "node index.js"
echo "[3]/[4] Sleep 2 seconds to make sure everything is down"
sleep 2
echo "[4]/[4] Start NodeJS"
node index.js
done
The final product does the following: When I start Docker it starts my node application and after a period of time it kills the node process BUT NOT the docker process and restarts it. Exactly what I was looking for.

Resources