Unable to start pm2 via jenkins pipeline - jenkins

on a Windows machine, I have setup a very simple pipeline in Jenkins that does the following:
clone a git repository,
install the packages,
run the app via "pm2 start command"
Below is the entire pipleline script :
node {
stage('dev'){
git credentialsId: 'my-credentials', url: 'git#myurl.git'
bat 'npm install'
bat 'pm2 start src\\index.js --name myapp'
}
}
Everything works fine except running the pm2 command. The output error says :
'pm2' is not recognized as an internal or external command,
operable program or batch file.
However, I can easily run the exact same PM2 command via CMD, I have tried putting the last line command into a .bat file and asked jenkins to execute it, and get the same error.

Jenkins couldn't access the PM2 that was installed on the Windows machine globally that is due to the fact that Jenkin was running as the system (root) user while pm2 was running with the local user. I had to include PM2 in the package.config file of the project and then call it from the node_module folder.
\node_modules\.bin\pm2 start src\\index.js --name myapp

Related

Why is this Todo app build failing in Jenkins when deploying on AWS Linux using Docker file in WSL2?

So I was trying to deploy a simple CD pipeline using docker by ssh’ing into my AWS Linux EC2 instance in the WSL2 terminal. The job is failing every time returning the following error:
Started by user Navdeep Singh Running as SYSTEM Building on the
built-in node in workspace /var/lib/jenkins/workspace/todo-dev
[todo-dev] $ /bin/sh -xe /tmp/jenkins6737039323529850559.sh + cd
/home/ubuntu/project/django-todo /tmp/jenkins6737039323529850559.sh:
2: cd: can’t cd to /home/ubuntu/project/django-todo Build step
‘Execute shell’ marked build as failure Finished: FAILURE
DockerFile contents:
FROM python:3 RUN pip install django==3.2
COPY . .
RUN python manage.py migrate
CMD [“python”,“manage.py”,“runserver”,“0.0.0.0:8000”]
Everything goes fine. This error cd: can’t cd to /home/ubuntu/project/django-todo Build step ‘Execute shell’ marked build as failure Finished: FAILURE is not an actual.
Your agent Node is not online.
To fix the problem, find commands on your jenkins web page after an agent setup. You need to run those commands from your terminal. See the screenshot for more details.
Make sure that your jenkins public IP and node agent public IP are the same. If an error occurs, you need to run some commands on the terminal. This is not a real error.
this issue follow this step which i give you
For Agent--->
change your ip here(44.203.138.174:8080) to your EC2 ip
1.curl -sO http://44.203.138.174:8080/jnlpJars/agent.jar
2.java -jar agent.jar -jnlpUrl http://44.203.138.174:8080/manage/computer/todo%2Dagent/jenkins-agent.jnlp -secret beb62de0f81bfd06e4cd81d1b896d85d38f82b87b21ef8baef3389e651c9f72c -workDir "/home/ubuntu"
For JOb --->
sudo vi /etc/sudoers
then add this command below root access in sudoers file
jenkins ALL=(ALL) NOPASSWD: ALL
3.then goto the ubuntu directory using cd .. then run this codes
grep ^ubuntu /etc/group
id jenkins
sudo adduser jenkins ubuntu
grep ^ubuntu /etc/group
4.restart the jenkins relogin
sudo systemctl stop jenkins
then you good to go

Jenkins hangs after "chroot . sh" during ssh

I'm doing a Jenkins freestyle build, ssh'ing into a VM and running some existing scripts. During a build step "Execute shell script on remote host using ssh", everything is working fine until I get to a command: "chroot . sh". This is mounting a rootfs which we build in, if I do this step manually it is bringing me to a sh prompt where I can run another script to do the actual build, but Jenkins just hangs forever at this point.
From looking around it looks like this is because the command does not return a completion signal?, so Jenkins is waiting indefinitely. I've also just tried doing the same steps in Putty, using a text file containing the commands I need. The Putty "script" also fails at this point, stopping any input because of the new sh prompt.
Is there any way around this? I've tried various solutions, like:
nohup chroot . sh 1>&2 - of course this doesnt work
and running the command in the background doesnt put me into the chroot environment I need.
Kind of confused at this point.
Edit:
Code snippet:
cd /home/dev/root_env
chroot . sh
cd /home/dev
./build.sh
Thats literally all I'm doing, however I freeze forever at line 2 of that.

Using Docker for Windows in Jenkins Declarative Pipeline

I am setting up a CI workflow with Jenkins declarative pipeline and Docker-for-Windows agents through Dockerfile.
Note: It is unfortunately currently not a solution to use a Linux-based docker daemon, since I need to run Windows binaries.
Setup: Jenkins master runs on Linux 16.04 through Docker. Jenkins build agent is
Windows 10 Enterprise 1709 (16299.551)
Docker-for-Windows 17.12.0-ce
Docker 18.x gave me headaches when trying to use Windows Containers, so I rolled back to 17.x. I still had some issues when trying to run with Jenkins and nohup not being on path, but it was solved by adding Git binaries to Windows search path (another reference). I suspect my current issue may be related.
Code: I am trying to initialize a Jenkinsfile and run a simple hello-world-printout within.
/Jenkinsfile
pipeline {
agent none
stages {
stage('Docker Test') {
agent {
dockerfile {
filename 'Dockerfile'
label 'windocker'
}
}
steps {
println 'Hello, World!'
}
}
}
}
/Dockerfile
FROM python:3.7-windowsservercore
RUN python -m pip install --upgrade pip
Basically, this should be a clean image that simply prints "Hello, World!". But it fails on Jenkins!
Output from the log:
[C:\jenkins\workspace\dockerfilecd4c215a] Running shell script
+ docker build -t cbe5e0bb1fa45f7ec37a2b15566f84aa9bd08f5d -f Dockerfile .
Sending build context to Docker daemon 337.4kB
Step 1/2 : FROM python:3.7-windowsservercore
---> 340689b75c39
Step 2/2 : RUN python -m pip install --upgrade pip
---> Using cache
---> a93f446a877f
Successfully built a93f446a877f
Successfully tagged cbe5e0bb1fa45f7ec37a2b15566f84aa9bd08f5d:latest
[C:\jenkins\workspace\dockerfilecd4c215a] Running shell script
+ docker inspect -f . cbe5e0bb1fa45f7ec37a2b15566f84aa9bd08f5d
.
Cannot run program "id": CreateProcess error=2, The system cannot find the file specified
The issue is, that windows is not supported at the moment. It is calling the linux "id" command to get the current user id.
There is an open Pull Request and JIRA Ticket at Jenkins to support Windows docker pipeline:
https://issues.jenkins-ci.org/browse/JENKINS-36776
https://github.com/jenkinsci/docker-workflow-plugin/pull/148

Cannot execute a shell script with docker command from Jenkins Pipeline step

I am using Jenkins version 2.121.1 with Pipeline On MacOS-HighSierra.
I've a shell script called build_docker_image.sh that builds a docker image using the following command:
docker build -t test_api:1 -f test-dockerfile
test-dockerfile is a Dockerfile and has instructions to build an image.
From CLI the whole set up works!
However, when I run it from Jenkins server Pipeline context, it is failing at the above line with an error: docker: command not found
The step that triggers from Jenkins server is simple. Call the script:
stage ('Build-Docker-Image') {
steps {
sh '/path/to/build-docker_image.sh'
}
}
In Jenkinsfile, I made sure the $PATH is including the path to Docker.
The issue was that I was appending the actual docker application like this /Applications/Docker.app/Contents/Resources/bin/docker, instead of the directory /Applications/Docker.app/Contents/Resources/bin

Jenkins run Shellscript via SSH is not leaving console

I'm using Jenkins to deploy my play application for this I've added SSH support to jenkins and I connect via ssh to the test server and then I run a shel script via ssh.
Thats working fine.
Not working ist finishing the job in jenkins.
The command in the the shell script is the following:
/usr/src/activator-dist-1.3.10/bin/activator "~ run" &
that only should run the activator, build and run the project
But then when The application is build and the activator runs the Jenkins job dosn't finish ... it always hang in console
looks like this:
When you run a script via ssh it will stay open until stdout/stderr are closed or a timeout occurs. In Jenkins it seems as if the script hangs.
So if you run a script as background job, make sure to redirect all its output to somewhere:
nohup yourCommand < /dev/null > /dev/null 2>&1 &
or
nohup yourCommand < /dev/null >> logfile.log 2>&1 &
See SSH Frequently Asked Questions for more details.

Resources