Curl usage in Jenkins - jenkins

I have tried this command in Jenkins pipeline script
sh ‘curl ${BUILD_URL}consoleText —-output ${WORKSPACE}/logs/log.txt’
But it’s not resolving the WORKSPACE variable.Any idea how to proceed.

You can use it in below way:
def Workspace=env.WORKSPACE
sh "<Yourcommand> ${Workspace}"

Related

how to create a directory with datestamp as its filename in jenkins pipeline?

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:

Declarative pipeline - Running a shell command

I have a jenkins pipeline script that I am updating wish to use the following shell command:
sh script: """
export PATH=\"${PATH}\":\"${WORKSPACE}\"
BASE_DIR=$(dirname $0)
source "${BASE_DIR}/shellscript.sh"
helm uninstall ${helmReleaseName} --namespace ${kubenamespace}
"""
And the result always is:
Errors encountered validating Jenkinsfile:
I've played around with it.
But it fails the validation? The question is why?
Thanks
Declarative pipeline with 'sh' step will look like:
stage ("Preparing") {
steps {
sh'''
export PATH=\"${PATH}\":\"${WORKSPACE}\"
BASE_DIR=$(dirname $0)
source "${BASE_DIR}/shellscript.sh"
helm uninstall ${helmReleaseName} --namespace ${kubenamespace}
'''
}
}
Take a look here

Jenkins. Error when copying secret file to my workspace during build

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}\""

OWASP Dependency-check does not scan any issue with Jenkins pipeline but freestyle job

I am running OWASP dependency-check in Jenkins. However, when i run it with Jenkins freestyle job, it shows 2 vulnerabilities from the report. But with the same source code, i run it with Jenkin pipeline, it shows the report, but 0 vulnerabilities.
Here is my Jenkinsfile:
stage ('Dependency Check') {
steps {
dependencyCheck additionalArguments: '''
-o "./"
-s "./"
-f "ALL"
--prettyPrint''', odcInstallation: 'dependencycheck'
dependencyCheckPublisher pattern: 'dependency-check-report.xml'
}
}
Thank you in advance.

Running an Ansible playbook via Jenkins

I am using Jenkins to take a number of parameters, generate an ansible-playbook command and run it. My Jenkins server is also my Ansible server.
My shell says ::
echo $ESXi_IP
echo $VM_NAME
echo $NIC1_MAC
echo $NIC2_MAC
echo $NIC3_MAC
echo $NIC4_MAC
echo $ESXi_HOSTNAME
echo $PLAYBOOK
ansible-playbook $PLAYBOOK --extra-vars "esxi_ip=$ESXi_IP vm_name=$VM_NAME nic1_mac=$NIC1_MAC nic2_mac=$NIC2_MAC nic3_mac=$NIC3_MAC nic4_mac=$NIC4_MAC esxi_hostname=$ESXi_HOSTNAME"
When I run the Job, the output is ::
+ ansible-playbook /root/ansible/sc-ece.yaml --extra-vars 'esxi_ip=5.232.66.49 vm_name=JenkinsTest nic1_mac=00:50:C0:A8:01:02 nic2_mac=00:50:0A:A9:37:A5 nic3_mac=00:50:0A:FF:FE:4C nic4_mac=00:50:AC:10:01:65 esxi_hostname=tmolab13-14iamesxi4'
ERROR! the playbook: /root/ansible/sc-ece.yaml could not be found
The playbook path is correct. there is no issue in it at all.
What seems to be missing here ?
You are correct Matt & Dave. Permissions to the folder was an issue. Thanks !

Resources