Created
November 19, 2023 04:29
-
-
Save RoseSecurity/76e968d62d01f1ce129d6b909b01cbbe to your computer and use it in GitHub Desktop.
A GitHub Action for establishing which HCL directories have been modified, setting up Packer, and running packer fmt against the templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Packer Format | |
on: | |
pull_request: | |
types: [opened, synchronize] | |
paths: | |
- '**/*' | |
permissions: | |
contents: read | |
pull-requests: write | |
env: | |
PRODUCT_VERSION: "1.9.4" | |
jobs: | |
find_hcl: | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.state == 'open' | |
outputs: | |
run_fmt: ${{ steps.find-modified-hcl.outputs.run_fmt }} | |
hcl_dirs: ${{ steps.find-modified-hcl.outputs.hcl_dirs }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Find modified HCL | |
id: find-modified-hcl | |
shell: bash -x -e -o pipefail {0} | |
env: | |
BASE_REF: ${{ github.base_ref }} | |
HEAD_REF: ${{ github.head_ref }} | |
run: | | |
MODIFIED_FILES=($(git diff --name-only origin/${BASE_REF} origin/${HEAD_REF})) | |
[[ -n "$MODIFIED_FILES" ]] && MODIFIED_DIRS=($(xargs -n 1 dirname <<< "${MODIFIED_FILES[@]}" | sort | uniq)) || true | |
[[ -n "$MODIFIED_DIRS" ]] && MODIFIED_HCL_DIRS=($(for f in "${MODIFIED_FILES[@]}"; do [[ "$f" == *.hcl ]] && dirname "$f" || true; done | sort | uniq)) || true | |
if [[ -z "$MODIFIED_HCL_DIRS" ]]; then | |
echo "No HCL files changed in this PR. Skipping Packer fmt." | |
echo "run_fmt=false" >> $GITHUB_OUTPUT | |
echo "hcl_dirs=[]" >> $GITHUB_OUTPUT | |
else | |
echo "Running checks on the following Packer components:" | |
printf -- "- %s\n" "${MODIFIED_HCL_DIRS[@]}" | |
echo "run_fmt=true" >> $GITHUB_OUTPUT | |
echo "hcl_dirs=$(IFS=,; printf '["%s"]\n' "${MODIFIED_HCL_DIRS[*]}" | sed 's/,/", "/g')" >> $GITHUB_OUTPUT | |
fi | |
packer: | |
runs-on: ubuntu-latest | |
needs: find_hcl | |
if: github.event.pull_request.state == 'open' && needs.find_hcl.outputs.run_fmt == 'true' | |
strategy: | |
fail-fast: false | |
matrix: | |
dir: ${{ fromJson(needs.find_hcl.outputs.hcl_dirs) }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Setup `packer` | |
uses: hashicorp/setup-packer@main | |
id: setup | |
with: | |
version: ${{ env.PRODUCT_VERSION }} | |
- name: Run `packer fmt` | |
id: packer-fmt | |
run: | | |
hcl_dirs="${{ matrix.dir }}" | |
for dir in $hcl_dirs; do | |
cd $dir | |
packer fmt . | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment