hashicorp / setup-packer

Run HashiCorp Packer as part of your GitHub Actions Workflow
https://github.com/marketplace/actions/setup-hashicorp-packer
Apache License 2.0
139 stars 83 forks source link

Doesn't seem to work with map variables? #37

Closed toast-gear closed 1 year ago

toast-gear commented 2 years ago

Expected Behavior

To be able to provide tags as variables in a packer build

Current Behavior

With a step like below the pipeline errors with a usage error i.e. Usage: packer build [options] TEMPLATE

      # Had issues with multiline for arguments
      - name: Build and Publish
        uses: hashicorp/packer-github-actions@master
        with:
          command: build
          arguments: |
            -on-error=abort
            -var security_group_id=sg-111111111111
            -var region=eu-west-1
            -var subnet_id=subnet-1111111111
            -var instance_type=t3.medium
            -var root_volume_size_gb=50
            -var runner_version=2.286.1
            -var tags="{ sandbox=${{ github.event.inputs.sandbox }} }"
          target: images/linux-amzn2/github_agent.linux.pkr.hcl
        env:
          PACKER_LOG: 1

I've tried various variations e.g.:

            -var tags="{ sandbox=${{ github.event.inputs.sandbox }} }"
            -var tags="{ sandbox=\"${{ github.event.inputs.sandbox }}\" }"
            -var tags="{ sandbox=true }"

I've also tried providing my arguments all on a single line and I get the same error.

If I remove the tag variable it works:

      # Had issues with multiline for arguments
      - name: Build and Publish
        uses: hashicorp/packer-github-actions@master
        with:
          command: build
          arguments: |
            -on-error=abort
            -var security_group_id=sg-111111111111
            -var region=eu-west-1
            -var subnet_id=subnet-1111111111
            -var instance_type=t3.medium
            -var root_volume_size_gb=50
            -var runner_version=2.286.1

Steps to Reproduce

  1. Create a packer template that takes in a variable with the tags structure shown abve
  2. Attempt to build said template providing the variable with the tags structure
vesubramanian commented 1 year ago

I am also facing the same issue. Can you please let me know what is the solution for this? I am not sure why it is closed without a solution.

toast-gear commented 1 year ago

It seems this repository has changed a bit since I used it and raised this issue (and appears to have been renamed). It was called hashicorp/packer-github-actions at the time which appears to be legacy, hashicorp/packer-github-actions worked as an abstraction to the packer binary. It now appears the repository instead is solely concerend with setting up the packer binary for you to then use the binary directly instead of via a nice declarative interface. As far as a solution, I believe I just did the tagging separately as an additional step with the AWS CLI so I didn't need a map to begin with.

If you take a look at the action manifest you can see the action is fundamentally different to when this issue was raised https://github.com/hashicorp/setup-packer/blob/main/action.yml. This issue is no longer relevant as the use case of this action has changed. I doubt they are planning long term to maintain the 2 branches, the master branch can now be considered legacy and so bugs present won't be fixed.

vesubramanian commented 1 year ago

Ok. Thank you for the quick response. However in my case, I want to merge some tags from SSM Parameter store of AWS along with some hardcoded tags while calling Packer Build from GitHub Actions. I have tried many things, but nothing seems to be working. It will be great, if you can post an example, if you know.

toast-gear commented 1 year ago

Either way it's a case of doing the tagging outside of the action with the aws cli. Going to close the issue as the repository now works fundamentally differently so this issue is no longer valid.