Jenkins stash is not stashing all files and folders - jenkins

I have Jenkinsfile, Im trying to stash BuildApp
and unstash it into other docker container to run npm test.
but seems like it's missing node_modules, so npm test fails.
steps {
dir("/var/jenkins_home") {
unstash "app"
}
echo "Building app (CLOUD_ENV = ${env.CLOUD_ENV})"
sh 'yarn install'
stash name: "BuildApp", includes: "*"
sh "ls -la ${pwd()}"
}
That's where I'm trying to stash and I did debug with "ls -la ${pwd()}"
and I see that node_modules are there
but when unstash them
steps {
dir("/var/jenkins_home") {
unstash "BuildApp"
}
echo "Testing app (CLOUD_ENV = ${env.CLOUD_ENV})"
// unstash 'builtApp'
sh "ls -la ${pwd()}"
sh 'npm test > test.out'
archiveArtifacts artifacts: 'test.out', fingerprint: true
stash name: "testApp", includes: "*"
}
I did "ls -la ${pwd()}" and I can see that node_modules folder is not there.
Am I doing something wrong?

I guess that * only includes files in the current directory. In that case ** should do the trick since it also matches path separators.
Also in your code you unstash in a different directory than you execute ls. I don't see any reason for the dir command, so I suggest to remove it.

Related

How to specify JDK version in Jenkinsfile pipeline script

I have a pipeline script to deploy applications to server. I'm building project using maven, I want Jenkins to use specified JDK version for building the project. My pipeline script looks like this:
pipeline {
agent any
tools {
// Install the Maven version configured as "M3" and add it to the path.
maven "Maven 3.6.3"
}
stages {
stage('Build') {
steps {
// Run Maven on a Unix agent.
sh "mvn clean package -DskipTests=true -U"
}
post {
// If Maven was able to run the tests, even if some of the test
// failed, record the test results and archive the jar file.
success {
archiveArtifacts "**/${war}"
}
}
}
stage('Deploy EQM Instance 1') {
steps {
sshagent(credentials: ['credentials']) {
sh "echo 1"
sh "echo Initializing deployment to Instance 1"
sh "scp target/${war} ${bastionHost}:/home/opc"
sh "echo 2.1"
sh "echo Uploaded war file to bastion instance"
sh "scp ${bastionHost}:/home/opc/${war} opc#${instanceDns}:/home/opc"
sh "echo 3.2"
sh "echo Uploaded war file from bastion instance to Instance 1 ${instanceDns}"
sh "echo Undeploying old war file"
sh "ssh ${bastionHost} -tt ssh opc#${instanceDns} sudo rm /opt/tomcat/webapps/${war}"
sh "echo 4.2.2"
sh "ssh ${bastionHost} -tt ssh opc#${instanceDns} sudo chown tomcat:tomcat -R ${war}"
sh "echo Deploying new war file"
sh "ssh ${bastionHost} -tt ssh opc#${instanceDns} sudo mv ${war} /opt/tomcat/webapps/"
sh "echo 4.3"
}
}
}
}
There are other already configured on Jenkins, I don't want to disturb them. So I want to specify JDK version in desired job configuration.

Jenkins cannot find sh script in git repo

In my git repo I have build script located in /Src/build.sh. I have set up simple Jenkins file which runs this script:
node {
stage 'Checkout'
checkout scm
stage 'Build'
sh "chmod +x ./Src/build.sh"
sh "./Src/build.sh"
}
Execution of chmod seems to be successfull, but running build.sh gives me this error:
+ ./Src/build.sh
/var/lib/jenkins/workspace/wrksp#tmp/durable-d3f345e7/script.sh: 1:
/var/lib/jenkins/workspace/wrksp#tmp/durable-d3f345e7/script.sh: ./Src/build.sh: not found
I have no idea what is wrong?

Jenkins pipeline doesn't copy files generated in previous stage

I am setting up a Jenkins pipeline to deploy a PHP application. The application is using composer, so I am running composer install -o in the script to ensure that all dependencies are there. The test setup also ensures that the vendor/autoload.php is generated (it's in the phpunit.xml bootstrap config)
My scripts are based on https://modess.io/jenkins-php/ and http://jenkins-php.org/
My issue is that the vendor folder is not included, and the generated config.inc.php is not included.
The Jenkins log shows that my deployment line sh "cp -rp ${SOURCE_DIR}/* ${DEPLOY_DIR}" is being expanded into cp -rp src/globals.template.inc.php src/index.php src/phpinfo.php /usr/nasShare/htdocs/sometest which is not including the mentioned files. (in fact those are the only files in the src directory in SCM. Looking in the working directory on the jenkins server, the files are generated... )
Jenkinsfile
#!groovy
pipeline {
agent any
environment {
SOURCE_DIR="src"
TEMPLATE_FILE="globals.template.inc.php"
CONFIG_FILE="globals.inc.php"
}
stages {
stage ('Testing'){
steps {
echo "Running ant clean"
sh 'ant clean'
echo "running composer"
sh "composer install -o -d ${SOURCE_DIR}"
echo ""
sh 'ant quick-build'
}
}
stage ('Staging'){
steps {
echo "Building config file"
script {
def inptext = readFile file: "${SOURCE_DIR}/${TEMPLATE_FILE}"
inptext = inptext.replaceAll(~/¤GIT_BRANCH¤/, "${GIT_BRANCH_NAME}")
inptext = inptext.replaceAll(~/¤GIT_COMMIT¤/, "${sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()}")
inptext = inptext.replaceAll(~/¤GIT_TAG¤/, "${sh(returnStdout: true, script: "git -C . describe --tags").trim()}")
writeFile file: "${SOURCE_DIR}/${CONFIG_FILE}", text: inptext
}
}
}
stage ('Remote Deploy'){
agent any
when{
//https://stackoverflow.com/a/44231270/1725871
environment name: 'DEPLOY_TYPE', value: 'remote'
}
steps {
echo "Deploying via SSH on ${SSH_SERVER_NAME}:${DEPLOY_DIR}"
//TODO: rename backup file
sh "ssh ${SSH_USERNAME}#${SSH_SERVER_NAME} tar -cvpzf BACKUP_FNAME ${DEPLOY_DIR}/* "
sh "ssh ${SSH_USERNAME}#${SSH_SERVER_NAME} rm -R ${DEPLOY_DIR}/*"
sh "scp -rp ${SOURCE_DIR}/* ${SSH_USERNAME}#${SSH_SERVER_NAME}:${DEPLOY_DIR}"
//TODO: delete backup on success
}
}
stage ('Local Deploy'){
agent any
when{
environment name: 'DEPLOY_TYPE', value: 'local'
}
steps {
echo "Deploying to ${DEPLOY_DIR} "
//TODO: backup existing files
sh "rm -R ${DEPLOY_DIR}/*"
sh "cp -rp ${SOURCE_DIR}/* ${DEPLOY_DIR}"
}
}
}
}
I found the error of my way. Adding ${WORKSPACE}/ to my SOURCE_DIR variable solved it. now all the expected files are being copied.
Working environment from jenkinsfile
environment {
SOURCE_DIR="${WORKSPACE}/src"
TEMPLATE_FILE="globals.template.inc.php"
CONFIG_FILE="globals.inc.php"
}

Jenkins stash not stashing files

I need to stash 2 ear files to be unstashed in the next stage.
This is my code to stash the files.
steps {
sh 'chmod +x gradlew'
echo "Building tms-load and tms-loadRemote ear files"
script {
sh "./gradlew -PjdkHome=${env.JAVA_HOME} -PweblogicHome=${WEBLOGIC_PATH} -Penv=at2 buildAll"
}
stash includes: "./build/staging/deploy/tms/AT2/*", name: "tmsLoadEars"
}
There are 2 ear files in this directory.
The gradle successfully builds the files.
The ant echo shows this:
[ant:echo] Deploying to ear area
'build/staging/deploy/tms/AT2/tms-load'.
The file name is 'tms-load.ear'
When I try to unstash and deploy, I get the error that there were no files included in the stash.
This is my unstash code:
script {
println JAVA_BIN_PATH;
dir('.') {
unstash "tmsLoadEars"
}
sh '''
. ~/.bash_profile
pghAdminConsole="<server url set here>"
wlLevel="L0"
"${JAVA_BIN_PATH}"/java -Xms512M -Xmx512M -cp "${WEBLOGIC_PATH}"/server/lib/weblogic.jar weblogic.Deployer \
-debug -stage -remote -verbose -upload \
-source ./build/staging/deploy/tms/AT2/tms-load.ear \
-targets $cluster -adminurl t3://$pghAdminConsole \
-username <username here> -password <password here> -deploy
'''
}
What am I doing wrong?
Added stash to the gradle command:
steps {
sh 'chmod +x gradlew'
echo "Building tms-load and tms-loadRemote ear files"
script
{
sh "./gradlew -PjdkHome=${env.JAVA_HOME} -PweblogicHome=/opt/weblogic/wl12.1.3.0/wlserver -Penv=at2 buildAll"
}
stash name: 'loadEarL0', includes: '**/tms-load.ear'
stash name: 'loadRemoteEarL0', includes: '**/tms-loadRemote.ear'
}
Then unstashed in the next step:
unstash "loadEarL0"
Defined the source switch in the Deploy program like this:
-source ./build/staging/deploy/tms/AT2/app/tms-load.ear \

Cannot conditionally remove a directory inside the workspace using basic Jenkins pipeline steps

I am trying to remove the directory junit located in the workspace of my Jenkins job using scripted Pipeline which looks somewhat like this:
node {
stage('Build') {
checkout scm
app = docker.build("...")
}
stage('Test') {
app.withRun("--name = ${CONTAINER_ID} ...") {
// sh "mkdir -p junit"
// sh "rm -rf junit/"
dir "junit" {
deleteDir
}
sh "docker exec ${CONTAINER_ID} /bin/bash -c 'source venv/bin/activate && python run.py test -x junit'"
sh "docker cp ${CONTAINER_ID}:/home/foo/junit junit"
}
}
junit 'junit/*.xml'
}
However I am getting the following (red haring?) error, e.g.
java.lang.ClassCastException:
hudson.tasks.junit.pipeline.JUnitResultsStep.testResults expects class
java.lang.String but received class
org.jenkinsci.plugins.workflow.cps.CpsClosure2
However when I am using the shell steps:
sh "mkdir -p junit"
sh "rm -rf junit/"
It works as expected. What am I doing wrong?
Try to use parentheses:
dir ("junit") {
deleteDir()
}

Resources