GitLab
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:
codeeinstalled and accessible in your GitLab runners.
GitLab CI/CD pipelines
1. Generate status report
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 --verbose $MODIFIED_FILES | tee codee_output.log; fi
- SUMMARY=$(tail -n 1 codee_output.log)
- echo ""
- echo "============ Codee Format Summary ============"
- echo "$SUMMARY"
- echo "=============================================="
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
allow_failure: false
How it works
- The job runs whenever a merge request is created or updated.
- It runs
codee formaton all modified Fortran source files. - It shows all the changes that could be made by
codee formatgenerating a report. Note that the pipeline is never blocked.
2. Verify format
GitLab CI/CD can be used to run codee format automatically on merge requests
and generate a report with the potential fixes that can be applied to the
modified Fortran files.
Add the following content to your .gitlab-ci.yml file:
stages:
- codee-status
codee_format_status:
stage: codee-status
tags:
- self-hosted
only:
- merge_requests
before_script:
- apt update && apt install -y git cmake ninja-build gfortran
- git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME --depth=1
- git fetch origin
- git checkout "$CI_COMMIT_REF_NAME"
script:
- |
echo "Detecting modified Fortran files..."
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
echo "Running Codee format in verbose mode for status report..."
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
else
echo "No modified Fortran files. Skipping Codee format status."
fi
allow_failure: false
How it works
- The job runs whenever a merge request is created or updated.
- It runs
codee formaton all modified Fortran source files. - It shows all the changes that could be made by
codee formatgenerating a report. Note that the pipeline is never blocked.
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
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 formaton all modified Fortran source files. - If
codee formatmakes 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.).