Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 7827b13

Browse files
authored
Adds packer hooks (#1)
* adds hooks for packer fix and packer fix (with validation) * adds README.md
1 parent 7e442f1 commit 7827b13

5 files changed

Lines changed: 320 additions & 0 deletions

File tree

.github/ISSUE_TEMPLATE/bug.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
name: Bug
3+
about: A bug or problem with the packer-<insert type> pre-commit-hook
4+
labels: bug
5+
---
6+
7+
<!-- Please provide a general summary of the issue in the Title above -->
8+
9+
# Expected Behavior
10+
11+
<!-- Explain what you expect to happen -->
12+
13+
## Current Behavior
14+
15+
<!-- Explain what actually happens -->
16+
17+
## Steps to Reproduce
18+
19+
<!-- Explain how to reproduce the problem -->
20+
<!-- If relevant, include code, screenshots or links -->
21+
22+
## Environment
23+
24+
1.) Output of `pre-commit --version`
25+
26+
```sh
27+
28+
```
29+
30+
2.) Any other relevant environment information:
31+
32+
```sh
33+
34+
```

.github/workflows/code-quality.yml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
3+
name: Code Quality
4+
5+
on:
6+
pull_request:
7+
8+
env:
9+
PARENT_ORG: operatehappy
10+
META_PATH: dotfiles-org
11+
SELF_PATH: repository
12+
PYTHON_VERSION: 3.8
13+
PYTHON_ARCH: x64
14+
PRECOMMIT_CACHE_PATH: ".cache/pre-commit"
15+
PRECOMMIT_VERSION: 1.18.3
16+
PRECOMMIT_CONFIG: ".pre-commit-config.yaml"
17+
18+
jobs:
19+
pre-commit:
20+
runs-on: ubuntu-latest
21+
name: pre-commit
22+
23+
steps:
24+
- name: Checkout Meta Repository
25+
uses: actions/checkout@v2
26+
with:
27+
repository: ${{ env.PARENT_ORG }}/${{ env.META_PATH }}
28+
ref: master
29+
path: ${{ env.META_PATH }}
30+
fetch-depth: 1
31+
32+
- name: Checkout Repository
33+
uses: actions/checkout@v2
34+
with:
35+
fetch-depth: 1
36+
path: ${{ env.SELF_PATH }}
37+
38+
- name: Setup Python
39+
uses: actions/setup-python@v1
40+
with:
41+
python-version: ${{ env.PYTHON_VERSION }}
42+
architecture: ${{ env.PYTHON_ARCH }}
43+
44+
- name: Update and Restore pip Binaries Cache
45+
uses: actions/cache@v1
46+
id: cache-pip-binaries
47+
with:
48+
path: "${{ env.pythonLocation }}/bin"
49+
key: "pip-binaries-${{ env.PYTHON_VERSION }}-${{ env.PRECOMMIT_VERSION }}"
50+
51+
- name: Update and Restore pip Packages Cache
52+
uses: actions/cache@v1
53+
id: cache-pip-packages
54+
with:
55+
path: "${{ env.pythonLocation }}/lib/python${{ env.PYTHON_VERSION }}/site-packages"
56+
key: "pip-packages-${{ env.PYTHON_VERSION }}-${{ env.PRECOMMIT_VERSION }}"
57+
58+
- name: Update and Restore pre-commit Cache
59+
uses: actions/cache@v1
60+
id: cache-precommit-hooks
61+
with:
62+
path: "~/${{ env.PRECOMMIT_CACHE_PATH }}"
63+
key: ${{ hashFiles(format('{0}/{1}/{2}', github.workspace, env.META_PATH, env.PRECOMMIT_CONFIG )) }}
64+
65+
- name: Install `pre-commit` via `pip`
66+
run: |
67+
pip \
68+
install "pre-commit==${{ env.PRECOMMIT_VERSION }}"
69+
shell: sh
70+
if: steps.cache-pip-binaries.outputs.cache-hit != 'true' && steps.cache-pip-packages.outputs.cache-hit != 'true'
71+
72+
- name: Setup `pre-commit`
73+
run: |
74+
pre-commit \
75+
install \
76+
--config "${{ github.workspace }}${{ env.UPSTREAM_REP }}/${{ env.PRECOMMIT_CONFIG }}"
77+
shell: sh
78+
working-directory: "${{ github.workspace }}/${{ env.SELF_PATH }}"
79+
if: steps.cache-precommit.outputs.cache-hit != 'true'
80+
81+
- name: Run `pre-commit`
82+
run: |
83+
pre-commit \
84+
run \
85+
--config "${{ github.workspace }}/${{ env.META_PATH }}/${{ env.PRECOMMIT_CONFIG }}" \
86+
--all-files
87+
working-directory: "${{ github.workspace }}/${{ env.SELF_PATH }}"
88+
shell: sh
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
3+
name: Repository Management
4+
5+
on:
6+
pull_request:
7+
8+
jobs:
9+
assign-pr-to-author:
10+
runs-on: ubuntu-latest
11+
name: assign-pr-to-author
12+
13+
steps:
14+
- name: Assign Pull Request to Author
15+
uses: technote-space/assign-author@v1
16+
if: github.event_name == 'pull_request' && github.event.action == 'opened'
17+
with:
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.pre-commit-hooks.yaml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
3+
-
4+
id: packer-validate
5+
name: Validate Packer template(s)
6+
entry: packer validate
7+
language: system
8+
types: [json]
9+
files: ^packer.json$
10+
always_run: false
11+
verbose: false
12+
pass_filenames: true
13+
description: Validates a template against builders, provisioners
14+
minimum_pre_commit_version: 1.15.0
15+
args: [
16+
-syntax-only
17+
]
18+
19+
-
20+
id: docker-packer-validate
21+
name: Validate Packer template(s)
22+
entry: hashicorp/packer:light validate
23+
language: docker_image
24+
types: [json]
25+
files: ^packer.json$
26+
always_run: false
27+
verbose: false
28+
pass_filenames: true
29+
description: Validates a template against builders, provisioners
30+
minimum_pre_commit_version: 1.15.0
31+
args: [
32+
-syntax-only
33+
]
34+
35+
-
36+
id: packer-fix
37+
name: Fix Packer template(s) with backwards incompatibilities
38+
entry: packer fix
39+
language: system
40+
types: [json]
41+
files: ^packer.json$
42+
always_run: false
43+
verbose: false
44+
pass_filenames: true
45+
description: Attempts to fix incompatibilities in a template
46+
minimum_pre_commit_version: 1.15.0
47+
48+
-
49+
id: docker-packer-fix
50+
name: Fix Packer template(s) with backwards incompatibilities
51+
entry: hashicorp/packer:light fix
52+
language: docker_image
53+
types: [json]
54+
files: ^packer.json$
55+
always_run: false
56+
verbose: false
57+
pass_filenames: true
58+
description: Attempts to fix incompatibilities in a template
59+
minimum_pre_commit_version: 1.15.0
60+
61+
-
62+
id: packer-validate-and-fix
63+
name: Validate Packer template(s) and fix backwards incompatibilities
64+
entry: packer fix -validate=true
65+
language: system
66+
types: [json]
67+
files: ^packer.json$
68+
always_run: false
69+
verbose: false
70+
pass_filenames: true
71+
description: Validates and attempts to fix incompatibilities in a template
72+
minimum_pre_commit_version: 1.15.0
73+
74+
-
75+
id: docker-packer-validate-and-fix
76+
name: Validate Packer template(s) and fix backwards incompatibilities
77+
entry: hashicorp/packer:light fix -validate=true
78+
language: docker_image
79+
types: [json]
80+
files: ^packer.json$
81+
always_run: false
82+
verbose: false
83+
pass_filenames: true
84+
description: Validates and attempts to fix incompatibilities in a template
85+
minimum_pre_commit_version: 1.15.0

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# `pre-commit-packer`
2+
3+
> Packer-specific hooks for use with [pre-commit](https://pre-commit.com).
4+
5+
## Table of Contents
6+
7+
- [Requirements](#requirements)
8+
- [Dependencies](#dependencies)
9+
- [Usage](#usage)
10+
- [List of Hooks](#list-of-hooks)
11+
- [Configuring Hooks](#configuring-hooks)
12+
- [Using Docker](#using-docker)
13+
- [Notes](#notes)
14+
- [Author Information](#author-information)
15+
- [License](#license)
16+
17+
## Requirements
18+
19+
- `pre-commit` version `1.15.0` or newer
20+
21+
## Dependencies
22+
23+
- `packer` for hooks that are prefixed with `packer-`
24+
- `docker` for hooks that are prefixed with `docker-`
25+
26+
## Usage
27+
28+
The hooks in this repository can be used by adding the following stub to your `.pre-commit-config.yaml` file:
29+
30+
```yaml
31+
---
32+
-
33+
repo: https://github.com/operatehappy/pre-commit-packer
34+
rev: 1.0.0
35+
hooks:
36+
- id: packer-validate
37+
files: packer*.json
38+
39+
- id: packer-fix
40+
files: packer*.json
41+
42+
- id: packer-validate-and-fix
43+
files: packer*.json
44+
```
45+
46+
### List of Hooks
47+
48+
- `packer-validate` wraps the `validate` [command](https://packer.io/docs/commands/validate.html) to parse Packer template(s) and check template configuration against any used builders, provisioners, and processors.
49+
50+
- `packer-fix` wraps the `fix` [command](https://packer.io/docs/commands/fix.html) and attempts to fix (known) backwards incompatibilities in Packer template(s).
51+
52+
- `packer-validate-and-fix` wraps the `fix` [command](https://packer.io/docs/commands/fix.html) and additionally validates the fixed Packer template(s).
53+
54+
### Configuring Hooks
55+
56+
All hooks accept the following configuration options:
57+
58+
- `files` accepts a string (wildcards are supported) to define which file(s) the hook should target
59+
- `args` accepts an array with one or more valid command-specific CLI arguments
60+
61+
The `files` configuration option defaults to `packer.json`
62+
63+
The `args` configuration option is most useful with the `packer-validate` action as it allows for passing additional arguments such as a path to a variables file (`-var-file=packer.vars.json`). The upstream documentation for the `validate` command provides an overview of all [available options](https://packer.io/docs/commands/validate.html#options).
64+
65+
### Using Docker
66+
67+
The hooks provided by `pre-commit-packer` support invocation through a containerized version of the Packer [Docker Image](https://hub.docker.com/r/hashicorp/packer).
68+
69+
This invocation defaults to the `light` [tag](https://hub.docker.com/r/hashicorp/packer/tags?page=1&name=light), but is otherwise functionally identical to a direct invocation via the `packer` binary.
70+
71+
The Docker-specific hooks are prefixed with `docker-`:
72+
73+
- `packer-validate` becomes `docker-packer-validate`
74+
- `packer-fix` becomes `docker-packer-fix`
75+
- `packer-validate-and-fix` becomes `docker-packer-validate-and-fix`
76+
77+
## Notes
78+
79+
- In the [code example](#usage), the `1.0.0` version of `pre-commit-packer` has been selected. The [Releases](https://github.com/operatehappy/pre-commit-packer/releases) tab provides an overview of all available versions.
80+
81+
## Author Information
82+
83+
This repository is maintained by the contributors listed on [GitHub](https://github.com/operatehappy/pre-commit-packer/graphs/contributors)
84+
85+
Development of this module was sponsored by [Operate Happy](https://github.com/operatehappy).
86+
87+
## License
88+
89+
Licensed under the Apache License, Version 2.0 (the "License").
90+
91+
You may obtain a copy of the License at [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
92+
93+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an _"AS IS"_ basis, without WARRANTIES or conditions of any kind, either express or implied.
94+
95+
See the License for the specific language governing permissions and limitations under the License.

0 commit comments

Comments
 (0)