jenkins Permission denied node - node.js

These are the logs of my jenkins execution:
> Step 1/2 : FROM tomcat:9.0.4-jre8-alpine
---> 631138bc037d
Step 2/2 : COPY /Fatcat-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/fatcat.war
---> Using cache
---> 700d13ec5fb5
Successfully built 700d13ec5fb5
>
> [Pipeline] dockerFingerprintFrom
>
> [Pipeline] }
>
> [Pipeline] // stage
>
> [Pipeline] sh
>
> [unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA]
> Running shell script
>
> + docker inspect -f . 3bf1d418c6b61a03e2f2abe9d37b4c5e759e0b0a
>
> . [Pipeline] withDockerContainer Jenkins does not seem to be running
> inside a container
>
> $ docker run -t -d -u 1000:1000 -w /data/jenkins_ws/unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA
> -v /data/jenkins_ws/unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA:/data/jenkins_ws/unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA:rw,z
> -v /data/jenkins_ws/unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA#tmp:/data/jenkins_ws/unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA#tmp:rw,z
> -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** 3bf1d418c6b61a03e2f2abe9d37b4c5e759e0b0a cat
> $ docker top f3826ded71315b33b4d352ac2181035655f73d58a7c998040db36b6d664f1421 -eo
> pid,comm
>
> [Pipeline] { [Pipeline] stage [Pipeline] { (Test) [Pipeline] sh
> [unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA]
> Running shell script
> + node --version /data/jenkins_ws/unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA#tmp/durable-f0e86b83/script.sh:
> line 1: node: Permission denied [Pipeline] }enter code here [Pipeline]
> // stage [Pipeline] }
> `$ docker stop --time=1`
> f3826ded71315b33b4d352ac2181035655f73d58a7c998040db36b6d664f1421
>
> $ docker rm -f f3826ded71315b33b4d352ac2181035655f73d58a7c998040db36b6d664f1421
>
> [Pipeline] // withDockerContainer [Pipeline] } [Pipeline] // node
> [Pipeline] End of Pipeline ERROR: script returned exit code 127
> Finished: FAILURE
The issue is in this line:
> [unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA] Running shell script
>
> + node --version
>
> > /data/jenkins_ws/unFatCat_master-RSJASSVAHZJXKMANQQZOIIGTV3YC2D2GJS65WJ23OAY3WGXOVRJA#tmp/durable-f0e86b83/script.sh:
> > line 1: node: Permission denied
I want to use a local ad-hoc Docker. This is the Dockerfile:
FROM node:7-alpine
FROM tomcat:9.0.4-jre8-alpine
COPY /Fatcat-1.0-SNAPSHOT.war /usr/local/tomcat/webapps/fatcat.war
Notice I load the node:7-alpine in the Docker and not in the Jenkins configuration
This is my Jenkinsfile:
pipeline {
agent { dockerfile true }
stages {
stage('Test') {
steps {
sh 'node --version'
sh 'mvn --version'
}
}
}
}
I installed Nodejs plugin in Jenkins.
These are my questions:
Why node --version is not executed?
why the permission denied error is reported?
Can the problem be that node --version is not executed inside the container?
I already checked the permission denied error from this link. However the partition is mounted with exec parameter . So the proposed solution is not valid for me.

Change your Jenkins Declaritive pipeline in below format. Hope you will get the result.
pipeline {
agent {
docker {
image 'maven:3-alpine'
image 'node:7-alpine'
}
}
stages {
stage('Test') {
steps {
sh 'node --version'
sh 'mvn --version'
}
}
}
}

Related

How to hide parameters such as APIKey from Jenkins pipeline console output

How can I hide certain parameters or **** them when I execute scripts inside stage.
The command that is producing the output which I want to hide is:
sh "./wsagent_execute.sh -s -apiKey ${WHITESOURCE_API_KEY} -projectToken ${WHITESOURCE_PROJECT_TOKEN} -C ${configPath} -d ${directoryPath} -logLevel info"
The parameters I want to hide are -apiKey and -projectToken. How can I do it?
If you get your credentials from a vault, you can use Mask Passwords plugin. It does not state that it supports Pipelines but actually it does.
pipeline {
agent any
stages {
stage('doing something') {
steps {
script {
def current_nano = "1616407597607795668"
sh label: "Now you see it", script: "echo ${current_nano}"
maskPasswords(varPasswordPairs: [[password: current_nano, var: 'IGNORE']]) {
sh label: "Now you don't", script: "echo ${current_nano}"
}
}
}
}
}
}
Output:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/
[Pipeline] {
[Pipeline] stage
[Pipeline] { (doing something)
[Pipeline] script
[Pipeline] {
[Pipeline] sh (Now you see it)
+ echo 1616407597607795668
1616407597607795668
[Pipeline] maskPasswords
[Pipeline] {
[Pipeline] sh (Now you don't)
+ echo ********
********
[Pipeline] }
[Pipeline] // maskPasswords
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Are these keys calculated in the pipeline or are they statics?
You can try to use it like credentials in Jenkins and you don't see their values in the log.
If you use a secret define in Jenkins credential, Jenkins will automatically mask that for you and in the log, it will show as ****
let's say you have defined your apiKey in jenkins credential with Id: apikey. Than you can use that on your pipeline like below example.
more details can be found here
node() {
withCredentials([string(credentialsId: 'apikey', variable: 'TOKEN')]) {
sh "./wsagent_execute.sh -s -apiKey $TOKEN -projectToken ${WHITESOURCE_PROJECT_TOKEN} -C ${configPath} -d ${directoryPath} -logLevel info"
}
}
If you are not happy with that use mask password or something similar plugins

How to access value of a jenkins groovy variable in shell script for loop

When i am passing value of a variable declared in jenkins Groovy script its value is not retained in for loop which is running on a remote server. Strange thing is i am able to access the same value outside the for loop.
Here is the sample code i am trying to use
#!/usr/bin/env groovy
def config
def COMMANDS_TO_CHECK='curl grep hello awk tr mkdir bc'
pipeline {
agent {
label "master"
}
stages {
stage ('Validation of commands') {
steps {
script {
sh """
#!/bin/bash
/usr/bin/sshpass -p passwrd ssh user#host << EOF
hostname
echo $COMMANDS_TO_CHECK ---> This is printed
for CURRENT_COMMAND in \$COMMANDS_TO_CHECK
do
echo ${CURRENT_COMMAND} ---> Why This is not printed?
echo \${CURRENT_COMMAND} ----> Why This is not printed?
done
hostname
EOF
exit
"""
}
}
}
}
}
Output
[workspace#3] Running shell script
+ /usr/bin/sshpass -p passwrd ssh user#host
Pseudo-terminal will not be allocated because stdin is not a terminal.
illinsduck01
curl grep hello awk tr mkdir bc
illinsduck01
+ exit
You can wrap sh in """ ... """ as below
#!/usr/bin/env groovy
def config
pipeline {
agent {
label "master"
}
stages {
stage ('Validation of commands') {
steps {
script {
sh """#!/bin/sh
/usr/bin/sshpass -p password ssh username#hostname << EOF
COMMANDS_TO_CHECK="curl grep hello awk tr mkdir bc"
hostname
echo \$COMMANDS_TO_CHECK
for CURRENT_COMMAND in \$COMMANDS_TO_CHECK
do
echo \$CURRENT_COMMAND
which \$CURRENT_COMMAND
status=\$?
if [ \${status} -eq 0 ]
then
echo \${CURRENT_COMMAND} command is OK
else
echo "Failed to find the \${CURRENT_COMMAND} command"
fi
done
hostname
EOF
exit
"""
}
}
}
}
}

Getting permission denied when trying to run a shell script on jenkins pipeline

I'm trying to set a Jenkins pipeline to build an Ionic app inside a Linux server (ec2 instance on amazon services) My first stage inside the jenkinsfile is to run npm install, but it returns permission denied.
I've tried setting permissions to the folder using:
chmod 777 /home/ec2-user/.nvm/versions/node/v10.16.0/bin
I've also tried adding the Jenkins user to the group that also has permissions. none of these seemed to work.
This is my Jenkinsfile
pipeline {
agent any
environment {
PATH='/usr/local/bin:/usr/bin:/bin'
}
stages {
stage('NPM Setup') {
steps { sh '/home/ec2-user/.nvm/versions/node/v10.16.0/bin/npm install' }
}
stage('Android Build') {
steps {
sh 'ionic cordova build android --release'
}
}
stage('APK Sign') {
steps {
echo "Sign Android APK Action"
}
}
stage('Zip APK') {
steps {
echo "Zip the APK Action"
}
}
}
}
I get this output
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (NPM Setup)
[Pipeline] sh
+ /home/ec2-user/.nvm/versions/node/v10.16.0/bin/npm install
/var/lib/jenkins/workspace/p-ionic4_borderapp_ionic4_master#tmp/durable-9b0ecc49/script.sh: line 1: /home/ec2-user/.nvm/versions/node/v10.16.0/bin/npm: Permission denied

replacing file variables by envsubst in jenkins pipeline

I want to replace some variables in a file having $variablename, at runtime from jenkins pipeline script. It seems envsubst is the best for my use case. When i execute by command line on linux server its working fine but when i'm executing through jenkins pipeline in sh script, nothing happens.
sonar-scanner.properties:
sonar.projectKey=Project:MavenTest$BRANCHNAME
sonar.projectName=MavenTest$BRANCHNAME
Example of Command line on linux box:
$ export BRANCHNAME=develop
$ envsubst '$BRANCHNAME'
Output:
sonar.projectKey=Project:MavenTestdevelop
sonar.projectName=MavenTestdevelop
But when i'm executing through jenkins file as a script, nothing is changed in file.
jenkins script:
node {
stage('checkout'){
checkout([$class: 'GitSCM', branches: [[name: ':^(?!origin/master$|origin/develop$).*']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c0ce73db-3864-4360-9c17-d87caf8a9ea5', url: 'http://172.16.4.158:17990/scm/ctoo/testmaven.git']]])
}
stage('initialize variables'){
// Configuring BRANCH_NAME variable
sh 'git name-rev --name-only HEAD > GIT_BRANCH'
sh label: '', script: 'cut -d \'/\' -f 3 GIT_BRANCH > BRANCH'
branchname = readFile('BRANCH').trim()
env.BRANCHNAME = branchname
}
stage('build & SonarQube analysis') {
withSonarQubeEnv('Sonar') {
sh "envsubst '$BRANCHNAME' <sonar-scanner.properties"
}
}
}
Output:
[Pipeline] sh (hide)
envsubst repotest
sonar.projectKey=Project:MavenTest$BRANCHNAME
sonar.projectName=MavenTest$BRANCHNAME
Can someone please help me
Hi I don't have idea about the envbust but this can be achieved by passing sonar parameters via command line to the sonar see the below example:
withSonarQubeEnv('Sonar') {
sh "<sonarscanner path> -Dsonar.projectKey=Project:MavenTest$BRANCHNAME"
}
I had this problem and solved it by using his escape character
\,
for example:
sh "envsubst '\${SERVER_NAME}' < ./config/nginx/nginx.conf.template > ./config/nginx/nginx.conf"

Jenkins Pipeline - Access environment variable in nodejs builds

i need some help with my Jenkins pipeline.
What i want to do is trigger some builds in a specific order to login to a platform, deploy apps and logout again.
The builds are implemented as nodejs scripts and store data and access data from previous builds by using environment variables.
My problem is that when i'm trying to access environment variables from within my nodejs scripts i always receive undefined. For example: In the 'LoginToPlatform' build i'm using process.env.username to receive the username but even though i set the environment variable in the environment block of my pipeline i receive undefined.
So my question is how can i access the environment variables from within my builds (nodejs scripts)?
Here is the build order:
1. 'LoginToPlatform'
Uses process.env.username and process.env.password
Sets process.env.session
2. 'DeployOnPlatform'
Uses process.env.session
3. 'LogoutFromPlatform'
Uses process.env.session
My Jenkins Pipeline:
pipeline {
agent any
environment {
username = 'abc'
password = 'asdf'
}
stages {
stage ('Login') {
steps {
echo 'Login.'
build job: 'LoginToPlatform'
}
}
stage ('Deployment') {
steps {
echo 'Deployment.'
build job: 'DeployOnPlatform'
}
}
stage ('Logout') {
steps {
echo 'Logout.'
build job: 'LogoutFromPlatform'
}
}
}
}
Here is one of the ways to access env variables.
Jenkinsfile
pipeline {
agent {
docker {
image 'node:6-alpine'
}
}
environment {
VARIABLE_1="10"
VARIABLE_2="7"
}
stages {
stage('Build') {
steps {
sh 'node main.js'
}
}
}
}
main.js
const envOne = process.env.VARIABLE_1;
const envTwo = process.env.VARIABLE_2;
console.log("envOne: " + envOne);
console.log("envTwo: " + envTwo);
Output:
Jenkins seems to be running inside container 646633d29eac6e0e5b56e4aef28055075b5a2274e26b159387a7a34f35919fe3
$ docker run -t -d -u 0:0 -p 3000:3000 -w /var/jenkins_home/workspace/fff_master-RUQD36MGKNUXMF26H5CQBCDE6AKFWFLUOG7MTQ6WMTXNXKQHCNMA --volumes-from 646633d29eac6e0e5b56e4aef28055075b5a2274e26b159387a7a34f35919fe3 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** node:6-alpine cat
$ docker top 3fe3059a78e890dc2cadd722c25b97d5a023da059cc807cef3acb29237f0261f -eo pid,comm
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[fff_master-RUQD36MGKNUXMF26H5CQBCDE6AKFWFLUOG7MTQ6WMTXNXKQHCNMA] Running shell script
+ node main.js
envOne: 10
envTwo: 7
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
$ docker stop --time=1 3fe3059a78e890dc2cadd722c25b97d5a023da059cc807cef3acb29237f0261f
$ docker rm -f 3fe3059a78e890dc2cadd722c25b97d5a023da059cc807cef3acb29237f0261f
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

Resources