$npx -y skills add hashicorp/agent-skills --skill push-to-registryPush Packer build metadata to HCP Packer registry for tracking and managing image lifecycle. Use when integrating Packer builds with HCP Packer for version control and governance.
| 1 | # Push to HCP Packer Registry |
| 2 | |
| 3 | Configure Packer templates to push build metadata to HCP Packer registry. |
| 4 | |
| 5 | **Reference:** [HCP Packer Registry](https://developer.hashicorp.com/hcp/docs/packer) |
| 6 | |
| 7 | > **Note:** HCP Packer is free for basic use. Builds push metadata only (not actual images), adding minimal overhead (<1 minute). |
| 8 | |
| 9 | ## Basic Registry Configuration |
| 10 | |
| 11 | ```hcl |
| 12 | packer { |
| 13 | required_version = ">= 1.7.7" |
| 14 | } |
| 15 | |
| 16 | variable "image_name" { |
| 17 | type = string |
| 18 | default = "web-server" |
| 19 | } |
| 20 | |
| 21 | locals { |
| 22 | timestamp = regex_replace(timestamp(), "[- TZ:]", "") |
| 23 | } |
| 24 | |
| 25 | source "amazon-ebs" "ubuntu" { |
| 26 | region = "us-west-2" |
| 27 | instance_type = "t3.micro" |
| 28 | |
| 29 | source_ami_filter { |
| 30 | filters = { |
| 31 | name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*" |
| 32 | } |
| 33 | most_recent = true |
| 34 | owners = ["099720109477"] |
| 35 | } |
| 36 | |
| 37 | ssh_username = "ubuntu" |
| 38 | ami_name = "${var.image_name}-${local.timestamp}" |
| 39 | } |
| 40 | |
| 41 | build { |
| 42 | sources = ["source.amazon-ebs.ubuntu"] |
| 43 | |
| 44 | hcp_packer_registry { |
| 45 | bucket_name = var.image_name |
| 46 | description = "Ubuntu 22.04 base image for web servers" |
| 47 | |
| 48 | bucket_labels = { |
| 49 | "os" = "ubuntu" |
| 50 | "team" = "platform" |
| 51 | } |
| 52 | |
| 53 | build_labels = { |
| 54 | "build-time" = local.timestamp |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | provisioner "shell" { |
| 59 | inline = [ |
| 60 | "sudo apt-get update", |
| 61 | "sudo apt-get upgrade -y", |
| 62 | ] |
| 63 | } |
| 64 | } |
| 65 | ``` |
| 66 | |
| 67 | ## Authentication |
| 68 | |
| 69 | Set environment variables before building: |
| 70 | |
| 71 | ```bash |
| 72 | export HCP_CLIENT_ID="your-service-principal-client-id" |
| 73 | export HCP_CLIENT_SECRET="your-service-principal-secret" |
| 74 | export HCP_ORGANIZATION_ID="your-org-id" |
| 75 | export HCP_PROJECT_ID="your-project-id" |
| 76 | |
| 77 | packer build . |
| 78 | ``` |
| 79 | |
| 80 | ### Create HCP Service Principal |
| 81 | |
| 82 | 1. Navigate to HCP → Access Control (IAM) |
| 83 | 2. Create Service Principal |
| 84 | 3. Grant "Contributor" role on project |
| 85 | 4. Generate client secret |
| 86 | 5. Save client ID and secret |
| 87 | |
| 88 | ## Registry Configuration Options |
| 89 | |
| 90 | ### bucket_name (required) |
| 91 | The image identifier. Must stay consistent across builds! |
| 92 | |
| 93 | ```hcl |
| 94 | bucket_name = "web-server" # Keep this constant |
| 95 | ``` |
| 96 | |
| 97 | ### bucket_labels (optional) |
| 98 | Metadata at bucket level. Updates with each build. |
| 99 | |
| 100 | ```hcl |
| 101 | bucket_labels = { |
| 102 | "os" = "ubuntu" |
| 103 | "team" = "platform" |
| 104 | "component" = "web" |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | ### build_labels (optional) |
| 109 | Metadata for each iteration. Immutable after build completes. |
| 110 | |
| 111 | ```hcl |
| 112 | build_labels = { |
| 113 | "build-time" = local.timestamp |
| 114 | "git-commit" = var.git_commit |
| 115 | } |
| 116 | ``` |
| 117 | |
| 118 | ## CI/CD Integration |
| 119 | |
| 120 | ### GitHub Actions |
| 121 | |
| 122 | ```yaml |
| 123 | name: Build and Push to HCP Packer |
| 124 | |
| 125 | on: |
| 126 | push: |
| 127 | branches: [main] |
| 128 | |
| 129 | env: |
| 130 | HCP_CLIENT_ID: ${{ secrets.HCP_CLIENT_ID }} |
| 131 | HCP_CLIENT_SECRET: ${{ secrets.HCP_CLIENT_SECRET }} |
| 132 | HCP_ORGANIZATION_ID: ${{ secrets.HCP_ORGANIZATION_ID }} |
| 133 | HCP_PROJECT_ID: ${{ secrets.HCP_PROJECT_ID }} |
| 134 | |
| 135 | jobs: |
| 136 | build: |
| 137 | runs-on: ubuntu-latest |
| 138 | steps: |
| 139 | - uses: actions/checkout@v4 |
| 140 | - uses: hashicorp/setup-packer@main |
| 141 | |
| 142 | - name: Build and push |
| 143 | run: | |
| 144 | packer init . |
| 145 | packer build \ |
| 146 | -var "git_commit=${{ github.sha }}" \ |
| 147 | . |
| 148 | ``` |
| 149 | |
| 150 | ## Querying in Terraform |
| 151 | |
| 152 | ```hcl |
| 153 | data "hcp_packer_artifact" "ubuntu" { |
| 154 | bucket_name = "web-server" |
| 155 | channel_name = "production" |
| 156 | platform = "aws" |
| 157 | region = "us-west-2" |
| 158 | } |
| 159 | |
| 160 | resource "aws_instance" "web" { |
| 161 | ami = data.hcp_packer_artifact.ubuntu.external_identifier |
| 162 | instance_type = "t3.micro" |
| 163 | |
| 164 | tags = { |
| 165 | PackerBucket = data.hcp_packer_artifact.ubuntu.bucket_name |
| 166 | } |
| 167 | } |
| 168 | ``` |
| 169 | |
| 170 | ## Common Issues |
| 171 | |
| 172 | **Authentication Failed** |
| 173 | - Verify HCP_CLIENT_ID and HCP_CLIENT_SECRET |
| 174 | - Ensure service principal has Contributor role |
| 175 | - Check organization and project IDs |
| 176 | |
| 177 | **Bucket Name Mismatch** |
| 178 | - Keep `bucket_name` consistent across builds |
| 179 | - Don't include timestamps in bucket_name |
| 180 | - Creates new bucket if name changes |
| 181 | |
| 182 | **Build Fails** |
| 183 | - Packer fails immediately if can't push metadata |
| 184 | - Prevents drift between artifacts and registry |
| 185 | - Check network connectivity to HCP API |
| 186 | |
| 187 | ## Best Practices |
| 188 | |
| 189 | - **Consistent bucket names** - Never change for same image type |
| 190 | - **Meaningful labels** - Use for versions, teams, compliance |
| 191 | - **CI/CD automation** - Automate builds and registry pushes |
| 192 | - **Immutable build labels** - Put changing data (git SHA, date) in build_labels |
| 193 | |
| 194 | ## References |
| 195 | |
| 196 | - [HCP Packer Documentation](https://developer.hashicorp.com/hcp/docs/packer) |
| 197 | - [hcp_packer_registry Block](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/blocks/build/hcp_packer_registry) |
| 198 | - [HCP Terraform Provider](https://registry.terraform.io/providers/hashicorp/hcp/latest/docs/data-sources/packer_artifact) |