Skip to content

Commit fdfca1a

Browse files
authored
Merge pull request #11 from dawitnida/feature/build-action
Packer GitHub build action included
2 parents 23254d1 + 57cf90d commit fdfca1a

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

build/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM hashicorp/packer:1.3.5
2+
3+
LABEL "com.github.actions.name" = "packer-build"
4+
LABEL "com.github.actions.description" = "Build packer template file in a directory"
5+
LABEL "com.github.actions.icon" = "circle"
6+
LABEL "com.github.actions.color" = "blue"
7+
8+
LABEL "repository" = "https://github.com/dawitnida/packer-github-actions"
9+
LABEL "homepage" = "https://github.com/dawitnida/packer-github-actions"
10+
LABEL "maintainer" = "Dawit Nida <dawit@dawitnida.com>"
11+
12+
RUN apk add --no-cache jq
13+
RUN apk add --no-cache curl
14+
15+
COPY entrypoint.sh /entrypoint.sh
16+
RUN chmod +x /entrypoint.sh
17+
ENTRYPOINT ["/entrypoint.sh"]

build/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Packer Build Action
2+
3+
Runs `packer build *.json` on release to validate and build image based on the template file in a directory
4+
This depends on the packer validate action, if the validation fails, it will print out error as pull request comment.
5+
Check out the inspect, validate actions and [packer build command][packer-build-doc] for further reference.
6+
7+
## Usage
8+
9+
To check this in action, please check [Packer actions demo project][packer-actions-demo] with a collection
10+
of sample packer template files.
11+
12+
Variables
13+
14+
- `PACKER_ACTION_WORKING_DIR` : Working directory
15+
- `TEMPLATE_FILE_NAME` : Packer template file
16+
- `ACTION_COMMENT` : Enable/Disable PR comment from validate result
17+
18+
```
19+
workflow "packer build template-y" {
20+
resolves = "packer-build-template-y"
21+
on = "release"
22+
}
23+
24+
action "packer-build-template-y" {
25+
uses = "dawitnida/packer-github-actions/build@master"
26+
needs = "packer-inspect-template-y"
27+
secrets = [
28+
"GITHUB_TOKEN",
29+
]
30+
env = {
31+
TEMPLATE_FILE_NAME = "packer-template-y.json"
32+
}
33+
}
34+
35+
action "filter-open-synced-pr" {
36+
uses = "actions/bin/filter@master"
37+
args = "action 'opened|synchronize'"
38+
}
39+
40+
workflow "packer inspect & validate template-y" {
41+
resolves = "packer-inspect-template-y"
42+
on = "pull_request"
43+
}
44+
45+
action "packer-validate-template-y" {
46+
uses = "dawitnida/packer-github-actions/validate@master"
47+
needs = "filter-open-synced-pr"
48+
secrets = [
49+
"GITHUB_TOKEN",
50+
]
51+
env = {
52+
TEMPLATE_FILE_NAME = "packer-template-y.json"
53+
}
54+
}
55+
56+
action "packer-inspect-template-y" {
57+
uses = "dawitnida/packer-github-actions/inspect@master"
58+
needs = "packer-validate-template-y"
59+
secrets = [
60+
"GITHUB_TOKEN",
61+
]
62+
env = {
63+
TEMPLATE_FILE_NAME = "packer-template-y.json"
64+
}
65+
}
66+
```
67+
68+
[packer-build-doc]: <https://www.packer.io/docs/commands/build.html>
69+
[packer-actions-demo]: <https://github.com/dawitnida/packer-actions-demo>

build/entrypoint.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Set the working directory for the template
5+
cd "${PACKER_ACTION_WORKING_DIR:-.}"
6+
7+
# Selected template file
8+
if [[ ! -f "$TEMPLATE_FILE_NAME" ]] && [[ $TEMPLATE_FILE_NAME != *.json ]]; then
9+
echo "${TEMPLATE_FILE_NAME} does not exit in the working directory (${PACKER_ACTION_WORKING_DIR})"
10+
echo ""
11+
echo "Setting the file to default."
12+
fi
13+
14+
set +e
15+
# Run packer template validator
16+
BUILD_OUTPUT=$(sh -c "packer build $* ${TEMPLATE_FILE_NAME}" 2>&1)
17+
BUILD_SUCCESS=$?
18+
echo "$BUILD_OUTPUT"
19+
set -e
20+
21+
# Capture the result and construct comment
22+
BUILD_COMMENT=""
23+
if [ $BUILD_SUCCESS -ne 0 ]; then
24+
BUILD_COMMENT="#### \`packer build \` Failed
25+
\`\`\`
26+
$BUILD_OUTPUT
27+
\`\`\`
28+
29+
- Template: ${TEMPLATE_FILE_NAME}
30+
- Workflow: ${GITHUB_WORKFLOW}
31+
- Action: ${GITHUB_ACTION}
32+
- Reference: ${GITHUB_REF}"
33+
34+
else
35+
BUILD_COMMENT="#### \`packer build\` Success
36+
\`\`\`
37+
$BUILD_OUTPUT
38+
\`\`\`
39+
40+
- Template: ${TEMPLATE_FILE_NAME}
41+
- Workflow: ${GITHUB_WORKFLOW}
42+
- Action: ${GITHUB_ACTION}
43+
- Reference: ${GITHUB_REF}"
44+
45+
fi
46+
47+
# Enable/disable comment on build action on the PR
48+
if [[ "$ACTION_COMMENT" == "1" ]] || [[ "$ACTION_COMMENT" == "false" ]]; then
49+
exit $BUILD_SUCCESS
50+
fi
51+
52+
# Spit out the validation output for reference as PR comment
53+
BUILD_PAYLOAD=$(echo '{}' | jq --arg body "$BUILD_COMMENT" '.body = $body')
54+
BUILD_COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url)
55+
/usr/bin/curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$BUILD_PAYLOAD" "$BUILD_COMMENTS_URL" > /dev/null
56+
57+
exit $BUILD_SUCCESS

0 commit comments

Comments
 (0)