SYSDATE with TimeStamp Format need to include in Jenkins Build - jenkins

Anyone know how to get SYSDATE while running Jenkins build, it should be in this format: 24Aug20221005 (sysdatewithtimestamp)
please let me know.
Thanks,
Harish.

You can use the command date +%d%b%Y%H%M to get a date like 25Aug20221431
If you want to change the date you can simply look this up yourself with date --help.
You can get this in the Jenkinsfile as a environment variable like this:
env.MY_DATE = sh(
script: """
date +%d%b%Y%H%M
""", returnStdout: true
)
or in a bash script like this:
sh '''
MY_DATE=$(date +%d%b%Y%H%M)
echo $MY_DATE
'''

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:

Jenkins plugin to make REST calls and fetch output in JSON

Please suggest a plugin or an approach to achieve the following in Jenkins declarative pipeline.
You don't need any plugin per se, as you can use e.g. curl:
def calendar_url = "https://calendarific.com/api/v2/holidays?&api_key=758f54db8c52c2b500c928282fe83af1b1aa2be8&country=IN&year=2020"
def curl_output = sh returnStdout: true, script: "curl -s ${calendar_url}"
You can convert it to json with readJson utility:
def holidays = readJson text: curl_output
for (holiday in holidays.response.holidays) {
def holiday_date = holiday.date.iso
println holiday_date
}
Defining the current date and bailing out of the build is left as an exercise for the implementor :)

Problem with escaping double quote Jenkinsfile

I start building a Jenkins job with a Jenkinsfile. In one of my steps, I create a variable. This variable echo's as expected, now I try to insert this variable into a new function and need to escape, everything looked fine until I see the last double quote on a new line and not on the end.
The line of code that create my variable is:
gitTag = sh(returnStdout: true, script: "git describe --tags--always")
The line code that use this variable:
sh "echo WEBSERVICE_VERSION=\"$gitTag\" > ${WORKSPACE}/webservice/src/webservice_version.py"
expected output WEBSERVICE_VERSION="$gitTag", but the output I see is WEBSERVICE_VERSION=$gitTag.
Did I make any mistake in escaping?
This should work for you
node('master'){
def gitTag="mytag"
sh "touch webservice_version.py"
sh "echo \\\"${gitTag}\\\" > webservice_version.py"
sh "cat webservice_version.py"
}

Groovy multiline shell script in Jenkins sh step does not return stdout

I am trying to save the output of a groovy shell script in a variable.
test = sh(returnStdout: true, script: "#!/bin/bash -l && export VAULT_ADDR=http://ourVault.de:8100 && export VAULT_SKIP_VERIFY=true && vault auth ${VAULT_TOKEN} && vault read -field=value test/${RELEASE2}/ID").trim()
But there is no output and I wonder why it does not capture the output?
If I do this:
def test = ""
sh"""#!/bin/bash -l
export VAULT_ADDR=http://ourVault.de:8100
export VAULT_SKIP_VERIFY=true
vault auth ${VAULT_TOKEN}
${test}=\"\$(vault read -field=value emea/test/hockey/ios/${RELEASE2}/appID)\"
"""
I see the output in the console. However, it doesn't get captured either. Is there any other way of capturing the output of multiline sh script?
The ${} syntax is not working that way. It can only be used add content to a string.
The returnStdout option can also be used with triple quoted scripts. So you probably want to do the following:
def test = sh returnStdout:true, script: """
#!/bin/bash -l
export VAULT_ADDR=http://ourVault.de:8100
export VAULT_SKIP_VERIFY=true
vault auth ${VAULT_TOKEN}
echo "\$(vault read -field=value emea/test/hockey/ios/${RELEASE2}/appID)" """

How to set the output of sh to a Groovy variable? [duplicate]

This question already has answers here:
Is it possible to capture the stdout from the sh DSL command in the pipeline
(7 answers)
Closed 6 years ago.
Is it possible to have the output of the sh command be set to a Groovy variable? It seems to be setting it to the status of the command instead.
Example input:
node {
stage "Current Date"
def curDate = sh "date"
echo "The current date is ${curDate}"
}
Results in the following output:
Entering stage Current Date
Proceeding
[Pipeline] sh
[workspace] Running shell script
+ date
Tue May 10 01:15:05 UTC 2016
[Pipeline] echo
The current date is 0
It is showing The current date is 0, I want it to show The current date is Tue May 10 01:15:05 UTC 2016 which you can see has been output by the sh command. Am I going about this all wrong?
Yes, sh is returning the exit status. Currently your best bet is:
sh 'date > outFile'
curDate = readFile 'outFile'
echo "The current date is ${curDate}"
ADDENDUM: after this answer was written a new option was added to the sh step, use returnStdout: true to get the result string from the sh call.

Resources