how can I save a command in a variable and executed anywhere in the stage
tried differnt way, but still success
here is my example
pipeline {
agent any
environment {
myscript = sh '''
echo "hello"
echo "hello"
echo "hello"
'''
}
stages {
stage("RUN") {
steps {
sh "${myscript}"
}
}
}
}
you can do it like this. Not with a groovy variable but can be more dynamic with groovy function/method
def reusableScript(message) {
sh """
echo Hello World
echo Hi ${message}
"""
}
pipeline {
agent any;
stages {
stage('01') {
steps {
script {
reusableScript("From ${env.STAGE_NAME}")
}
}
}
stage('02') {
steps {
script {
reusableScript("From ${env.STAGE_NAME}")
}
}
}
}
}
Related
I am trying to setup a jenkins pipeline script that sends out an email when there is a job that have been running for more than 24 hours.
// Long running jobs
pipeline {
agent any
environment {
EMAIL_ALERT_TO = "address"
EMAIL_ALERT_CC = "address"
}
stages {
stage('def methods') {
steps {
script {
Jenkins.instance.getAllItems(Job).each(){ job -> job.isBuildable()
if (job.isBuilding()){
def myBuild= job.getLastBuild()
def runningSince= groovy.time.TimeCategory.minus( new Date(), myBuild.getTime() )
echo "myBuild = ${myBuild}"
echo "runningSince = ${runningSince}"
env.myBuild = myBuild
env.runningSince = runningSince
}
}
}
}
}
}
post {
// Email out the results
always {
script {
if (runningSince.hours >= 1){
mail to: "${env.EMAIL_ALERT_CC}",
cc: "${env.EMAIL_ALERT_CC}",
subject: "Long Running Jobs",
body: "Build: ${myBuild} ---- Has Been Running for ${runningSince.hours} hours:${runningSince.minutes} minutes"
}
}
}
}
}
I am seeing RejectedAccessException which appears to be related to arrays/list.
This is what I believe you are looking for
https://issues.jenkins-ci.org/browse/JENKINS-54952?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel
I a variable in my Jenkinsfile that contains a list of URLs and i would like a be able to run over them. When i pass the variable $URL to the function, I get an error:
No such property: $URL for class: groovy.lang.Binding
However, I'm able to echo this variable with sh.
pipeline {
agent any
environment {
URL="https://www.aaa.com," \
+ "https://www.bbb.com," \
+ "https://www.ccc.com"
}
stages {
stage ('A') {
//...
}
stage ('B') {
//...
}
stage ('C') {
steps {
script {
sh 'echo $URL'
funcion($URL)
}
}
}
}
}
def funcion(URL) {
sh "echo Going to echo a list"
for (int i = 0; i < URL.size(); i++) {
sh "echo ${URL[i]}"
}
}
What could be the issue?
You should pass this variable to the funcion() method as
function(URL)
instead of
funcion($URL)
The dollar sign $ is used only inside the GString when you want to interpolate a variable. For instance
#!groovy
def name = "Joe"
println "My name is $name"
results in
My name is Joe
You can read more about string interpolation in the Groovy documentation - http://groovy-lang.org/syntax.html#_string_interpolation
I need to fail one Jenkins pipeline stage when one file contains 'errors'
I do not know how to return an error from bash to Jenkins
stage('check if file continas error and exit if true') {
steps {
sh "grep 'error' filetocheck.txt"
}
}
}
reference Is it possible to capture the stdout from the sh DSL command in the pipeline
This worked for me,
def runShell(String command){
def responseCode = sh returnStatus: true, script: "${command} &> tmp.txt"
def output = readFile(file: "tmp.txt")
return (output != "")
}
pipeline {
agent any
stages {
stage('check shellcheck') {
steps {
script {
if (runShell('grep \'error\' file_to_parse.txt')) {
sh "exit 1"
}
}
}
}
}
}
you can try using String.count(charSequence) where String could be a file or string.
def file = 'path/to/file.txt'
if ( file.count('error') > 0 )
return stageResultMap.didB2Succeed = false
I am trying to write a stages inside another stage based on if condition. I am not able to come up with a solution. Can anyone guide on this
stages {
stage('Example') {
steps {
script {
if(!(fileExists("c:/test.txt")))
{
echo "Inside if"
stage('1') {
echo "stage1"
}
stage('2') {
echo "stage2"
}
}
else
{
stage('else stage') {
echo "else stage1"
}
}
}
}
}
}
This worked for me.
when {
expression
{
return !(fileExists("c:/test.txt"))
}
}
I have a Jenkinsfile as below and
i am passing environment variables per stage,
want to create a docker.env file using these environment variables,
so i can use this docker.env file as part of my docker run 'docker run -d --env-file=docker.env java' per stage
looking for a <command_to_copy_all_values_under_'environment'_to_file_'docker.env'> ???
Jenkinsfile:
pipeline {
agent any
stages {
stage('Staging') {
environment {
KEY1_1=VALUE1_1
KEY1_2=VALUE1_2
KEY2_1=VALUE2_1
KEY2_2=VALUE2_2
}
steps {
timestamps() {
deleteDir()
sh '''
<command_to_copy_all_values_under_above_'environment'_to_file_'docker.env'>
docker run -d --env-file=docker.env java
'''
}
}
}
stage('Production') {
when {
branch 'release'
}
environment {
KEY1_1=VALUE1_1
KEY1_2=VALUE1_2
KEY2_1=VALUE2_1
KEY2_2=VALUE2_2
}
steps {
timestamps() {
timeout(time: 30, unit: 'MINUTES') {
input 'Ready to deploy to PRODUCTION, click PROCEED or ABORT ?'
}
deleteDir()
sh '''
<command_to_copy_all_values_under_above_'environment'_to_file_'docker.env'>
docker run -d --env-file=docker.env java
'''
}
}
}
}
tools {
maven 'apache_maven_352'
}
environment {
KEY1=VALUE1
KEY2=VALUE2
KEY3=VALUE3
KEY4=VALUE4
}
}
Here's solution with AWK:
awk -v B1="block-2" -v B2="block-Y" '/^[^ \t]/ { main=$1; next } main==B1 && $1==B2 { fnd=1; next } $1=="}" { fnd=0 } fnd && $0~/=/ { gsub(/^[ \t]+/,""); print $0 }' YOURFILE
Output:
key2_1=value2_1
key2_2=value2_2