Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 36 ยท Post-processors and Pipelines

Checksum, Compress, shell-local and Artifice

Use the checksum, compress, shell-local and artifice post-processors to publish file images safely, verify downloads, run custom steps and replace artifacts in a chain.

Besides manifest, a handful of general-purpose post-processors cover most file-based publishing needs. This lesson explains four of them: checksum and compress for disk images you distribute, shell-local for custom steps and artifice for the special case where the artifact Packer holds is not the one you want to ship.

Packer is not available on this site. HCL blocks are read-only; the runnable examples are JavaScript stand-ins for the logic (naming, ordering, templating).

checksum

The checksum post-processor computes one or more hashes for each file in the artifact and writes them to files.

HCL
post-processor "checksum" {
  checksum_types = ["sha256", "sha512"]
  output         = "dist/{{.BuildName}}_{{.ChecksumType}}.checksum"
}

Supported types include md5, sha1, sha224, sha256, sha384 and sha512; prefer SHA-256 or stronger. The output path may use {{.BuildName}} and {{.ChecksumType}}. Publish the checksum next to the image so consumers can verify the download. Remember it only proves integrity, not authenticity: sign the checksum file (GPG, cosign) if consumers need to trust the publisher.

compress

HCL
post-processor "compress" {
  output            = "dist/{{.BuildName}}-{{timestamp}}.tar.gz"
  format            = "tar.gz"
  compression_level = 9
}

Formats are inferred from the output extension (.zip, .tar.gz, .tar.bz2, .tar.xz, .tar.lz4, .gz). Disk images with large zeroed regions compress very well, which is why you zero free space before the export. Compression consumes the input artifact unless keep_input_artifact = true.

shell-local

shell-local runs commands on the machine running Packer (the CI runner or your laptop), not inside the image. It is the escape hatch for anything without a dedicated plugin: signing, uploading with a CLI, posting a message.

HCL
post-processor "shell-local" {
  environment_vars = ["VERSION=${var.version}"]
  inline = [
    "cosign sign-blob --yes dist/web-base.tar.gz > dist/web-base.sig",
    "aws s3 cp dist/ s3://my-images/web-base/$VERSION/ --recursive"
  ]
}

Tips: set execute_command or use_linux_pty only if you need them; remember that on Windows runners the default shell differs; make commands fail loudly (set -euo pipefail in a script) so a broken upload does not look like success; never print secrets.

Also useful: only_on = ["linux"] skips the step on other host operating systems.

artifice

By default a post-processor operates on the artifact the builder produced. Sometimes the interesting thing is something else, for example a set of files your provisioners downloaded back from the VM, or a different cloud id. artifice replaces the artifact with a list of files you name.

HCL
provisioner "file" {
  direction   = "download"
  source      = "/tmp/app-bundle.tar"
  destination = "build/app-bundle.tar"
}

post-processors {
  post-processor "artifice" {
    files = ["build/app-bundle.tar"]
  }
  post-processor "checksum" {
    checksum_types = ["sha256"]
  }
}

Here the VM only existed to produce a tarball. artifice makes Packer treat that file as the artifact, and the next steps work on it. This is common for "build inside a clean VM" jobs. Note that artifice does not delete the original machine image for you; set the builder's cleanup options or use -on-error accordingly.

Choosing the order

A sensible chain for distributing a file image:

flowchart LR
  A["Disk image"] --> C["compress"] --> H["checksum"] --> S["shell-local: sign + upload"] --> M["manifest"]

Compress first so the checksum covers the final file; record in the manifest last.

Checksum after compress, because the hash must describe the exact bytes people download. This is one of the most common ordering mistakes: hashing the raw disk and publishing the compressed archive.

Try it: template placeholders

JS Expand output templates (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: catch ordering mistakes

JS Post-processor order linter (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where does the shell-local post-processor run its commands?

    shell-local is local to the Packer host, like a CI runner.
  2. 2. Why should checksum come after compress?

    A hash of the raw image does not match the compressed archive people download.
  3. 3. What does artifice do?

    It lets you ship files created during the build instead of the machine image.
  4. 4. A checksum proves what?

    Use signatures when you also need authenticity.

Summary

  • checksum hashes files; compress packs them; both change what consumers download, so order them with care.
  • shell-local runs on the Packer host and is the escape hatch for signing and uploading.
  • artifice swaps the artifact for files you collected during the build.
  • Finish chains with manifest; sign checksum files when authenticity matters.

Next: Tagging and pushing container images in a pipeline