Skip to content

Commit

Permalink
Packer validate action, part of the complete [Packer GitHub Actions](h…
Browse files Browse the repository at this point in the history
  • Loading branch information
dawitnida committed Apr 7, 2019
0 parents commit 6e48879
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Packer template
# Cache objects
packer_cache/

# For built boxes
*.box

.idea/*

17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM hashicorp/packer:1.3.5

LABEL "com.github.actions.name" = "Packer validate"
LABEL "com.github.actions.description" = "Validate packer template file in a directory"
LABEL "com.github.actions.icon" = "check-circle"
LABEL "com.github.actions.color" = "blue"

LABEL "repository" = "https://github.com/dawitnida/packer-validate-action"
LABEL "homepage" = "https://github.com/dawitnida/packer-validate-action"
LABEL "maintainer" = "Dawit Nida <dawit@dawitnida.com>"

RUN apk add --no-cache jq
RUN apk add --no-cache curl

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Packer Validate Action

These is unofficial Packer [GitHub Actions][github-actions] which allows you to run packer validation and inspection on
pull requests to review Packer template changes and potentially build on pull merge.
Check out the [official Packer documentation][packer-doc] for further reference.

Runs `packer validate *.json` on pull request to validate the syntax and configuration of a template file in a directory
If the validation fails, it will print out error as pull request comment.
Check out the [packer validate command][packer-validate-doc] for further reference.

## Usage

To check this in action, please check [Packer actions demo project][packer-actions-demo] with a collection
of sample packer template files.

Variables

- `PACKER_ACTION_WORKING_DIR` : Working directory
- `TEMPLATE_FILE_NAME` : Packer template file
- `ACTION_COMMENT` : Enable/Disable PR comment from validate result

```
workflow "packer validate docker-image-template" {
resolves = "packer-validate-docker-image-template"
on = "pull_request"
}
action "filter-open-synced-pr" {
uses = "actions/bin/filter@master"
args = "action 'opened|synchronize'"
}
# For single template (eg. dockers dir contains *.json template)
action "packer-validate-docker-image-template" {
uses = "dawitnida/packer-github-actions/validate@master"
needs = "filter-open-synced-pr"
secrets = [
"GITHUB_TOKEN",
]
env = {
TEMPLATE_FILE_NAME = "*.json"
PACKER_ACTION_WORKING_DIR = "dockers"
}
}
workflow "packer validate template-x with var-file" {
resolves = "packer-validate-template-x"
on = "pull_request"
}
# For specific template file (eg. packer-template-x.json) with var-file (global-vars.json) arg
action "packer-validate-template-x" {
uses = "dawitnida/packer-github-actions/validate@master"
needs = "filter-open-synced-pr"
secrets = [
"GITHUB_TOKEN",
]
args = [
"-var-file=global-vars.json",
]
env = {
TEMPLATE_FILE_NAME = "packer-template-x.json"
}
}
workflow "packer validate template-y without arg" {
resolves = "packer-validate-template-y"
on = "pull_request"
}
# For specific template file (eg. packer-template-y.json) without any args
action "packer-validate-template-y" {
uses = "dawitnida/packer-github-actions/validate@master"
needs = "filter-open-synced-pr"
secrets = [
"GITHUB_TOKEN",
]
env = {
TEMPLATE_FILE_NAME = "packer-template-y.json"
}
}
```

**Figure 1.** *Packer validate without args failed with a comment*
![failed-validation](assets/packer-template-y.png)

**Figure 2.** *Packer validate success & failed outputs*
![success-failed-output](assets/fail-success-validation.png)

**Figure 3.** *Packer validate complete check list diagram*
![checks-list-diagram](assets/action-results.png)

### Author
[Dawit Nida](https://github.com/dawitnida)

[packer-validate-doc]: <https://www.packer.io/docs/commands/validate.html>
[packer-actions-demo]: <https://github.com/dawitnida/packer-actions-demo>
[packer-actions-demo]: <https://github.com/dawitnida/packer-actions-demo>
[github-actions]: <https://github.com/features/actions>
[packer-doc]: <https://www.packer.io/docs/index.html>
Binary file added assets/action-results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/fail-success-validation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/packer-template-y.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh
set -e

# Set the working directory for the template
cd "${PACKER_ACTION_WORKING_DIR:-.}"

# Selected template file
if [[ ! -f "$TEMPLATE_FILE_NAME" ]] && [[ $TEMPLATE_FILE_NAME != *.json ]]; then
echo "${TEMPLATE_FILE_NAME} does not exit in the working directory (${PACKER_ACTION_WORKING_DIR})"
echo ""
echo "Setting the file to default."
fi

set +e
# Run packer template validator
VALIDATE_OUTPUT=$(sh -c "packer validate $* ${TEMPLATE_FILE_NAME}" 2>&1)
VALIDATE_SUCCESS=$?
echo "$VALIDATE_OUTPUT"
set -e

# Capture the result and construct comment
VALIDATE_COMMENT=""
if [ $VALIDATE_SUCCESS -ne 0 ]; then
VALIDATE_COMMENT="#### \`packer validate \` Failed
\`\`\`
$VALIDATE_OUTPUT
\`\`\`
- Template: ${TEMPLATE_FILE_NAME}
- Workflow: ${GITHUB_WORKFLOW}
- Action: ${GITHUB_ACTION}
- Reference: ${GITHUB_REF}"

else
VALIDATE_COMMENT="#### \`packer validate\` Success
\`\`\`
$VALIDATE_OUTPUT
\`\`\`
- Template: ${TEMPLATE_FILE_NAME}
- Workflow: ${GITHUB_WORKFLOW}
- Action: ${GITHUB_ACTION}
- Reference: ${GITHUB_REF}"

fi

# Enable/disable comment on validate action on the PR
if [[ "$ACTION_COMMENT" == "1" ]] || [[ "$ACTION_COMMENT" == "false" ]]; then
exit $VALIDATE_SUCCESS
fi

# Spit out the validation output for reference as PR comment
VALIDATE_PAYLOAD=$(echo '{}' | jq --arg body "$VALIDATE_COMMENT" '.body = $body')
VALIDATE_COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url)
/usr/bin/curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$VALIDATE_PAYLOAD" "$VALIDATE_COMMENTS_URL" > /dev/null

exit $VALIDATE_SUCCESS

0 comments on commit 6e48879

Please sign in to comment.