I have a simple jenkins pipeline script that I have some sh code running. The important bit looks like this
script{
sh "cp folderA/file.txt newFolder/"
sh "cp -r folderB/* newFolder/"
sh "cp folderC/\\* newFolder/"
}
When I look at the console output of the jenkins pipeline I see the following
[Pipeline] sh
+ cp folderA/file.txt newFolder/
[Pipeline] sh
+ cp -r folderB/folder1 folderB/folder2 folderB/folder3 newFolder/
[Pipeline] sh
+ cp 'folderC/*' newFolder/
cp: cannot stat 'folderC/*': No such file or director
I've tried various combinations of quotes and escape characters, but jenkins/groovy will either expand the filenames (as in the second example), or add quotes around the 'folderA/*' (as in the third example) which confuses sh because folderC/ exists but 'folderC/' does not and the copy fails. Any idea how to do this properly?
I tried using fileCopyOperation already but ran into issues because I couldn't manipulate the file paths properly. ie: I couldn't figure out how to move many files/folders like folderA/folderB/file.txt to newFolder/file.txt without it becoming newFolder/folderA/folderB/file.txt
Related
I have a legacy project in Jenkins that hast to be pipelined (for
later parallelization), hence moving from simple tcsh script to
pipeline
running the script as
#!/bin/tcsh
source ./mysetting.sh
update
works but the same pipeline step fails due to missing alias expansion
stage ('update') {
steps {
//should be working but alias expansion fails
sh 'tcsh -c "source ./mysettings.sh; alias; update"'
//manually expanding the alias works fine
sh 'tcsh -c "source ./mysettings.sh; alias; python update.py;"'
}
}
calling alias in the steps properly lists all the set aliases, so I
can see them, but not use them.
I know in bash alias expansion has to be set
#enable shell option for alias_expansion
shopt -s expand_aliases
but in csh/tcsh that should be taken care of by source.
what am I missing?
found the solution:
sh '#!/bin/tcsh \n' +
'source ./mysettings.sh \n' +
'echo "Calling my alias" \n' +
'my_alias \n'
every line starting with sh launches a new shell, so it has to be in one line including line breaks.
further adding to the confusing was that documentation of jenkins says that it starts "a bash" but it launched /bin/sh which in my case pointed to something else
bat 'set OutputFolderName=%date:~12,2%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%'
bat 'mkdir %OutputFolderName%'
These two commands should give the correct output but they aren't working.
This is the error I got:
Try multiline bat command as follows:
bat """
set OutputFolderName=%date:~12,2%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
mkdir %OutputFolderName%
"""
Edit: Updated with snapshots
Have a look at my pipeline snapshot here:
Pipeline Console Output
Creates a folder something like this:
While running, my pipeline is duplicating the binaries located in a BitBucket workspace into the build workspace, then needs to add in the build workspace some secret files from the credential store, and then start to build the docker image.
But the pipeline is failing when copying the files.
I searched and applied different solutions found here but still have the same error.
Running the following command :
stage('push credential in jenkins workspace') {
steps {
script {
withCredentials([
file(credentialsId: 'saremediation', variable: 'SA_KEY_PATH')]){
sh "ls -al"
sh "mkdir ${CERTIFDIR}"
sh "cp ${SA_KEY_PATH} ${CERTIFDIR}/credent.json"
}
}
}
}
failed with the following error :
[Pipeline] sh
Warning: A secret was passed to "sh" using Groovy String interpolation, which is insecure.
Affected argument(s) used the following variable(s): [SA_KEY_PATH]
See https://jenkins.io/redirect/groovy-string-interpolation for details.
+ cp **** server/src/configuration/certificats/credent.json
cp: target 'server/src/configuration/certificats/credent.json' is not a directory
the CERTIFDIR folder is well created, because when I add sh "ls -al ${CERTIFDIR}", I cans see that the folder is created and empty.
fix the problem by applyong this syntax in the cp command
sh "cp \"${SA_KEY_PATH}\" \"${CERTIFDIR}\""
I am new to writting JenkinsFile
I was able to succesfully run mkdir and cd commands under Executable windows batch commands (Free style project)
But, now I want to write it inside JenkinsFile to use pipeline project
I have the below script which fails to cd into an existing directory
node('Windows-OS') {
def workspace = pwd()
stage('pre-build') {
checkout scm
}
stage('build') {
bat 'echo "Buils starting..."'
bat 'echo "CD"'
bat "cd '${workspace}\\CS'"
bat 'CD'
}
}
error: C:\Source\workspace\Win_Pipeline_Proj>cd 'C:\Source\workspace\Win_Pipeline_Proj\CS'
The filename, directory name, or volume label syntax is incorrect.
I even tried running bat "cd CS" but that didn't work either.
It worked fine using multiline batch commands
bat '''
echo "Buils starting..."'
CD CS
cd
'''
I try to make simple pipeline on Jenkins to remove files from few directories time to time. I decided not to create python script with Jenkinsfile as new project, instead of it I try to define new pipeline script in Jenkins job.
pipeline {
agent any
stages {
stage('Check virtualenv') {
steps {
sh """
rm -r /mnt/x/some/directory/Problem\ 1.0/path
"""
}
}
}
}
And I got an error WorkflowScript: 4: unexpected char: '\'. How can I use path with whitespace on it without using backslash? Any other ideas how define path?
The '\' character is a special character in Groovy. If you tried to compile this kind of code with the normal Groovy compiler, it would give you a better error message. The easiest way to handle it would be to escape it:
"""
rm -r /mnt/x/some/directory/Problem\\ 1.0/path
"""
You can modify the shell command as follows:
sh """
rm -r /mnt/x/some/directory/Problem""" + """ 1.0/path"""
Provide space before 1.0 as required. Hope this helps.