GitHub
Integrate codee format
with GitHub to automatically check if Fortran code is
properly formatted and fail the workflow if any formatting issues are found.
Prerequisites
Ensure you have:
codee
installed and accessible in your GitHub runners.
Create a GitHub Actions workflow
GitHub Actions can be used to run codee format
automatically on pull requests
and ensure that all contributions follow a consistent formatting style.
In your repository, create the following directory structure if it does not exist:
mkdir -p .github/workflows/
Then, create a new workflow file:
touch .github/workflows/check-formatting.yml
Define the GitHub actions workflow
1. Generate status report
Add the following content to .github/workflows/check-formatting.yml
:
name: Codee Formatter Status Report
on:
pull_request:
jobs:
format:
name: Codee Formatter Status Report
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get modified Fortran files
id: modified-files
run: |
MODIFIED_FILES=$(git diff --name-only --diff-filter=d origin/${{ github.base_ref }}...HEAD -- '*.f90' '*.F90' '*.f' '*.F')
echo "MODIFIED_FILES=$MODIFIED_FILES" >> $GITHUB_ENV
- name: Run Codee format status
if: env.MODIFIED_FILES != ''
run: |
for f in $MODIFIED_FILES; do
echo "Formatting $f..."
codee format --verbose "$f"
done
echo "Summary of formatting status:"
for f in $MODIFIED_FILES; do
codee format --verbose "$f" 2>&1 | grep -E "diagnostics|Formatted"
done
How it works
- The workflow runs whenever a pull request is opened or updated.
- It runs
codee format
on all modified Fortran source files. - It shows all the changes that could be made by
codee format
generating a report. Note that the pipeline is never blocked.
2. Verify format
Add the following content to .github/workflows/check-formatting.yml
:
name: Check Fortran Formatting
on:
pull_request:
jobs:
format:
name: Check formatting
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get modified Fortran files
id: modified-files
run: |
MODIFIED_FILES=$(git diff --name-only --diff-filter=d origin/${{ github.base_ref }}...HEAD -- '*.f90' '*.F90' '*.f' '*.F')
echo "MODIFIED_FILES=$MODIFIED_FILES" >> $GITHUB_ENV
- name: Run `codee format` on modified files
if: env.MODIFIED_FILES != ''
run: codee format $MODIFIED_FILES
- name: Fail if there are uncommitted changes
run: git diff --quiet || (echo "Formatting issues detected. Run 'codee format' locally before committing." && exit 1)
How it works
- The workflow runs whenever a pull request is opened or updated.
- It runs
codee format
on all modified Fortran source files. - If
codee format
makes any changes, the workflow fails, requiring contributors to fix formatting before merging.
3. Apply format
Be cautious when following this guide as there can always be issues in formatting that could cause problems in your code. Always review before merging
GitHub actions can also be used to automatically use codee format
to format
Fortran code from the contributors.
Add the following content to .github/workflows/format-code.yml
:
name: Codee format changes
on:
pull_request:
permissions:
contents: write
pull-requests: write
jobs:
format:
name: Check formatting
runs-on: self-hosted
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: Get modified Fortran files
id: modified-files
run: |
MODIFIED_FILES=$(git diff --name-only --diff-filter=d origin/${{ github.base_ref }}...HEAD -- '*.f90' '*.F90' '*.f' '*.F')
echo "MODIFIED_FILES=$MODIFIED_FILES" >> $GITHUB_ENV
- name: Run `codee format` on modified files
if: env.MODIFIED_FILES != ''
run: git codee-format --diff ${{ github.base_ref }} HEAD
- name: Commit changes
if: env.MODIFIED_FILES != ''
run: |
if ! git diff --quiet; then
# Push with the github-actions user. More information at:
# https://github.com/actions/checkout?tab=readme-ov-file#push-a-commit-using-the-built-in-token
git commit -c user.name="github-actions[bot]" -c user.email="41898282+github-actions[bot]@users.noreply.github.com" -am "Apply codee format formatting fixes"
git push --set-upstream origin ${{ github.head_ref }}
else
echo "No changes detected."
exit 0
fi
How it works
- The workflow runs whenever a pull request is opened or updated.
- It runs
codee format
on all modified Fortran source files. - If
codee format
makes any changes, it commits those changes and pushes them to the current branch for review.
Troubleshooting
- If the action does not trigger, verify the
on:
section. - If formatting does not apply, check if your files match the specified glob
patterns (
*.f90
,*.F90
, etc.).