Jenkins-Publish over SSH plugin: Getting error on npm - node.js

I am trying to setup CI/CD pipeline for my node.js application with Jenkins.
What I am trying to do is login to my application server through jenkins and execute the commands on the server with the help of publish over ssh plugin
(https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plugin).
I've selected the Send Files or Execute commands over SSH option and added the following commands in Exec command section.
cd <project-folder> && git pull origin master && npm install
I am getting an error like npm not found but I try to directly on the server there is no error.
So I believe there's is a permission issue on jenkins but i can't find the solution

Yes, you identified the issue correctly ... Its permission issue ..
I assume you have already added the root login details at "SSH remote hosts" in the "configure system" section of the jenkins.
First pull the GIT repo to the jenkins workspace via web hook settings in github. Hope you have succeeded in this step also
In "Build Environment" select "Execute shell script on remote host using ssh" then use the following
cd <project-folder> (Ex: cd /var/lib/jenkins/workspace/<project-folder>)
npm install
NOTE: you should give the complete server path in the server to avoid permission .. If you are working with localhost then it may work and other very important when you use "Execute shell script on remote host using ssh" you should access via root user only NOT with the cpanel account login.. This will avoid permissions issue.
Once the NPM is install, check the console log for "success" Then do the other commands and suggest to remove the "npm install" as installation multiple times will increase the application compiling time..

Related

No access to remote repository

I am deploying my node.js app.
I am using git hooks and creating a remote repository.
In the image below you can see that i have added the remote 'adiproduction' to which i will push from my local repo.
Below i am pasting the image of content in post-receive file of hooks.
Following is the error when i try to push my code on 'adiproduction' remote.
ubuntu#35.154.65.179: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Edit:
I tried running the ssh -Tv ubuntu#35.154.65.179 command. And i got the following output.
try to run the code with "sudo"
sudo helps you to run the command with root privilege
and secondly, if you dont want use sudo so please set the ssh key on your git account, this link will helps you to set the ssh key-
'https://www.cyberciti.biz/faq/how-to-set-up-ssh-keys-on-linux-unix/'
and lastly if above solution not helps then please delete the current repository and try to clone again useing following command with ssh clone link-
git clone -b
thank you

Pulling a git repo from a startup script on google cloud compute engine

To show my team how the app that I am building is progressing, I created a small dev server on google cloud compute engine. This server is usually switched off to save cost and only is switched on when we are working together. I am developing and pushing to a git repo when the server is not on. When I start the server, the latest changes should be pulled, the node packages installed and the node server should be started. To do this I have created the following startup script:
#! /bin/bash
cd /to/my/server/folder
git pull
sudo npm install --no-progress
nohup node src/ &
I have created an ssh key and added that as a read only deploy key in my gitlab account on this particular repo. The script is tested on the server and works totally fine. Now the fun part.
When the script is run as a startup script (https://cloud.google.com/compute/docs/startupscript) it doesn't work. The error:
permission denied (public key)
fatal: could not read from repo make sure it exists.
I tried these fixes:
Getting permission denied (public key) on gitlab. The problem being that they can not pull git repos in general. In my case it works fine from command line, it works fine from shell script, but it just doesn't work from startup script.
I also tried a whole bunch of other stuff on the whole spectrum from 'could be it' to 'a wild guess'. Clearly there is something I am missing here. Could anyone help me out?
Finally found the answer here: https://superuser.com/a/868699/852795. Apparently something goes wrong with the SSH keys that are used in a google startup script. The solution is to explicitly tell git what key to use. Like this: GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa" git pull.

Openshift action_hook is not executable

I have recently create my first Openshift node application and currently trying to get the build scripts working when I push to the server via git (using action_hook).
I am writing the application using PhpStorm on Windows and have had no problem pushing files to the server.
I have create the following file: .openshift/action_hook/build which contains the following:
#!/bin/bash
webpack --config $OPENSHIFT_DATA_DIR/webpack.config.js
When I push to the server using git, I get the following error:
NOTE: The .openshift/action_hooks/build hook is not executable, to make it executable:
On Windows run: git update-index --chmod=+x .openshift/action_hooks/build
On Linux/OSX run: chmod +x .openshift/action_hooks/build
I have tried the windows command above but when running git ls-tree HEAD to check the file permission, I can see they have not changed:
100644 blob 1b82131d8b9e9c63b765ef328ecbbfb54f0c70aa build
Has anybody managed to get this working on windows?
Thanks a lot

Execute command after deploy AWS Beanstalk

I have problem with execute command after deploy, i have some node.js project and script, this script use some bin from node_modules, if i write my command for script in .ebextensions/.config he execute before npm install and return error ("node_modules/.bin/some": No such file or directory). How i can execute command after deploy. Thanks.
I found the following solution
I add to beanstalk config next command:
commands:
create_post_dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/some_job.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
cd /var/app/current
export PATH=$PATH:$(ls -td /opt/elasticbeanstalk/node-install/node-* | head -1)/bin
npm run some_script
This commands create(if not exist) folder for post-hooks scripts and adds bash script. Scripts in this folders execute only after npm install, this very important for my problem.
Thanks to this guy http://junkheap.net/blog/2013/05/20/elastic-beanstalk-post-deployment-scripts/
create a file called .ebextensions/post_actions.config:
container_commands:
<name of container_command>:
command: "<command to run>"
this will be executed after the code was extracted, but before it was launched.
A better approach would be to go with the aws platform hooks. Where you can define the postdeploy hooks AWS Patform Hooks
In that inside the project root directory you can add .platform/hooks/postdeploy/
Insdie this path you can create xxx-postdeploy-script.sh. Files here run after the Elastic Beanstalk platform engine deploys the application and proxy server.This is the last deployment workflow step
If you read the AWS ebextensions documentation they mention the execution, specifically where they mention that all commands are executed before the application version is deployed.
"You can use the container_commands key to execute commands for your
container. The commands in container_commands are processed in
alphabetical order by name. They run after the application and web
server have been set up and the application version file has been
extracted, but before the application version is deployed."
If you deploy it for a second time it should work; this is because your application is already unpacked. This however is not a working solution because every new instance that is spawned will error.

ThoughtWorks GO Add Pipeline - Failing to find GIT in PATH

"Failed to find 'git' on your PATH. Please ensure 'git' is executable by the Go Server and on the Go Agents where this material will be used."
I'm getting this when attempting to use GIT when creating a new build pipeline.
I'm not that experienced with Linux to know how to debug this and I can't find anything in the docs or via a google search.
GO was installed as root, so was GIT, the server has been rebooted since and I can run git in bash. "which git" shows that it is in /usr/bin. "export $path" shows that /usr/bin is in the PATH variable.
Can anyone recommend anything else to try?
Thanks.
The Go server and agent runs under user 'go' and therefore you will need to ensure git is in path for this user.
Change the user to go. If you get a prompt for password, put the password of the current logged in user.
$ sudo su - go
Then check if git is in path or simply check for the version
$ which git
$ git --version
If you don't get expected output for the above commands you might have to provide git executables to the user 'go', that is, you will need to add git in this user's PATH. Alternatively, you can provide the path in /etc/default/go-server
export PATH=$PATH:/path/to/git
I recall having this problem too, for me it was related to a local git repository that I did not share with the git daemon. Running the repository with the git daemon solved this problem for me.
URL for me starts with git://localhost, not sure if http will work too

Resources