Skip to content
hashicorp

GitHub Action

Setup HashiCorp Packer

v3.1.0 Latest version

Setup HashiCorp Packer

hashicorp

Setup HashiCorp Packer

A GitHub Action to install a specific version of HashiCorp Packer and add it to PATH

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Setup HashiCorp Packer

uses: hashicorp/[email protected]

Learn more about this action in hashicorp/setup-packer

Choose a version

GitHub Action: setup-packer

The hashicorp/setup-packer Action sets up the Packer CLI in your GitHub Actions workflow by adding the packer binary to PATH.

GitHub Action: Self-Test

Table of Contents

Requirements

This GitHub Actions supports all commands that are available in the packer CLI.

The build command may require access to provider-specific credentials.

Other environment variables (such as PACKER_LOG) may be set as normal and will be picked up accordingly.

Usage

Create a GitHub Actions Workflow file (e.g.: .github/workflows/packer.yml):

name: packer

on:
  push:

env:
  PRODUCT_VERSION: "1.10.0" # or: "latest"

jobs:
  packer:
    runs-on: ubuntu-latest
    name: Run Packer
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup `packer`
        uses: hashicorp/setup-packer@main
        id: setup
        with:
          version: ${{ env.PRODUCT_VERSION }}

      - name: Run `packer init`
        id: init
        run: "packer init ./image.pkr.hcl"

      - name: Run `packer validate`
        id: validate
        run: "packer validate ./image.pkr.hcl"

In the above example, the following definitions have been set.

  • The event trigger has been set to push. For a complete list, see Events that trigger workflows.
  • The origin of this GitHub Action has been set as hashicorp/setup-packer@main. For newer versions, see the Releases.
  • The version of packer to set up has been set as 1.10.0. For a complete list, see releases.hashicorp.com.
  • The Packer manifest to interact with has been set as ./image.pkr.hcl.

These definitions may require updating to suit your deployment, such as specifying self-hosted runners.

Additionally, you may configure outputs to consume return values from the Action's operations.

Inputs

This section contains a list of all inputs that may be set for this Action.

  • version - The version of packer to install. Defaults to latest if unset.

Note

To retrieve the latest version, this GitHub Action polls the HashiCorp Releases API and finds the latest released version of Packer that isn't marked as a pre-release (is_prerelease).

Outputs

This section contains a list of all outputs that can be consumed from this Action.

  • version - The version of packer that was installed.

Integrating with HCP Packer

To integrate with HCP Packer, add your HCP Client ID and HCP Client secret as environment variables to the Packer build call.

We add an HCP_PACKER_BUILD_FINGERPRINT in this example that is based on the workflow run ID, that way it is always unique

We recommend storing these in GitHub Actions Secrets (as opposed to plain-text).

For more information on using HCP Packer, see the Getting Started tutorial.

name: hcp-packer

on:
  - push

jobs:
  hcp-packer:
    runs-on: ubuntu-latest
    name: Run Packer
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup `packer`
        uses: hashicorp/setup-packer@main
        id: setup
        with:
          version: "latest"

      - name: Run `packer init`
        id: init
        run: "packer init ./image.pkr.hcl"

      - name: Run `packer validate`
        id: validate
        run: "packer validate ./image.pkr.hcl"

      - name: Build Artifact
        run: packer build -color=false -on-error=abort ./image.pkr.hcl
        env:
          HCP_CLIENT_ID: ${{ secrets.HCP_CLIENT_ID }}
          HCP_CLIENT_SECRET: ${{ secrets.HCP_CLIENT_SECRET }}
          HCP_PACKER_BUILD_FINGERPRINT: "run.id.${{ github.run_id }}.run.attempt.${{ github.run_attempt }}"

Author Information

This GitHub Action is maintained by the contributors listed on GitHub.

The original code of this repository is based on work done by Matthew Sanabria as part of the setup-packer GitHub Action.

License

Licensed under the Apache License, Version 2.0 (the "License").

You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" basis, without WARRANTIES or conditions of any kind, either express or implied.

See the License for the specific language governing permissions and limitations under the License.