Skip to content

Commit

Permalink
Add a build argument for specifying the Packer version
Browse files Browse the repository at this point in the history
Set default version to `latest`

Using the Docker Action build argument workaround suggested from:
  https://github.community/t/feature-request-build-args-support-in-docker-container-actions/16846/6
  • Loading branch information
zmingxie committed Oct 21, 2020
1 parent a227696 commit f55a6d4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
FROM hashicorp/packer:1.6.4
FROM alpine:latest

LABEL "com.github.actions.name" = "Packer AMI Builder"
LABEL "com.github.actions.description" = "Use GitHub Action to execute Packer AMI build"
LABEL "com.github.actions.color"="blue"
LABEL "com.github.actions.icon"="anchor"

COPY docker-action /docker-action
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

RUN apk add --update --no-cache docker

ENTRYPOINT ["/entrypoint.sh"]
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
# Packer AMI Builder

Use GitHub Action to execute Packer build commands. This supports both HCL and
JSON packer template files.

For HCL Mode, set `packerArgs` to `build .` to load all `.pkr.hcl` files within
the dir. To use the override variable file, set `packerArgs` to
`build -var-file=overrides.pkrvars.hcl`

You can also use `packerVersion` to specify the Packer release you would like
to use.

## Input Parameters
| Name | Description | Mandatory | Default |
| ------------ | -------------------------------------------------------- | --------- | ------- |
| `packerArgs` | Arguments that will be passed down to the packer command | Yes | |
| `workDir` | Working directory inside the container | No | `.` |
| Name | Description | Mandatory | Default |
| --------------- | -------------------------------------------------------- | --------- | -------- |
| `packerVersion` | Version of Packer will be used | No | `latest` |
| `packerArgs` | Arguments that will be passed down to the packer command | Yes | |
| `workDir` | Working directory inside the container | No | `.` |

## Example
Create a workflow file (e.g `.github/workflows/ami-build.yaml`) like below:
Expand All @@ -30,6 +35,7 @@ jobs:
- name: Packer AMI Build
uses: zmingxie/packer-ami-builder@master
with:
packerVersion: '1.6.4'
packerArgs: 'build template.json'
workDir: '.'
env:
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ branding:
icon: 'anchor'

inputs:
packerVersion:
description: 'Version of Packer will be used'
required: false
default: 'latest'
packerArgs:
description: 'Arguments that will be passed down to the packer command'
required: true
Expand All @@ -16,3 +20,5 @@ inputs:
runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.packerVersion }}
6 changes: 6 additions & 0 deletions docker-action/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ARG packer_version
FROM hashicorp/packer:${packer_version}

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
10 changes: 10 additions & 0 deletions docker-action/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

cd "${INPUT_WORKDIR:-.}"

# Show Packer version
packer version

# Run packer with the given arguments
packer ${INPUT_PACKERARGS}
22 changes: 17 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#!/bin/bash
set -ex
#!/bin/sh

cd "${INPUT_WORKDIR:-.}"
PACKER_VERSION=$1

# Run packer with the given arguments
packer ${INPUT_PACKERARGS}
# The repo source code is cloned to $RUNNER_WORKSPACE/$REPO_NAME
# Setup the workspace path to that for easier access later
REPO_NAME=$(basename $RUNNER_WORKSPACE)
WS_PATH=$RUNNER_WORKSPACE/$REPO_NAME

cd /docker-action

echo "Creating a docker image with Packer version: $PACKER_VERSION"
docker build -t docker-action --build-arg packer_version="$PACKER_VERSION" .

echo "Run Docker Action container"
docker run -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION \
-e INPUT_PACKERARGS -e INPUT_PACKERVERSION -e INPUT_WORKDIR \
-v $WS_PATH:$GITHUB_WORKSPACE \
--workdir $GITHUB_WORKSPACE docker-action

0 comments on commit f55a6d4

Please sign in to comment.