0

I wrote a declarative Jenkins pipeline and would like to track the CLI commandos executed by Jenkins. To do this, I added a stage and the step sh 'history -a' in it:

pipeline {
    options {
        ...
    }
    agent {
        node {
            ...
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'hostname'
                sh 'pwd'
                ...
            }
        }
        ...
        stage('History') {
            steps {
                sh 'history -a'
            }
        }
    }
    post {
        ...
    }
}

But that is not working:

Console Output
...
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Tear Down)
[Pipeline] sh
+ history -a
/path/to/project-root@tmp/durable-66ba15cc/script.sh: 1: history: not found
[Pipeline] }
...

Other Linux commands like hostname, ls, or pwd are working fine.

Why does history run into an error? How to store the shell commands called by Jenkins in the context of a pipeline?

automatix
  • 14,018
  • 26
  • 105
  • 230
  • Why? Because each `sh` steps creates its own shell session which is discarded when the function call returns. How? [Maybe this](https://stackoverflow.com/questions/2853803/how-to-echo-shell-commands-as-they-are-executed) and then filter the Jenkins log for messages starting with "+ ". – zett42 Apr 28 '20 at 11:37

1 Answers1

0

That specific error that you are getting I think it is only because the agent where you are running sh, does not have the history cmd available - history: not found

If you can store the sh commands... If you want only the sh commands, I think you need to write to a file that you create at the beginning, where you write to everytime you have a sh step, or you can just use the pipeline log file (the output console).

You can find here a thread about the location of the pipeline or build logs, in case it helps.

nandilov
  • 699
  • 8
  • 18