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

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

Related

groovy.lang.MissingPropertyException: No such property: GIT_PASSWORD for class: groovy.lang.Binding

I have checked all the topics on the same mistake, tried everything, and yet I was unable to find anything to help me solving my issue.
my code builds a docker container, starts a flask application and run a python script, run tests and then should merge Master. The code is the following :
pipeline {
agent any
stages {
stage('Master merging'){
steps{
script{
passwordVariable = 'password'
usernameVariable = 'unsername'
// Variables for input
if(env.BRANCH_NAME == 'features'||env.BRANCH_NAME == 'main'){
sh 'git checkout origin/features'
sh 'git pull'
sh 'git remote update'
sh 'git fetch'
sh 'git checkout origin/main'
sh 'git merge origin/features'
withCredentials([usernamePassword(credentialsId : 'GitHub', passwordVariable:'GIT_PASSWORD', usernameVariable:'GIT_USERNAME')]){
sh "git push http://${GIT_USERNAME}:${GIT_PASSWORD}#github.com/Username/Repo.git"
}
}}
}
}
}
}
when I arrive to stage('master merging'), I have the following mistake
Masking supported pattern matches of $GIT_PASSWORD
[Pipeline] {
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (container shutdown)
Stage "container shutdown" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: GIT_PASSWORD for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
Thanks a lot in advance for your help :)
I have an answer :)
Prerequisites : create a credential on Manage jenkins -> Manage credentials -> jenkins -> Global credentials (unrestricted) -> username with password. Inquire github username as username and Personal access token as password. ID is up to you.
Code :
stage('Master merging'){
steps{
script{
// Variables for input
if(env.BRANCH_NAME == 'features'||env.BRANCH_NAME == 'main'){
sh 'git checkout features'
sh 'git pull'
sh 'git remote update'
sh 'git fetch'
sh 'git checkout origin/main'
sh 'git merge features'
sh "git config user.email \"GITHUB EMAIL\""
sh "git config user.name \"GITHUB USERNAME\""
withCredentials([gitUsernamePassword(credentialsId:'ID you inquired in credentials')]) {
sh 'git push https://github.com/Username/Repo.git'
}
}
}
}
}

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

Jenkins. Invalid agent type "docker" specified. Must be one of [any, label, none]

My JenkinsFile looks like:
pipeline {
agent {
docker {
image 'node:12.16.2'
args '-p 3000:3000'
}
}
stages {
stage('Build') {
steps {
sh 'node --version'
sh 'npm install'
sh 'npm run build'
}
}
stage ('Deliver') {
steps {
sh 'readlink -f ./package.json'
}
}
}
}
I used to have Jenkins locally and this configuration worked, but I deployed it to a remote server and get the following error:
WorkflowScript: 3: Invalid agent type "docker" specified. Must be one of [any, label, none] # line 3, column 9.
docker {
I could not find a solution to this problem on the Internet, please help me
You have to install 2 plugins: Docker plugin and Docker Pipeline.
Go to Jenkins root page > Manage Jenkins > Manage Plugins > Available and search for the plugins. (Learnt from here).
instead of
agent {
docker {
image 'node:12.16.2'
args '-p 3000:3000'
}
}
try
agent {
any {
image 'node:12.16.2'
args '-p 3000:3000'
}
}
that worked for me.
For those that are using CasC you might want to include in plugin declaration
docker:latest
docker-commons:latest
docker-workflow:latest

jenkins pipeline nodeJs

My JenkinsFile script started throwing npm not found error. (it is working for maven but failing at npm)
pipeline {
environment {
JENKINS='true'
}
agent any
stages{
stage('change permissions') {
steps {
sh "chmod 777 ./mvnw "
}
}
stage('clean') {
steps {
sh './mvnw clean install'
}
}
stage('yarn install') {
steps{
sh 'npm install -g yarn'
sh 'yarn install'
}
}
stage('yarn webpack:build') {
steps {
sh 'yarn webpack:build'
}
}
stage('backend tests') {
steps {
sh './mvnw test'
}
}
stage('frontend tests') {
steps {
sh 'yarn test'
}
}
}
}
To fix that
I am trying to setup NodeJs on my jenkins node. I installed the nodejs plugin and wrote the script
pipeline {
agent any
stages {
stage('Build') {
steps {
nodejs(nodeJSInstallationName: 'Node 6.x', configId: '<config-file-provider-id>') {
sh 'npm config ls'
}
}
}
}
}
as shown in the https://wiki.jenkins.io/display/JENKINS/NodeJS+Plugin
I also setup nodejs on global tools config
I also tried the solution in the installing node on jenkins 2.0 using the pipeline plugin
and it throws
Expected to find ‘someKey "someValue"’ # line 4, column 7.
node {
error.
but I am still getting npm not found error on jenkins. I am new to jenkins so any help is appreciated.
Thanks in advance
I was able to fix the issues. Followed the following link and was able to fix the issue. https://medium.com/#gustavo.guss/jenkins-starting-with-pipeline-doing-a-node-js-test-72c6057b67d4
Its a puzzle. ;)
Has a little reference trick.
You need to configure your jenkins to see your nodejs config name.
At Global Tool Configuration, you need define your node config name. It has reference to your Jenkinsfile reference.
Look an Jenkingsfile adapted example with reference:
pipeline {
agent any
tools {nodejs "node"}
stages {
stage('Cloning Git') {
steps {
git 'https://github.com/xxxx'
}
}
stage('Install dependencies') {
steps {
sh 'npm i -save express'
}
}
stage('Test') {
steps {
sh 'node server.js'
}
}
}
}
Complete case to study: Post at Medium by Gustavo Apolinario
Hope it helps!
If you need different version of Node.js and npm, you can install NodeJS plugin for Jenkins.
Go to Manage Jenkins -> Global tools configuration and find NodeJS section.
Select the version you need and name it as you prefer. You can also add npm packages that needs to be installed globally.
In a declarative pipeline, just reference the correct version of node.js to use:
stage('Review node and npm installations') {
steps {
nodejs(nodeJSInstallationName: 'node13') {
sh 'npm -v' //substitute with your code
sh 'node -v'
}
}
}
Full example here: https://pillsfromtheweb.blogspot.com/2020/05/how-to-use-different-nodejs-versions-on.html

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