Commit 6c4a30ff authored by Davide Ricci's avatar Davide Ricci
Browse files

Update .gitlab-ci.yml file

parent 0970da3b
Loading
Loading
Loading
Loading
Loading
+6 −90
Original line number Diff line number Diff line
# software-di-controllo/.gitlab-ci.yml

image: python:3.12

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  LINT_FORMAT_TARGETS: "noctua" # Directories/packages to format and lint
  PYLINT_TARGETS: "noctua"      # Usually just the main package for pylint
  # For auto-committing
  LINT_FORMAT_TARGETS: "noctua"
  PYLINT_TARGETS: "noctua"
  GIT_USER_EMAIL: "davide.ricci@inaf.it" 
  GIT_USER_NAME: "Davide GitLab CI"
  FORMATTED_BRANCH_NAME: "ci-auto-formatted" # Branch for auto-committed changes
@@ -29,9 +27,7 @@ prepare_and_install:
  script:
    - echo "Python version $(python -V)"
    - pip install --upgrade pip
    # Install all necessary tools for subsequent stages
    - pip install isort autopep8 pylint black # Added black for completeness
    # Install the project in editable mode for context
    - pip install isort autopep8 pylint
    - echo "Installing project 'noctua' and its dependencies..."
    - pip install -e .
    - echo "Setup complete."
@@ -55,15 +51,14 @@ apply_trailing_whitespace_fix:
    - echo "Trailing whitespaces removed."
  artifacts:
    paths:
      - noctua/ # Pass only modified package/test dirs
    expire_in: 1 hour
      - noctua/ 

apply_autopep8_format:
  stage: format_and_lint
  tags:
    - git-run-ia2
  needs:
    - job: apply_trailing_whitespace_fix # Depends on the previous formatting step
    - job: apply_trailing_whitespace_fix
      artifacts: true
  script:
    - echo "Applying autopep8 formatting to ${LINT_FORMAT_TARGETS}..."
@@ -72,7 +67,6 @@ apply_autopep8_format:
  artifacts:
    paths:
      - noctua/
    expire_in: 1 hour

apply_isort_format:
  stage: format_and_lint
@@ -88,14 +82,13 @@ apply_isort_format:
  artifacts:
    paths:
      - noctua/
    expire_in: 1 hour

run_pylint_check:
  stage: format_and_lint
  tags:
    - git-run-ia2
  needs:
    - job: apply_isort_format # Depends on all formatting being done
    - job: apply_isort_format
      artifacts: true
  script:
    - echo "Running pylint code quality check on '${PYLINT_TARGETS}'..."
@@ -103,80 +96,3 @@ run_pylint_check:
    # The `pip install -e .` in prepare_and_install ensures pylint can find 'noctua'
    - pylint --rcfile=.pylintrc ${PYLINT_TARGETS}
    - echo "Pylint check complete."
  # No artifacts needed from pylint if it just passes/fails the job

# --- Auto Commit Job ---

commit_formatted_changes:
  stage: auto_commit_if_changed
  tags:
    - git-run-ia2
  needs:
    # This job needs the final state of the code after all formatting.
    # It should depend on the last formatting job that produces artifacts.
    # Pylint is a check, so we depend on the last *modifying* job (isort).
    - job: apply_isort_format # Artifacts from the last formatter
      artifacts: true
    # Optionally, also depend on pylint succeeding, though pylint itself doesn't change files.
    # If pylint fails, you might not want to commit anything.
    - job: run_pylint_check # No artifacts, just ensures pylint passed
  before_script:
    # Git and SSH setup (same as before, ensure SSH_DEPLOY_KEY variable is set)
    - apt-get update -y && apt-get install -y openssh-client git
    - eval $(ssh-agent -s)
    - mkdir -p ~/.ssh && chmod 700 ~/.ssh
    - echo "$SSH_DEPLOY_KEY" | tr -d '\r' > ~/.ssh/id_ci_deploy && chmod 600 ~/.ssh/id_ci_deploy
    - ssh-add ~/.ssh/id_ci_deploy
    - ssh-keyscan -p ${CI_SERVER_PORT:-22} $CI_SERVER_HOST >> ~/.ssh/known_hosts && chmod 644 ~/.ssh/known_hosts
    - git config --global user.email "${GIT_USER_EMAIL}"
    - git config --global user.name "${GIT_USER_NAME}"
    # We are working with artifacts, so the current state of files is what we got.
    # We need to re-initialize git context if not already a git repo, or ensure we are on the right commit.
    # GitLab runners typically checkout the commit that triggered the pipeline.
    # The artifacts overlay these checked-out files.
    - git checkout "$CI_COMMIT_BRANCH" # Ensure we are on the correct branch
    - git pull origin "$CI_COMMIT_BRANCH" --ff-only # Ensure local is up-to-date; artifacts will then apply on top
                                                # This pull might overwrite artifact changes if not careful.
                                                # It's often better if the commit job *also* runs the formatters
                                                # on a fresh checkout to be certain.

    # Alternative: Re-run formatters in this job on a fresh checkout to be absolutely sure.
    # - pip install isort autopep8 # if not already available from a base image or previous step
    # - git checkout "$CI_COMMIT_BRANCH" && git pull origin "$CI_COMMIT_BRANCH"
    # - find ${LINT_FORMAT_TARGETS} -type f -name "*.py" -exec sed -i 's/[[:space:]]*$//' {} \;
    # - autopep8 --in-place --recursive --aggressive --aggressive ${LINT_FORMAT_TARGETS}
    # - isort ${LINT_FORMAT_TARGETS}
  script:
    - echo "Checking for formatting changes to commit..."
    - |
      # Add all potentially modified files (safer to be specific with LINT_FORMAT_TARGETS)
      # `git add .` can be too broad if other files were unintentionally changed.
      git add ${LINT_FORMAT_TARGETS}

      if ! git diff --cached --quiet; then # Check staged changes
        echo "Code formatting changes detected. Committing to new branch '${FORMATTED_BRANCH_NAME}'."
        # Create the new branch from the current commit (CI_COMMIT_SHA)
        git checkout -B "${FORMATTED_BRANCH_NAME}" "${CI_COMMIT_SHA}"
        # Re-apply the staged changes (which are the formatting changes)
        # This sequence is a bit tricky. A cleaner way:
        # 1. In this job, on a fresh checkout of CI_COMMIT_BRANCH:
        # 2. Run all formatters.
        # 3. Then check diff, create new branch, commit, push.

        # Simplified (assuming artifacts correctly represent the final formatted state on top of CI_COMMIT_SHA files):
        # The `git add LINT_FORMAT_TARGETS` above should have staged the artifact changes.
        git commit -m "ci: Apply automated code formatting [skip ci]"
        echo "Pushing changes to branch '${FORMATTED_BRANCH_NAME}'..."
        # The runner needs push permissions via SSH_DEPLOY_KEY
        git push -u origin "${FORMATTED_BRANCH_NAME}" -o ci.skip
        echo "Changes pushed to branch '${FORMATTED_BRANCH_NAME}'."
        echo "Please review and merge '${FORMATTED_BRANCH_NAME}' into '${CI_COMMIT_BRANCH}'."
      else
        echo "No formatting changes to commit."
      fi
  rules:
    # This job should only run if the previous linting/formatting jobs succeed
    # And typically made manual or on a specific trigger to avoid too many auto-commits.
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH # Example: only on default branch
      when: manual # Make it a manual action for safety
      allow_failure: false # If it runs, it should succeed or clearly fail