Skip to content

Commit

Permalink
Simplify the argument passing
Browse files Browse the repository at this point in the history
Assuming you know how to use packer, we don't need to setup an argument
template for this. This also makes the whole action more flexible.
  • Loading branch information
zmingxie committed Apr 3, 2020
1 parent edb9cb7 commit c0040bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 46 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Packer AMI Builder
Use GitHub Action to execute Packer AMI build. This supports both HCL and JSON packer template files.
Use GitHub Action to execute Packer build commands. This supports both HCL and
JSON packer template files.

For HCL Mode, set `templateFile` to `.` to read all `pkr.hcl` files within your working directory.
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`

## Input Parameters
| Name | Description | Mandatory | Default |
| -------------- | -------------------------------------- | --------- | ------- |
| `templateFile` | Packer template file name | Yes | |
| `varFile` | Optional user variables file | No | `NULL` |
| `workDir` | Working directory inside the container | No | `.` |
| Name | Description | Mandatory | Default |
| ------------ | -------------------------------------------------------- | --------- | ------- |
| `packerArgs` | Arguments that will be passed down to the packer command | Yes | |
| `workDir` | Working directory inside the container | No | `.` |

## Example
Create a workflow file (`.github/workflows/ami-build.yaml`)like below:
Create a workflow file (e.g `.github/workflows/ami-build.yaml`) like below:

```yaml
name: Build an AMI using Packer
Expand All @@ -28,8 +30,7 @@ jobs:
- name: Packer AMI Build
uses: zmingxie/packer-ami-builder@master
with:
templateFile: 'packer-template.json'
varFile: 'packer-vars.json'
packerArgs: 'build template.json'
workDir: '.'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
Expand Down
8 changes: 2 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ branding:
icon: 'anchor'

inputs:
templateFile:
description: 'Packer template file name'
packerArgs:
description: 'Arguments that will be passed down to the packer command'
required: true
varFile:
description: 'Optional user variables file'
required: false
default: NULL
workDir:
description: 'Working directory inside the container'
required: false
Expand Down
33 changes: 3 additions & 30 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
#!/bin/bash
set -e
set -ex

cd "${INPUT_WORKDIR:-.}"

# By default, HCL_MODE is disabled
HCL_MODE=false
if [[ ${INPUT_TEMPLATEFILE} == '.' || ${INPUT_TEMPLATEFILE} =~ '*.pkr.hcl' ]]; then
HCL_MODE=true
fi
echo "[INFO] HCL_MODE is ${HCL_MODE}"

if [[ "${HCL_MODE}" == false && ! -f "${INPUT_TEMPLATEFILE}" ]]; then
echo "Error! Template file [${INPUT_TEMPLATEFILE}] is not found!"
exit 1
fi

# Validate the template (Currently doesn't work with HCL files)
if [[ "${HCL_MODE}" == false ]]; then
packer validate ${INPUT_TEMPLATEFILE}
if [[ $? -ne 0 ]]; then
echo "Error! Packer template is not valid"
exit 1
fi
fi

if [[ -f "${INPUT_VARFILE}" ]]; then
VAR_FILE_FLAG="-var-file=${INPUT_VARFILE}"
else
VAR_FILE_FLAG=""
fi

# Run packer build
packer build ${VAR_FILE_FLAG} ${INPUT_TEMPLATEFILE}
# Run packer with the given arguments
packer ${INPUT_PACKERARGS}

0 comments on commit c0040bc

Please sign in to comment.