Skip to main content

GitLab

Goal

Integrate codee format with GitLab CI/CD to automatically check if Fortran code is properly formatted and fail the pipeline if any formatting issues are found.

Prerequisites

Ensure you have:

  • codee installed and accessible in your GitLab runners.

GitLab CI/CD pipelines

Codee Formatter on Merge Requests

GitLab CI/CD can be used to run codee format automatically on merge requests and ensure that all contributions follow a consistent formatting style.

Add the following content to your .gitlab-ci.yml file:

stages:
- format-check

format:
stage: format-check
image: ubuntu:latest
before_script:
- apt update && apt install -y git
- git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME --depth=1
script:
- MODIFIED_FILES=$(git diff --name-only --diff-filter=d origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME...HEAD -- '*.f90' '*.F90' '*.f' '*.F')
- if [ -n "$MODIFIED_FILES" ]; then codee format $MODIFIED_FILES; fi
- git diff --quiet || (echo "Formatting issues detected. Run 'codee format' locally before committing." && exit 1)
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"

How it works

  • The job runs whenever a merge request is created 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.

Automatic formatting from the CI/CD pipeline

Warning

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

GitLab CI/CD can also be used to automatically apply codee format to format Fortran code from the contributors. To follow this approach it is necessary to create a personal token on your GitLab instance.

Add the following content to your .gitlab-ci.yml file:

stages:
- format

format:
stage: format
tags:
- self-hosted
before_script:
- apt update && apt install -y git
- git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME --depth=1
script:
- git fetch origin
- git checkout "$CI_COMMIT_REF_NAME"
- |
MODIFIED_FILES=$(git diff --name-only --diff-filter=d origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}...HEAD -- '*.f90' '*.F90' '*.f' '*.F')
echo "Modified files: $MODIFIED_FILES"
if [ -n "$MODIFIED_FILES" ]; then
git codee-format --diff origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} HEAD
if ! git diff --quiet; then
git commit -c user.name="gitlab-ci" -c user.email="ci@example.com" -am "Apply codee format formatting fixes"
git push https://gitlab-ci-token:${GITLAB_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git $CI_COMMIT_REF_NAME;
else
echo "No changes detected."
fi
else
echo "No Fortran files modified."
fi
only:
- merge_requests

How it works

  • The job runs whenever a push is made.
  • It runs codee format on all modified Fortran source files.
  • If codee format makes any changes, the changes are commited and pushed to the current branch.

Troubleshooting

  • If the job does not trigger, verify the rules: section.
  • If formatting does not apply, check if your files match the specified glob patterns (*.f90, *.F90, etc.).