Home > Enterprise >  Terraform Jenkins Pipeline User input Failure
Terraform Jenkins Pipeline User input Failure

Time:12-28

I am facing a problem with Jenkins Terraform pipeline where terraform asks for a user input and I have no ability to provide the input and as a result the build fails. Here is my configuration:

Jenkinsfile:

pipeline {
    agent any
    
    stages {
        stage ('Terraform Image version') {
       agent{   
              docker {
                    //args 'arg1'     // optional
            label 'jenkins-mytask'
                    reuseNode true
                    alwaysPull true
                    registryUrl 'https://docker-xyz-virtual.artifactory.corp'
                    image 'docker-xyz-virtual.artifactory.corp/jenkins/slaves/terraform:0.12.15'
                }
       }
            steps {
                sh 'terraform --version'
        sh 'ls -ltr'
                sh 'terraform init -no-color'
        sh 'terraform apply  -no-color'
            }
        }
    }
}

Error output from Jenkins:

Do you want to migrate all workspaces to "local"?
  Both the existing "s3" backend and the newly configured "local" backend
  support workspaces. When migrating between backends, Terraform will copy
  all workspaces (with the same names). THIS WILL OVERWRITE any conflicting
  states in the destination.
  
  Terraform initialization doesn't currently migrate only select workspaces.
  If you want to migrate a select number of workspaces, you must manually
  pull and push those states.
  
  If you answer "yes", Terraform will migrate all states. If you answer
  "no", Terraform will abort.

  Enter a value: 

Error: Migration aborted by user.

I need to understand if it is possible to handle such user input event in Jenkins pipeline.

CodePudding user response:

Assuming that you do want to migrate your Terraform state, then you must update the flags in the sh step method to provide non-interactive inputs for these prompts:

sh 'terraform init -no-color -input=false -force-copy'

or if you do not want to migrate the state:

sh 'terraform init -no-color -input=false -reconfigure'

Heads up that the next sh step method also needs to be similarly modified:

sh 'terraform apply -no-color -input=false -auto-approve'

You also probably want to set the normal environment variable in the environment directive:

environment { TF_IN_AUTOMATION = true }
  • Related