Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to launch few instance on AWS using terraform script, i am automating the whole process using jenkins

pipeline{
    agent any
    tools {
        terraform 'terraform'
    }
     stages{
        stage('Git Checkout'){
            steps{
                git branch: 'main', credentialsId: 'gitlab id', url: 'https://gitlab.com/ndey1/kafka-infra'
            }
        }
        
        stage('Terraform init'){
            steps{
                sh 'cd terraform-aws-ec2-with-vpc'
                sh 'terraform init'
            }
        }
        
        stage('Terraform plan'){
            steps{
                sh 'terraform plan'
            }
        }
        
        stage('Terraform apply'){
            steps{
                sh 'terraform apply --auto-approve'
            }
        }
    }
    }

but while running jenins jobs ( pipeline ) it throws the error

+ cd terraform-aws-ec2-with-vpc
[Pipeline] sh
+ terraform init
[0m[1mTerraform initialized in an empty directory![0m

The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.[0m
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Terraform plan)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
+ terraform plan
[31m?[0m[0m
[31m│[0m [0m[1m[31mError: [0m[0m[1mNo configuration files[0m
[31m│[0m [0m
[31m│[0m [0m[0mPlan requires configuration to be present. Planning without a configuration
[31m│[0m [0mwould mark everything for destruction, which is normally not what is
[31m│[0m [0mdesired. If you would like to destroy everything, run plan with the
[31m│[0m [0m-destroy option. Otherwise, create a Terraform configuration file (.tf
[31m│[0m [0mfile) and try again.
[31m?[0m[0m

though all terraform cod is in the same dir named "kafka-infra" but still its saying no configuration file in dir but terraform init runs successfully , the error comes in the stage of " terraform plan"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
243 views
Welcome To Ask or Share your Answers For Others

1 Answer

The answer was edited as per @NoamHelmer suggestions from the comments.


You can use the dir option and set it to the directory of the cloned repo, as by default Jenkins is using something called a workspace directory.


        stage('Terraform init'){
            steps{
                dir("terraform-aws-ec2-with-vpc") { // this was added
                    sh 'terraform init'
               }
            }
        }

The same line should be added to all the stages.

Or you could alternatively use multiline shell scripts:

            steps{
                sh '''
                   cd terraform-aws-ec2-with-vpc
                   terraform init
                '''
           }
            

As for the style of the configuration, there are probably multiple (better) ways of doing it. For example, you could use environment variables instead of hardcoding the directory you want to use to execute Terraform code etc.

[1] https://www.jenkins.io/doc/pipeline/tour/environment/

[2] https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#dir-change-current-directory


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...