push-to-registry

द्वारा hashicorp

Packer बिल्ड मेटाडेटा को HCP Packer रजिस्ट्री में पुश करें, ताकि इमेज लाइफसाइकिल ट्रैकिंग और गवर्नेंस हो सके। बिल्ड आर्टिफैक्ट्स को न्यूनतम ओवरहेड के साथ HCP Packer में रजिस्टर करता है, केवल मेटाडेटा (वास्तविक इमेज नहीं) संग्रहीत करता है और बिल्ड समय में एक मिनट से कम जोड़ता है। बकेट-स्तरीय लेबल (प्रति बिल्ड अपडेट) और अपरिवर्तनीय बिल्ड-स्तरीय लेबल (git SHA, टाइमस्टैम्प) को वर्जन कं

npx skills add https://github.com/hashicorp/agent-skills --skill push-to-registry

Push to HCP Packer Registry

Configure Packer templates to push build metadata to HCP Packer registry.

Reference: HCP Packer Registry

Note: HCP Packer is free for basic use. Builds push metadata only (not actual images), adding minimal overhead (<1 minute).

Basic Registry Configuration

packer {
  required_version = ">= 1.7.7"
}

variable "image_name" {
  type    = string
  default = "web-server"
}

locals {
  timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}

source "amazon-ebs" "ubuntu" {
  region        = "us-west-2"
  instance_type = "t3.micro"

  source_ami_filter {
    filters = {
      name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
    }
    most_recent = true
    owners      = ["099720109477"]
  }

  ssh_username = "ubuntu"
  ami_name     = "${var.image_name}-${local.timestamp}"
}

build {
  sources = ["source.amazon-ebs.ubuntu"]

  hcp_packer_registry {
    bucket_name = var.image_name
    description = "Ubuntu 22.04 base image for web servers"

    bucket_labels = {
      "os"   = "ubuntu"
      "team" = "platform"
    }

    build_labels = {
      "build-time" = local.timestamp
    }
  }

  provisioner "shell" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get upgrade -y",
    ]
  }
}

Authentication

Set environment variables before building:

export HCP_CLIENT_ID="your-service-principal-client-id"
export HCP_CLIENT_SECRET="your-service-principal-secret"
export HCP_ORGANIZATION_ID="your-org-id"
export HCP_PROJECT_ID="your-project-id"

packer build .

Create HCP Service Principal

  1. Navigate to HCP → Access Control (IAM)
  2. Create Service Principal
  3. Grant "Contributor" role on project
  4. Generate client secret
  5. Save client ID and secret

Registry Configuration Options

bucket_name (required)

The image identifier. Must stay consistent across builds!

bucket_name = "web-server"  # Keep this constant

bucket_labels (optional)

Metadata at bucket level. Updates with each build.

bucket_labels = {
  "os"        = "ubuntu"
  "team"      = "platform"
  "component" = "web"
}

build_labels (optional)

Metadata for each iteration. Immutable after build completes.

build_labels = {
  "build-time" = local.timestamp
  "git-commit" = var.git_commit
}

CI/CD Integration

GitHub Actions

name: Build and Push to HCP Packer

on:
  push:
    branches: [main]

env:
  HCP_CLIENT_ID: ${{ secrets.HCP_CLIENT_ID }}
  HCP_CLIENT_SECRET: ${{ secrets.HCP_CLIENT_SECRET }}
  HCP_ORGANIZATION_ID: ${{ secrets.HCP_ORGANIZATION_ID }}
  HCP_PROJECT_ID: ${{ secrets.HCP_PROJECT_ID }}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-packer@main

      - name: Build and push
        run: |
          packer init .
          packer build \
            -var "git_commit=${{ github.sha }}" \
            .

Querying in Terraform

data "hcp_packer_artifact" "ubuntu" {
  bucket_name  = "web-server"
  channel_name = "production"
  platform     = "aws"
  region       = "us-west-2"
}

resource "aws_instance" "web" {
  ami           = data.hcp_packer_artifact.ubuntu.external_identifier
  instance_type = "t3.micro"

  tags = {
    PackerBucket = data.hcp_packer_artifact.ubuntu.bucket_name
  }
}

Common Issues

Authentication Failed

  • Verify HCP_CLIENT_ID and HCP_CLIENT_SECRET
  • Ensure service principal has Contributor role
  • Check organization and project IDs

Bucket Name Mismatch

  • Keep bucket_name consistent across builds
  • Don't include timestamps in bucket_name
  • Creates new bucket if name changes

Build Fails

  • Packer fails immediately if can't push metadata
  • Prevents drift between artifacts and registry
  • Check network connectivity to HCP API

Best Practices

  • Consistent bucket names - Never change for same image type
  • Meaningful labels - Use for versions, teams, compliance
  • CI/CD automation - Automate builds and registry pushes
  • Immutable build labels - Put changing data (git SHA, date) in build_labels

References

hashicorp की और Skills

provider-actions
hashicorp
Implement Terraform Provider actions using the Plugin Framework. Use when developing imperative operations that execute at lifecycle events (before/after…
official
new-terraform-provider
hashicorp
Use this when scaffolding a new Terraform provider with the Plugin Framework: workspace layout, go module setup, provider server main.go, and a provider.go…
official
terraform-test
hashicorp
Comprehensive guide for writing and running Terraform tests. Use when creating test files (.tftest.hcl), writing test scenarios with run blocks, validating…
official
terraform-test
hashicorp
Terraform परीक्षण लिखने और चलाने के लिए व्यापक मार्गदर्शिका, जिसमें अभिकथन, मॉकिंग और मॉड्यूल सत्यापन शामिल हैं। .tftest.hcl सिंटैक्स का उपयोग करके परीक्षण फ़ाइलें लिखें, जिनमें रन ब्लॉक हों जो प्लान या अप्लाई मोड में निष्पादित हों, अनुक्रमिक और समानांतर निष्पादन का समर्थन करते हुए वैकल्पिक स्थिति पृथक्करण के साथ। संसाधन विशेषताओं, आउटपुट और डेटा स्रोतों पर शर्तों का अभिकथन करें; अमान्य इ
official
provider-actions
hashicorp
प्लगइन फ्रेमवर्क का उपयोग करके संसाधन जीवनचक्र घटनाओं पर अनिवार्य Terraform प्रदाता क्रियाएँ लागू करें। बनाने से पहले/बाद और अपडेट करने से पहले/बाद जीवनचक्र ट्रिगर का समर्थन करता है (Terraform 1.14.0 में नष्ट करने की घटनाएँ उपलब्ध नहीं हैं)। सही फ्रेमवर्क प्रकार, संग्रह के लिए ElementType और इनपुट सत्यापन के लिए वैलिडेटर के साथ उचित स्कीमा परिभाषा की आवश्यकता है। लंबे समय तक चलने वाले संच
official
aws-ami-builder
hashicorp
Packer के amazon-ebs बिल्डर के साथ कस्टम Amazon Machine Images बनाएं। स्रोत AMI से HCL टेम्पलेट्स का उपयोग करके AMI निर्माण को स्वचालित करता है, जिसमें अनुकूलन के लिए प्रोविज़नर (शेल स्क्रिप्ट, फ़ाइल अपलोड, कॉन्फ़िगरेशन प्रबंधन) शामिल हैं। ami_regions के माध्यम से बहु-क्षेत्र AMI वितरण और नाम, स्वामी और वर्चुअलाइज़ेशन प्रकार के आधार पर लचीली स्रोत AMI फ़िल्टरिंग का समर्थन करता है। पर्यावरण चर, AWS क्रेडेंशियल फ़ाइल या
official
new-terraform-provider
hashicorp
Plugin Framework का उपयोग करके एक नया Terraform प्रदाता तैयार करें। मानक "terraform-provider-" नामकरण परंपरा के साथ एक नया Go मॉड्यूल वर्कस्पेस उत्पन्न करता है और आवश्यक निर्भरताओं को आरंभ करता है। HashiCorp के Plugin Framework पैटर्न का पालन करते हुए एक टेम्पलेट main.go फ़ाइल प्रदान करता है, जिसमें अनुकूलन के लिए TODO मार्कर होते हैं। बिल्ड और टेस्ट कमांड चलाकर सेटअप को मान्य करता है ताकि यह सुनिश्चित हो सके कि प्रदाता संकलित होता है और प्रारंभिक जांच पास कर
official
azure-verified-modules
hashicorp
Azure Terraform मॉड्यूल के लिए AVM अनुपालन हेतु प्रमाणन आवश्यकताएँ और सर्वोत्तम अभ्यास। प्रदाता संस्करण बाधाओं (azurerm >= 4.0, < 5.0; azapi >= 2.0, < 3.0) को लागू करता है और पिन किए गए Terraform रजिस्ट्री स्रोतों के पक्ष में git-आधारित मॉड्यूल संदर्भों को प्रतिबंधित करता है। सभी पहचानकर्ताओं के लिए लोअर स्नेक_केसिंग, सटीक चर प्रकार, एंटी-भ्रष्टाचार परत पैटर्न के माध्यम से अलग-अलग आउटपुट
official