Running packer via pipeline script

14 views
Skip to first unread message

zil...@gmail.com

unread,
Jan 13, 2021, 4:06:35 PM1/13/21
to Jenkins Users
I'm trying to create an AMI (for Jenkins master in this case), and I have the following Packer template, which as you see needs an SSH_PRIVATE_KEY.

{

  "variables" : {

    "aws_access_key" : "{{ env `AWS_ACCESS_KEY` }}",

    "aws_secret_key" : "{{ env `AWS_SECRET_KEY` }}",

    "ssh_private_key" : "{{ env `SSH_PRIVATE_KEY` }}",

    "aws_ami_us_east_1" : "{{ env `AWS_BASE_AMI` }}",

    "jenkins_master_image_version" : "{{ env `JENKINS_MASTER_IMAGE_VERSION` }}"

  },

  "builders" : [...],

  "provisioners" : [...]

}

I invoke packer in a pipeline, like this

    stage("Create Jenkins AMI") { 

      steps { 

        script { 

          withCredentials([sshUserPrivateKey(credentialsId: "ec2-user-ssh", keyFileVariable: "SSH_PRIVATE_KEY")]) { 

            dir("$env.WORKSPACE/deployment/jenkins/packer") { 

              sh """ 

                 export AWS_BASE_AMI=`aws ec2 describe-images \ 

                   --owners amazon \ 

                   --filters "Name=name,Values=amzn2-ami-hvm-2.0.*.?-x86_64-gp2" "Name=state,Values=available" \ 

                   --query "reverse(sort_by(Images, &CreationDate))[:1].ImageId" \ 

                   --output text` \ 

                   && export JENKINS_MASTER_IMAGE_VERSION=$env.JENKINS_VERSION \

                   && packer build jenkins-ami-master.json 

              """ 

            } 

          } 

        } 

      } 

    } 

Packer builds the temporary instance but fails to ssh to it, and hence can't run my provisioners. what am i missing?


zil...@gmail.com

unread,
Jan 18, 2021, 2:25:23 PM1/18/21
to Jenkins Users
It turns out my builder block was set up with the wrong security group. Once I set this up correctly, the above configuration worked.
Reply all
Reply to author
Forward
0 new messages