Home > Mobile >  Packer Unable to validate the hcl2 template in jenkins job
Packer Unable to validate the hcl2 template in jenkins job

Time:11-07

I am working to build the packer pipeline I had created the json template which are working fine but as per the packer recommendation, I am working to upgrade it to the hcl2 template. When i run the hcl2_upgrade command. I see a the json template is converted to the .pkr.hcl template but while running it. I have done some customization to template as per the recommded in packer documentation. While executing the pipeline through jenkins job , it gives me error.

template_pqr.json.pkr.hcl: Below is the starting line of my template over which it throws error

variable "ami_name" {
  type    = string
  default = "abc"
}

My jenkisn file is

stage('packer template validation') {
            steps{
                    sh label: 'Validate Template', script: "packer validate template_pqr.json.pkr.hcl"
           }
        }
        stage('AMI creation') {
            steps{
                    withAWS(credentials: 'credent') {
                        sh label: 'build template_pqr ', script: "packer build template_pqr.json.pkr.hcl"
                    }
            }
        }
       }

error:

packer validate template_pqr.json.pkr.hcl
Failed to parse template: Error parsing JSON: invalid character 'v' looking for beginning of value
At line 2, column 2 (offset 2):
    1: 
    2: v

CodePudding user response:

That error message is thrown when the validate command targets a HCL2 file in versions of Packer prior to version 1.6. That is the version of Packer which first introduced HCL2 support for the validate command (even though the build command was supported in 1.5). You need to update your Jenkins Pipeline agent to include an installed version of Packer that is at least 1.6.0. I would recommend the latest version of 1.6.x as 1.7 introduced plugin separation, which your templates and configs probably do not support.

version 1.6.0 release notes: features

  • Related