github-action-test icon indicating copy to clipboard operation
github-action-test copied to clipboard

Add automatic Dockerfile formatting enforcement using dockerfmt with auto-commit

Open Copilot opened this issue 5 months ago • 2 comments

Overview

This PR introduces consistent Dockerfile formatting across the repository using dockerfmt, with automatic enforcement through CI and optional local pre-commit hooks. The CI workflow automatically commits formatting fixes to PRs.

Changes Made

1. Makefile

Added two new targets for Dockerfile formatting:

  • make fmt - Formats all Dockerfiles in the repository (runs dockerfmt -w)
  • make lint - Checks if Dockerfiles are properly formatted (runs dockerfmt --check)

Both targets work recursively, finding all files matching Dockerfile* or *.dockerfile patterns throughout the repository.

2. GitHub Actions Workflow (.github/workflows/lint.yml)

Created a new CI workflow that:

  • Triggers on pushes to main and pull requests
  • Only runs when Dockerfile-related files are modified
  • Installs Go and dockerfmt using github.com/reteps/dockerfmt@latest
  • Checks formatting with make lint
  • Automatically formats Dockerfiles if formatting issues are detected
  • Auto-commits and pushes the changes back to the PR with the message "chore: format Dockerfiles with dockerfmt"

3. Pre-commit Hook Configuration (.pre-commit-config.yaml)

Added optional pre-commit hook configuration that:

  • Automatically formats Dockerfiles before commits
  • Runs dockerfmt -w on modified Dockerfiles
  • Can be enabled locally by running pre-commit install

4. Formatted Existing Dockerfile

Applied dockerfmt formatting to the repository's Dockerfile:

  • Moved && operators to the start of continuation lines (standard shell formatting style)
  • Minimal changes that improve readability while maintaining functionality

5. Documentation

Updated README.md with comprehensive documentation covering:

  • How to install dockerfmt
  • Usage of make fmt and make lint commands
  • Setting up optional pre-commit hooks
  • Information about CI integration and auto-commit behavior

Benefits

  • Consistent formatting across all Dockerfiles in the repository
  • Automated enforcement via CI with auto-commit - no manual intervention needed
  • Developer-friendly with local make targets and optional pre-commit hooks
  • Works with nested Dockerfiles throughout the repository structure
  • Minimal disruption to existing workflows
  • Zero maintenance - formatting issues are automatically fixed by the bot

Testing

All components have been tested locally:

  • make lint correctly detects formatting issues
  • make fmt properly formats Dockerfiles
  • Works with both root-level and nested Dockerfiles
  • CI workflow YAML syntax validated

Usage

For developers:

# Check formatting
make lint

# Format Dockerfiles
make fmt

# Optional: Enable pre-commit hook
pip install pre-commit
pre-commit install

In CI: The workflow runs automatically on PR creation/updates. If Dockerfiles are not formatted, the GitHub Actions bot will automatically format them, commit the changes, and push them back to the PR. No manual intervention required!

Fixes chenrui333/github-action-test#560

Original prompt

This section details on the original issue you should resolve

<issue_title>Introduce a new github action to run dockerfmt and commit the change to the PR when there is a change</issue_title> <issue_description></issue_description>

<agent_instructions>💬 Prompt for Copilot

Add automatic Dockerfile formatting enforcement using dockerfmt.

Goals:

Introduce a consistent Dockerfile style across the repo using dockerfmt.

Add a pre-commit hook or CI step to automatically check formatting.

Ensure local developer workflow runs dockerfmt in format mode (dockerfmt -w) and CI runs it in lint mode (dockerfmt --check).

Acceptance Criteria:

Add dockerfmt as a dev dependency or ensure it’s installed in CI (use go install github.com/moby/dockerfile-linter/cmd/dockerfmt@latest or similar).

Add a make fmt target that runs dockerfmt -w ..

Add a make lint target that runs dockerfmt --check ..

Integrate the lint step in .github/workflows/lint.yml (or equivalent CI file).

Fail the CI build if Dockerfiles are not properly formatted.

Optionally, configure a pre-commit hook (via .pre-commit-config.yaml) to run dockerfmt -w automatically before committing.

References:

https://github.com/moby/buildkit/tree/master/frontend/dockerfile/dockerfmt

Example CI integration:

  • name: Check Dockerfile formatting run: | go install github.com/moby/buildkit/frontend/dockerfile/cmd/dockerfmt@latest dockerfmt --check .

Make sure it works for both root-level and nested Dockerfiles.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes chenrui333/github-action-test#560

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Oct 08 '25 05:10 Copilot

@copilot, can you also auto commit the changes for the PR?

chenrui333 avatar Oct 08 '25 12:10 chenrui333

@copilot, can you also auto commit the changes for the PR?

Added auto-commit functionality in commit 6c08e9a. The workflow now automatically formats Dockerfiles and commits the changes back to the PR when formatting issues are detected. The bot will commit with the message "chore: format Dockerfiles with dockerfmt" and push to the branch.

Copilot avatar Oct 08 '25 12:10 Copilot