Command not found in jenkins pipeline grrovy - jenkins

I'm getting a "command not found" error while executing the below jenkns code. Could someone please help me to solve this?
def myVariable = "foo"
pipeline {
agent none
stages {
stage ('npm publish on web package') {
agent {label 'master'}
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
script{
def pom = readMavenPom file: 'Project1/pom.xml'
sh "echo ${pom.version}"
sh 'ssh -tt admin#20.100.300.28 cd /opt/admin/projects/as/ && echo "${myVariable}" >/opt/automation/projects/automation_suite/.version'
}
}
}
}
}
}
Log is attached below. This is the part of the log which is related to the jenkins error.
Commit message: "fff"
[Pipeline] withEnv
[Pipeline] {
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] catchError
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ 1.1.39
/opt/software/jenkins/workspace/automation_suite/Test_Pipeline_2#tmp/durable-14b3d621/script.sh: line 1: 1.1.39: command not found
[Pipeline] }
[Pipeline] // script
[Pipeline] }
ERROR: script returned exit code 127
[Pipeline] // catchError
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage

Double-check your pipeline code. The error you see can be caused by
sh "${pom.version}"
The example you posted uses
sh "echo ${pom.version}"
but if this was the case, you would see something like this in the console log:
[Pipeline] sh
+ echo 1.1.39
1.1.39
[Pipeline] }
Your console log proves that it is not the case and you pass ${pom.version} to the sh command somewhere clearly.
[Pipeline] sh
+ 1.1.39
/opt/software/jenkins/workspace/automation_suite/Test_Pipeline_2#tmp/durable-14b3d621/script.sh: line 1: 1.1.39: command not found

Related

Failed to assign sh output to a variable when sh output contains opening bracket ( | Jenkins Declarative Pipeline

I am trying in one of the steps to write some data in file, and in step after that to read that data using and assign it to a variable. This is my declarative Jenkins pipeline:
pipeline {
agent {label 'build-slave-aws'}
stages {
stage('Notify about start') {
steps {
sh 'echo "Some fatct with brackets ()" > /tmp/facts.issues'
}
}
stage('Gather the facts') {
steps {
script {
factsIssues = sh( script: "cat /tmp/facts.issues", returnStdout: true )
}
sh "echo these are facts: ${factsIssues}"
}
}
}
}
The output of this run is following:
Started by user 123
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on i-000df827977fd5175 in /workspace/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Notify about start)
[Pipeline] sh
+ echo 'Some fatct with brackets ()'
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Gather the facts)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ cat /tmp/facts.issues
[Pipeline] }
[Pipeline] // script
[Pipeline] sh
/workspace/workspace/test#tmp/durable-2a1f8cdf/script.sh: line 1: syntax error near unexpected token `('
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 2
Finished: FAILURE
Any other text that doesn't contain ( simply work just fine. Do you have any suggestion on how I can write some data in file with ( and reading that back to a variable?
You are missing quotes around the argument of the echo command within the last sh script.
Fix:
sh "echo 'these are facts: ${factsIssues}'"

Getting error "Stage <repoName> skipped due to earlier failure(s)"

Description:In production when was running a jenkins pipeline with set of few jobs it was failing at 2nd stage with above error.
The task of this job is to create a github repository.
Kindly assist.
Resolution:
At the stage where job was failing it was prompting a form or run time prompt to enter the data which was not filled by the user so it was failing with this error.
Upon executing jobs at console it produces something like:
> git checkout -f 4dfa1dc42ea3727a972a81aead8129ab65564208
> git rev-list --no-walk 4dfa1dc42ea3727a972a81aead8129ab65564208 # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] withEnv [Pipeline] { [Pipeline] stage [Pipeline] { (Collecting Infrastructure details) [Pipeline] container [Pipeline] { [Pipeline] script [Pipeline] { [Pipeline] sh
+ git --version git version 2.1.4 [Pipeline] input Input requested Approved by Pankaj Gupta (CompuCom) [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // container [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Collecting ServiceNow details) [Pipeline] container [Pipeline] { [Pipeline] script [Pipeline] { [Pipeline] input Input requested [Pipeline] } [Pipeline] // script [Pipeline] } [Pipeline] // container [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Display Setup) Stage "Display Setup" skipped due to earlier failure(s) [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Validate Inputs) Stage "Validate Inputs" skipped due to earlier failure(s) [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Create Repo And Add Collaborators) Stage "Create Repo And Add Collaborators" skipped due to earlier failure(s) [Pipeline] }
So if you look at the output then there are two time a URL is called which is "Input requested", when click this hyperlink it gives a new form to fill the request values, if that will not be filled it will give error.

Jenkins Pipeline: abort an input in a stage cannot trigger the aborted post action of that stage

My purpose is simple, just want to do some post actions in a stage when user click 'Abort' button in the input step of that stage. I've read some docs from jenkins.io and found there seems an implicit way for doing this by using the post directive. So I make some simple test below:
First is this:
pipeline {
agent any
stages {
stage('test') {
input {
message 'Proceed?'
ok 'yes'
submitter 'admin'
}
steps {
echo "helloworld"
}
post {
aborted {
echo "stage test has been aborted"
}
}
}
}
post {
aborted {
echo "pipeline has been aborted"
}
}
}
If I click Abort button, the log output will only show:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] input
Proceed?
yes or Abort
Aborted by admin
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
pipeline has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Rejected by SYSTEM
Finished: ABORTED
which means Input Abort only trigger the post action part of the pipeline but not the part within that stage.
Then I try another one:
pipeline {
agent any
stages {
stage('test') {
steps {
sh "sleep 15"
}
post {
aborted {
echo "stage test has been aborted"
}
}
}
}
post {
aborted {
echo "pipeline has been aborted"
}
}
}
I abort this job within 15 seconds, and output will show
[Pipeline] { (hide)
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] sh
+ sleep 15
Sending interrupt signal to process
Aborted by admin
Terminated
script returned exit code 143
Post stage
[Pipeline] echo
stage test has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
pipeline has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED
Which means the post aborted action part in a stage could be triggered.
How can I do some post actions when abort an input step within a stage, not the whole pipeline?
I think I figure out what's the difference between the above two examples and how to solve this problem myself :).
It seems like the Post actions in a stage will only be triggered when the related Steps section's currentResult is matched but not the Input section above. So I make a little change and it works. That is make the Input section a script inside the Steps section.
pipeline {
agent any
stages {
stage('test') {
steps {
script {
input message: 'Proceed?', ok: 'Yes', submitter: 'admin'
}
echo "helloworld"
}
post {
aborted{
echo "test stage has been aborted"
}
}
}
}
post {
aborted {
echo "pipeline has been aborted"
}
}
}
When click Abort button, the output log is:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Proceed?
Yes or Abort
[Pipeline] }
[Pipeline] // script
Post stage
[Pipeline] echo
test stage has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
pipeline has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Rejected by admin
Finished: ABORTED
which satisfys me:). I can compose a complex pipeline with user confirmation and auto rolling back now. Hope it also help you.

Aborted jenkins pipeline job continues running later stages

I'm on the latest version of Jenkins and the pipeline plugins, and I have a jenkins declarative pipeline job configured as follows:
pipeline {
agent {
label 'default'
}
stages {
stage('Prepare') {
steps {
dir('foo') {
git ...
}
}
}
stage('Temp1') {
steps {
sh script: 'dotnet run ...'
echo 'Temp1'
}
}
stage('Temp2') {
steps {
echo 'Temp2'
}
}
}
}
If I abort the build during the sh script in the Temp1 stage, my expectation is that neither the rest of the Temp1 stage nor the Temp2 stage would run. However, when I abort it, it stops the sh script, then runs the rest of Temp1 stage and continues on to run the Temp2 stage as well!
Here is the log:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on ...
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] dir
Running in ...
[Pipeline] {
[Pipeline] git
Fetching changes from the remote Git repository
...
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Temp1)
[Pipeline] sh
+ dotnet run ...
Aborted by ...
Sending interrupt signal to process
[Pipeline] echo
Temp1
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Temp2)
[Pipeline] echo
Temp2
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED
How do I get the build to actually abort?
This ended up being a dotnet issue, where it returns 0 as the exit code when killed by SIGTERM. https://github.com/dotnet/coreclr/issues/21243 was resolved several days ago but has not been released yet.
As a workaround, you can do something like this:
public static int Main(string[] args)
{
Environment.ExitCode = 1;
// Do stuff
return 0;
}
If Main completes successfully, it will return an exit code of 0. If it receives a SIGTERM, it will return the value in Environment.ExitCode.

Getting Jenkinsfile error - command not found

What is wrong with this Jenkins file? I am new to it but I don't get what am I doing wrong
pipeline {
agent any
stages {
stage('Test') {
steps {
dir ('/var/lib/jenkins/workspace/pipleline_2') {
}
}
}
}
post {
always {
sh 'hello2.sh'
}
failure {
mail(from: "heenashree2010#gmail.com",
to: "qshoretechnologies#gmail.com",
subject: "That build passed.",
body: "Nothing to see here")
}
}
}
I am getting below error. hello2.sh exists in the directory which I have specified but I am not able to execute it. I also tried sh('hello2.sh') but it didn't work for me. What am I doing wrong?
Started by user qshore
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipleline_2
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] dir
Running in /var/lib/jenkins/workspace/pipleline_2
[Pipeline] {
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] sh
[pipleline_2] Running shell script
+ hello2.sh
/var/lib/jenkins/workspace/pipleline_2#tmp/durable-dbcba8b2/script.sh: line 2: hello2.sh: command not found
[Pipeline] mail
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE
The script called hello2.sh is not found. Make sure that it is included in the repo that you're checking out.

Resources