|
| 1 | +name: Image Builders Comparison |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + run_packer: |
| 7 | + description: 'Run Packer build' |
| 8 | + type: boolean |
| 9 | + default: true |
| 10 | + run_ec2_imagebuilder: |
| 11 | + description: 'Run EC2 Image Builder' |
| 12 | + type: boolean |
| 13 | + default: true |
| 14 | + |
| 15 | +env: |
| 16 | + AWS_REGION: us-west-2 |
| 17 | + |
| 18 | +jobs: |
| 19 | + packer-build: |
| 20 | + name: HashiCorp Packer Build |
| 21 | + runs-on: ubuntu-latest |
| 22 | + if: ${{ github.event.inputs.run_packer == 'true' }} |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout |
| 26 | + uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Configure AWS credentials |
| 29 | + uses: aws-actions/configure-aws-credentials@v4 |
| 30 | + with: |
| 31 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 32 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 33 | + aws-region: ${{ env.AWS_REGION }} |
| 34 | + |
| 35 | + - name: Setup Packer |
| 36 | + uses: hashicorp/setup-packer@main |
| 37 | + with: |
| 38 | + version: "1.9.4" |
| 39 | + |
| 40 | + - name: Create Packer template |
| 41 | + run: | |
| 42 | + cat > packer-template.pkr.hcl << 'EOF' |
| 43 | + packer { |
| 44 | + required_plugins { |
| 45 | + amazon = { |
| 46 | + source = "github.com/hashicorp/amazon" |
| 47 | + version = "~> 1" |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +
|
| 52 | + source "amazon-ebs" "ubuntu" { |
| 53 | + ami_name = "packer-demo-{{timestamp}}" |
| 54 | + instance_type = "t3.micro" |
| 55 | + region = "us-west-2" |
| 56 | + |
| 57 | + source_ami_filter { |
| 58 | + filters = { |
| 59 | + name = "ubuntu/images/hvm-ssd/ubuntu-22.04-amd64-server-*" |
| 60 | + root-device-type = "ebs" |
| 61 | + virtualization-type = "hvm" |
| 62 | + } |
| 63 | + most_recent = true |
| 64 | + owners = ["099720109477"] |
| 65 | + } |
| 66 | + |
| 67 | + ssh_username = "ubuntu" |
| 68 | + |
| 69 | + tags = { |
| 70 | + Name = "packer-demo-{{timestamp}}" |
| 71 | + Tool = "HashiCorp-Packer" |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | + build { |
| 76 | + sources = ["source.amazon-ebs.ubuntu"] |
| 77 | + |
| 78 | + provisioner "shell" { |
| 79 | + inline = [ |
| 80 | + "sudo apt-get update", |
| 81 | + "sudo apt-get install -y nginx", |
| 82 | + "sudo systemctl enable nginx", |
| 83 | + "echo '<h1>Built with HashiCorp Packer</h1>' | sudo tee /var/www/html/index.html" |
| 84 | + ] |
| 85 | + } |
| 86 | + } |
| 87 | + EOF |
| 88 | +
|
| 89 | + - name: Initialize and build with Packer |
| 90 | + run: | |
| 91 | + echo "🚀 Starting Packer build..." |
| 92 | + packer init packer-template.pkr.hcl |
| 93 | + packer validate packer-template.pkr.hcl |
| 94 | + packer build packer-template.pkr.hcl | tee packer-build.log |
| 95 | + echo "✅ Packer build completed!" |
| 96 | +
|
| 97 | + - name: Upload Packer logs |
| 98 | + uses: actions/upload-artifact@v4 |
| 99 | + with: |
| 100 | + name: packer-build-logs |
| 101 | + path: packer-build.log |
| 102 | + |
| 103 | + ec2-imagebuilder-build: |
| 104 | + name: AWS EC2 Image Builder Build |
| 105 | + runs-on: ubuntu-latest |
| 106 | + if: ${{ github.event.inputs.run_ec2_imagebuilder == 'true' }} |
| 107 | + |
| 108 | + steps: |
| 109 | + - name: Checkout |
| 110 | + uses: actions/checkout@v4 |
| 111 | + |
| 112 | + - name: Configure AWS credentials |
| 113 | + uses: aws-actions/configure-aws-credentials@v4 |
| 114 | + with: |
| 115 | + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} |
| 116 | + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} |
| 117 | + aws-region: ${{ env.AWS_REGION }} |
| 118 | + |
| 119 | + - name: Run EC2 Image Builder pipeline |
| 120 | + run: | |
| 121 | + echo "🚀 Starting EC2 Image Builder..." |
| 122 | + |
| 123 | + # Create component |
| 124 | + cat > component.yml << 'EOF' |
| 125 | + name: InstallNginx |
| 126 | + description: Install Nginx web server |
| 127 | + schemaVersion: 1.0 |
| 128 | + phases: |
| 129 | + - name: build |
| 130 | + steps: |
| 131 | + - name: UpdateOS |
| 132 | + action: UpdateOS |
| 133 | + - name: InstallNginx |
| 134 | + action: ExecuteBash |
| 135 | + inputs: |
| 136 | + commands: |
| 137 | + - apt-get update |
| 138 | + - apt-get install -y nginx |
| 139 | + - systemctl enable nginx |
| 140 | + - echo '<h1>Built with EC2 Image Builder</h1>' > /var/www/html/index.html |
| 141 | + EOF |
| 142 | +
|
| 143 | + COMPONENT_ARN=$(aws imagebuilder create-component \ |
| 144 | + --name "nginx-component-$(date +%s)" \ |
| 145 | + --semantic-version "1.0.0" \ |
| 146 | + --description "Install Nginx" \ |
| 147 | + --platform Linux \ |
| 148 | + --data file://component.yml \ |
| 149 | + --query 'componentBuildVersionArn' \ |
| 150 | + --output text) |
| 151 | + |
| 152 | + echo "✅ Component created: $COMPONENT_ARN" |
| 153 | + |
| 154 | + # Get base AMI and create recipe |
| 155 | + BASE_AMI=$(aws ec2 describe-images \ |
| 156 | + --owners 099720109477 \ |
| 157 | + --filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-22.04-amd64-server-*" \ |
| 158 | + --query 'Images | sort_by(@, &CreationDate) | [-1].ImageId' \ |
| 159 | + --output text) |
| 160 | + |
| 161 | + RECIPE_ARN=$(aws imagebuilder create-image-recipe \ |
| 162 | + --name "nginx-recipe-$(date +%s)" \ |
| 163 | + --semantic-version "1.0.0" \ |
| 164 | + --description "Ubuntu with Nginx" \ |
| 165 | + --parent-image "$BASE_AMI" \ |
| 166 | + --components componentArn=$COMPONENT_ARN \ |
| 167 | + --query 'imageRecipeArn' \ |
| 168 | + --output text) |
| 169 | + |
| 170 | + echo "✅ Recipe created: $RECIPE_ARN" |
| 171 | + |
| 172 | + # Create infrastructure config |
| 173 | + INFRA_ARN=$(aws imagebuilder create-infrastructure-configuration \ |
| 174 | + --name "basic-infra-$(date +%s)" \ |
| 175 | + --instance-types t3.micro \ |
| 176 | + --query 'infrastructureConfigurationArn' \ |
| 177 | + --output text) |
| 178 | + |
| 179 | + echo "✅ Infrastructure config created: $INFRA_ARN" |
| 180 | + |
| 181 | + # Start build |
| 182 | + IMAGE_ARN=$(aws imagebuilder create-image \ |
| 183 | + --image-recipe-arn "$RECIPE_ARN" \ |
| 184 | + --infrastructure-configuration-arn "$INFRA_ARN" \ |
| 185 | + --query 'imageBuildVersionArn' \ |
| 186 | + --output text) |
| 187 | + |
| 188 | + echo "✅ Image build started: $IMAGE_ARN" |
| 189 | + echo "Build ARNs:" > ec2-imagebuilder-build.log |
| 190 | + echo "Image: $IMAGE_ARN" >> ec2-imagebuilder-build.log |
| 191 | + echo "Recipe: $RECIPE_ARN" >> ec2-imagebuilder-build.log |
| 192 | + echo "Component: $COMPONENT_ARN" >> ec2-imagebuilder-build.log |
| 193 | +
|
| 194 | + - name: Upload EC2 Image Builder logs |
| 195 | + uses: actions/upload-artifact@v4 |
| 196 | + with: |
| 197 | + name: ec2-imagebuilder-logs |
| 198 | + path: ec2-imagebuilder-build.log |
0 commit comments