While writing pipelines in Jenkins using Groovy, we can interchangeably use echo and println statements. Is there any difference between these statements?
For example,
buildNumber = "1.10";
echo "BUILD #${buildNumber}";
println "BUILD #${buildNumber}";
Related
How do I access this variable in a shell script.
I have tried
echo params.$STATES;
echo $STATES;
Output for the first one is params. . Output for the second one is an empty string. The output I am expecting is the string I am passing when I build that job with parameters.
If you are using the 'Execute Shell' block in a Freestyle Project, you will need a $ (dollar sign) before the variable. Since you already tried this, there could be an issue not wrapping variable within {}
try echo ${STATES}
More info on curly braces around variables,
See: codeforester answer on usage of curly braces around shell variables
If you are using a Jenkinsfile without a shell block (valid in Scripted and Declarative)
Use dollar sign with double quotes
node(){
echo "$STATES"
}
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo "$STATES"
}
}
}
}
or without Double quotes and dollar sign
node(){
echo STATES
}
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo STATES
}
}
}
}
If you are using a Shell block in the Jenkinsfile pipeline, use
sh "echo $STATES" or sh "echo ${STATES}"
(as in the Freestyle Execute Shell block, Double quotes are needed for interpolation)
See
String interpolation in Jenkinsfiles
Handling parameters in Jenkinsfiles
I have a parameterized pipeline, accorindg to the user input, then some values are assigned to the variables, if you see the code there are 2 echo sections, one just after the switch statement, and another one within the pipeline steps in "preparation" stage, the first set of "echos" is displaying the correct values of the vars, while the second set of "echos" withing the pipeline steps is not, so it looks like the variables are not being received in the pipeline, how can I get this done?, (in the example below "Stress_Test" was seleted).
def local_path
def branch
def script_file
def datagen_file
switch (TEST_TYPE) {
case "Smoke_Test":
branch = "SmokeTest"
local_path = "/e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/${BRANCH}"
script_file = "PE_TCP_RESTAPI_June2020_SMK.jmx"
datagen_file= "TCP_JMeter_DataFiles_smk.yaml"
break
case "Regular_Load":
branch = "Regular"
local_path = "/e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/${branch}"
script_file = "PE_TCP_RESTAPI_July2020_Regular.jmx"
datagen_file= "TCP_JMeter_DataFiles_regular.yaml"
break
case "Peak_Load":
branch = "PeakTest"
local_path = "/e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/${branch}"
script_file = "PE_TCP_RESTAPI_July2020_Peak.jmx"
datagen_file= "TCP_JMeter_DataFiles_peak.yaml"
break
case "Stress_Test":
branch = "StressTest"
local_path = "/e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/${branch}"
script_file = "PE_TCP_RESTAPI_July2020_Stress.jmx"
datagen_file= "TCP_JMeter_DataFiles_stress.yaml"
break
default:
println "Test type was not set!"
break
}
echo "test type selected ${TEST_TYPE}"
echo "branch to checkout ${branch}"
echo "path in local ${local_path}"
echo "script name ${script_file}"
echo "datagen file name ${datagen_file}"
pipeline {
agent any
stages {
stage('Preparation...') {
steps{
sh '''
echo "test type selected ${TEST_TYPE}"
echo "branch to checkout ${branch}"
echo "path in local ${local_path}"
echo "script name ${script_file}"
echo 2datagen file name ${datagen_file}"
echo "****************************************"
*rest of the code is not relevant.
and this is what I'm getting in the output...
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
test type selected Stress_Test
[Pipeline] echo
branch to checkout StressTest
[Pipeline] echo
path in local /e/jmeter/apache-jmeter-5.3/bin/tcp-performance-engineering/StressTest
[Pipeline] echo
script name PE_TCP_RESTAPI_July2020_Stress.jmx
[Pipeline] echo
datagen file name TCP_JMeter_DataFiles_stress.yaml
[Pipeline] node
Running on Jenkins in E:\jenkins\workspace\TCP_Performance_Test_V1
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Preparation...)
[Pipeline] sh
+ echo 'test type selected Stress_Test'
test type selected Stress_Test
+ echo 'branch to checkout '
branch to checkout
+ echo 'path in local '
path in local
+ echo 'script name '
script name
This is my first pipeline, any help will be really appreciated.
Jenkins pipelines are written in groovy and in groovy, ' is used for literal strings that do not support variable interpolation. In the question, you are using sh ''' ... ''' that makes everything within that block literal and executing as is. Meaning, all the variables are looked in the shell environment and not in the pipeline.
To fix, either change sh ''' ... ''' to sh """ ... """ or remove the sh block altogether.
Have a look at 4. Strings section of groovy documentation.
In my scripted Jenkinsfile I have a line that runs parallel deployments . I omitted the other stages and code for security.
When I run this, it can't find the reportUrl and I get the error: groovy.lang.MissingPropertyException: No such property: teamsUrl for class: groovy.lang.Binding.
However, if I run without the parallel deployments it works and I'm able to reach the value of reportUrl. I tested with echo statements. Any thoughts?
Am I not exiting the parallel statement properly?
stage("Deploy") {
def deployments = [:]
// Code here not pasted
parallel deployments
echo "Deployed to clusters"
}
stage('Reporting') {
def reportUrl = 'https://testurl'
echo "${reportUrl}"
sh """
./my-tool report deploy \
--report-url "${reportUrl}" \
--force
"""
}
EDIT 6/22
reportUrl comes from a groovy file: example.groovy
example = load("deploy/example.groovy")
def reportUrl = example.REPORT_URL['report']
//I can see the url being pulled correctly here
echo "${reportUrl}"
stage('Test Application'){
agent { label 'windows' }
steps{
script{
def appName = "${params.ApplicationName}"
println appName
def appName1 = "\"Projects/$appName\""
println appName1
bat '''cd cognizant-intelligent-test-scripter-1.1-setup
run.bat -run -project_location appName1 -scenario "Scenario1" -testcase "TC2" -browser "IE" -standalone_report'''
}
}
I want to use the actual value of variable appName1 in bat of groovy section but variable value in not getting replace ..see the command output while executing above stage in pipeline :
Running on windows in C:\Jenkins\workspace\CI_Pipeline
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Billing
[Pipeline] echo
"Projects/Billing"
[Pipeline] bat
[CI_Pipeline] Running batch script
C:\Jenkins\workspace\CI_Pipeline>cd cognizant-intelligent-test-scripter-1.1-setup
C:\Jenkins\workspace\CI_Pipeline\cognizant-intelligent-test-scripter-1.1-setup>run.bat -run -project_location appName1 -scenario "Scenario1" -testcase "TC2" -browser "IE" -standalone_report
I was facing the same problem and it got resolved using triple double-quotes instead of single quotes. So, the query would look like:
bat """cd cognizant-intelligent-test-scripter-1.1-setup
run.bat -run -project_location ${appName1} -scenario "Scenario1" -testcase "TC2" -browser "IE" -standalone_report"""
Also, make sure to use the variables inside ${<var_name>}
Use triple double-quotes instead of single to define batch.
Triple-double-quoted strings behave like double-quoted strings, with the addition that they are multiline, like the triple-single-quoted strings. Neither double quotes nor single quotes need be escaped in triple-double-quoted strings.
bat """cd cognizant-intelligent-test-scripter-1.1-setup
run.bat -run -project_location appName1 -scenario "Scenario1" -testcase "TC2" -browser "IE" -standalone_report"""```
Is there a way to use the Jenkins "Execute system groovy script" step from a pipeline file which is SCM commited ?
If yes, how would I access the predefined variables (like build) in it ?
If no, would I be able to replicate the functionality otherwise, using for example the Shared Library Plugin ?
Thanks !
You can put groovy code in a pipeline in a (always-source-controlled) Jenkinsfile, like this:
pipeline {
agent { label 'docker' }
stages {
stage ('build') {
steps {
script {
// i used a script block because you can jam arbitrary groovy in here
// without being constrained by the declarative Jenkinsfile DSL
def awesomeVar = 'so_true'
print "look at this: ${awesomeVar}"
// accessing a predefined variable:
echo "currentBuild.number: ${currentBuild.number}"
}
}
}
}
}
Produces console log:
[Pipeline] echo
look at this: so_true
[Pipeline] echo
currentBuild.number: 84
Click on the "Pipeline Syntax" link in the left navigation of any of pipeline job to get a bunch of examples of things you can access in the "Global Variables Reference."