0

I have a Jenkinsfile that calls a shell script:

        stage('Terraform Plan') {
            steps {
                    sh 'chmod +x ./jenkins_plan.sh'
                    sh 'bash jenkins_plan.sh'
            }
        }

The shell script (jenkins_plan.sh) has the following commands in it:

        cd "$dir"
        terraform init
        terraform validate 
        terraform plan -lock=false -input=false -out tfplan
        terraform show -no-color tfplan > tfplan.txt

I would like to send the output (tfplan.txt) from the shell script to the Jenkinsfile so I can do something similar to:

        stage('Terraform Plan') {
            steps {
                    sh 'chmod +x ./jenkins_plan.sh'
                    sh 'bash jenkins_plan.sh'
               script{
                    env.PLAN_OUTPUT = readFile('tfplan.txt')
                }
            }
        }

Any help would be greatly appreciated.

marcorivera8
  • 217
  • 1
  • 5
  • 13
  • Your TF CLI commands and JP code are both completely fine. Could you please elaborate on what the delta is between current and expected behavior? – Matthew Schuchard Aug 03 '22 at 17:33
  • I don't want to output everything using 'returnStdout', I would just like to get tfplan.txt and its contents from the shell script to the jenkinsfile. Currently with the way my code is setup, the tfplan.txt just returns null. – marcorivera8 Aug 04 '22 at 00:22
  • The `chmod +x jenkins_plan.sh` is superfluous if you always run the script with `bash jenkins_plan.sh` – tripleee Aug 04 '22 at 06:25
  • A much better fix is to take out the hard-coded redirection within the script and then just capture its output. – tripleee Aug 04 '22 at 06:26

0 Answers0