diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..87c9677 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +### Packer template +# Cache objects +packer_cache/ + +# For built boxes +*.box + +.idea/* + diff --git a/validate/Dockerfile b/validate/Dockerfile new file mode 100644 index 0000000..fce39b5 --- /dev/null +++ b/validate/Dockerfile @@ -0,0 +1,16 @@ +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" = "alert-circle" +LABEL "com.github.actions.color" = "orange" + +LABEL "repository" = "https://github.com/dawitnida/packer-github-actions" +LABEL "homepage" = "https://github.com/dawitnida/packer-github-actions" +LABEL "maintainer" = "Dawit Nida " + +RUN apk --no-cache add jq + +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] diff --git a/validate/entrypoint.sh b/validate/entrypoint.sh new file mode 100755 index 0000000..cbd31fc --- /dev/null +++ b/validate/entrypoint.sh @@ -0,0 +1,25 @@ +#!/bin/sh +set -e + +set +e +# Run packer template validator +VALIDATE_OUTPUT=$(sh -c "packer validate -no-color $*" 2>&1) +VALIDATE_SUCCESS=$? +echo "$VALIDATE_OUTPUT" +set -e + +# Capture the result +if [ $VALIDATE_SUCCESS -eq 0 ]; then + exit 0 +fi + +# Spit out the validation output for reference +VALIDATE_COMMENT="#### \`packer validate\` Failed +\`\`\` +$VALIDATE_OUTPUT +\`\`\`" +VALIDATE_PAYLOAD=$(echo '{}' | jq --arg body "$VALIDATE_COMMENT" '.body = $body') +VALIDATE_COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url) +curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$VALIDATE_PAYLOAD" "$VALIDATE_COMMENTS_URL" > /dev/null + +exit $VALIDATE_SUCCESS