Jenkins - Source code analyser to validate a terraform file

71 Views Asked by At

I have an url that’s all about source code analyser api. I want that url to validate the terraform file and give the output.

The same url is used in postman form data and looks good. I am not sure how to achieve this in jenkins as I have to have a job for this in jenkins.

Kindly help. Thank You.

1

There are 1 best solutions below

1
On

If you want to send a .tf file as a POST request to a URL with data (which is what I'm understanding you did with postman and want to replicate in Jenkins), you need to use the httprequest plugin.

Here is an example of what your POST request might look like (assuming you are sending a file called file.tf:

httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_OCTETSTREAM,
            httpMode: 'POST', quiet: true,
            uploadFile: 'file.tf', 
            multipartName: 'file.tf', 
            wrapAsMultipart: false,
            url: 'https://<your_url>'

EDIT:

The above plugin is meant for use with declarative pipeline. You can have the .tf file anywhere, but you need to follow a guide to setting up the declarative pipeline properly. Once you have done that, you can have the above code block within a stage pointing at where you placed your .tf file:

stage('send-file') {
  steps {
    dir('/path/to/tf') {
      httpRequest acceptType: 'APPLICATION_JSON', contentType: 'APPLICATION_OCTETSTREAM,
            httpMode: 'POST', quiet: true,
            uploadFile: 'file.tf', 
            multipartName: 'file.tf', 
            wrapAsMultipart: false,
            url: 'https://<your_url>'
    }
  }
}

If you want to use a freestyle project instead, you will have to set it up and in a "Execute Bash/Batch job" segment, use curl:

curl -i -X POST 'https://<your_url>' \
  -H "Content-Type: text/xml" \
  --data-binary "@path/to/tf/file"