Commit b6fb5f7c authored by Davide Ricci's avatar Davide Ricci
Browse files

Update .gitlab-ci.yml file

parent d8034133
Loading
Loading
Loading
Loading
Loading
+122 −65
Original line number Diff line number Diff line
@@ -5,61 +5,131 @@ image: python:3.12
variables:
  PROJECT: "noctua"
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
  FORMAT_TARGETS: "${PROJECT}"
  PYLINT_TARGETS: "${PROJECT}"
  FORMAT_TARGETS: "${PROJECT}" # For sed, autopep8, isort
  PYLINT_TARGETS: "${PROJECT}" # For pylint
  GIT_USER_EMAIL: "davide.ricci@inaf.it"
  GIT_USER_NAME: "Davide GitLab CI"
  FORMATTED_BRANCH_NAME: "validated"

cache:
  key: "$CI_COMMIT_REF_SLUG"
  key: "$CI_COMMIT_REF_SLUG" # Cache pip downloads per branch
  paths:
    - .cache/pip
    # - venv/

stages:
  - validate  # Combined setup, formatting, and linting
  - deploy
  - deploy_docs
  - setup_and_format 
  - lint # Does not modify code
  - auto_commit

# Job 1: Install dependencies, format code, and lint
# This single job is more efficient than the previous multi-job chain.
validate_code:
  stage: validate
# Job 1: Install ${PROJECT} and its dependencies
install_project:
  stage: setup_and_format 
  tags:
    - git-run-ia2 
  before_script:
  script:
    - echo "Current directory $(pwd)"
    - ls -la
    - echo "Python version $(python -v)"
    - pip install --upgrade pip
    # Install all tools at once
    # - pip install ".[dev]" # Assuming you have a pyproject.toml with dev dependencies like isort, autopep8, pylint
    # # Or install them manually:
    - echo "Installing project '${PROJECT}' and its dependencies..."
    - pip install -e .
    - pip install isort autopep8 pylint
    - echo "${PROJECT} installation complete."
  artifacts:
    paths:
      # Pass the entire workspace. This includes the checked-out code
      # or if -e . modifies local files (e.g. .egg-info).
      - . # The current working directory state

# Job 2: Remove Trailing Whitespaces
fix_trailing_whitespaces:
  stage: setup_and_format
  tags:
    - git-run-ia2
  needs:
    - job: install_project
      artifacts: true
  script:
    # --- STEP 1: Format Code ---
    - echo "Removing trailing whitespaces..."
    - echo "Current directory $(pwd)"
    - ls -la
    - echo "Removing trailing whitespaces from '${FORMAT_TARGETS}' directory..."
    - find "${FORMAT_TARGETS}" -type f -name "*.py" -exec sed -i 's/[[:space:]]*$//' {} \;
    - echo "Trailing whitespaces removed."
  artifacts:
    paths:
      - . # Pass the modified workspace

    - echo "Applying isort..."
# Job 3: Apply isort
apply_isort:
  stage: setup_and_format
  tags:
    - git-run-ia2
  needs:
    - job: fix_trailing_whitespaces
      artifacts: true
  before_script:
    - pip install isort
  script:
    - echo "Current directory $(pwd)"
    - ls -la
    - echo "Applying isort to '${FORMAT_TARGETS}' directory..."
    # isort will use pyproject.toml for configuration
    - isort "${FORMAT_TARGETS}"
    - echo "isort formatting complete."
  artifacts:
    paths:
      - . # Pass the modified workspace

    - echo "Applying autopep8..."
# Job 4: Apply autopep8
apply_autopep8:
  stage: setup_and_format
  tags:
    - git-run-ia2
  needs:
    - job: apply_isort
      artifacts: true
  before_script:
    - pip install autopep8
  script:
    - echo "Current directory $(pwd)"
    - ls -la
    - echo "Applying autopep8 to '${FORMAT_TARGETS}' directory..."
    - autopep8 --in-place --recursive --aggressive --aggressive "${FORMAT_TARGETS}"
    - echo "Formatting complete."

    # --- STEP 2: Lint the formatted code ---
    #- echo "Running pylint..."
    #- pylint --rcfile=.pylintrc --fail-on=E,F ${PYLINT_TARGETS}/sequencer.py
    #- echo "Pylint check complete."

    - echo "autopep8 formatting complete."
  artifacts:
    # Pass the formatted code to the commit job
    paths:
      - "${PROJECT}/"
      - ".pylintrc" # And any other relevant files
      - . # Pass the modified workspace

# Job 5: Run Pylint
run_pylint:
  stage: lint
  tags:
    - git-run-ia2
  needs:
    - job: apply_autopep8 
      artifacts: true
  before_script:
    - pip install pylint
  script:
    - echo "Current directory $(pwd)"
    - echo "Running pylint on '${PYLINT_TARGETS}'..."
    - pylint --version     
    - pylint --rcfile=.pylintrc --fail-on=E,F ${PYLINT_TARGETS}/sequencer.py || echo "Pylint finished with exit code $(( $? &2)) (non-zero indicates issues)"
    - echo "Pylint check complete."
  #rules:
  #  - if: $CI_PIPELINE_SOURCE == "merge_request_event"
  #  - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  #  - when: manual
  #    allow_failure: true # Allow manual trigger to not fail overall pipeline if desired



# Job 2: Create and deploy documentation to GitLab Pages
# Job 6: Create documentation
# This defines a job named 'pages'. GitLab recognizes this special name
# and knows that its purpose is to deploy a website to GitLab Pages.
pages:
  stage: deploy
  stage: deploy_docs
  tags:
    - git-run-ia2
  before_script:
@@ -80,43 +150,30 @@ pages:
    # Only run on the default branch
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  
# Job 3: Commit the formatted code to a new branch
# Job 7: commit in branch
commit_formatted_code:
  stage: auto_commit
  tags:
    - git-run-ia2
  needs:
    - job: validate_code # Depends on the single validation job
    - job: run_pylint # Must pass pylint
    - job: apply_autopep8 # Needs the final code state from formatting
      artifacts: true
  before_script:
    - git config --global user.email "${GIT_USER_EMAIL}"
    - git config --global user.name "${GIT_USER_NAME}"
    - git config --global user.email "davide.ricci@inaf.it"
    - git config --global user.name "CI Bot"
    - git checkout "$CI_COMMIT_SHA"  
  script:
    - echo "Creating/updating branch '${FORMATTED_BRANCH_NAME}' with formatted code..."
    # Create the new branch from the original commit
    - git checkout -B "${FORMATTED_BRANCH_NAME}"
    
    # Add all the changes from the formatted code (which came from artifacts)
    - echo "Checking for changes and committing to ${FORMATTED_BRANCH_NAME} branch..."
    - git add .
    
    # Check if there's anything to commit
    - echo "Creating/updating branch '${FORMATTED_BRANCH_NAME}' based on commit $CI_COMMIT_SHA..."
    - git checkout -B "${FORMATTED_BRANCH_NAME}" "$CI_COMMIT_SHA"
    - MAIN_COMMIT_MSG=$(git log -1 --pretty=%B $CI_COMMIT_SHA)
    - | 
      if git diff --staged --quiet; then
        echo "No formatting changes to commit."
      else
        echo "Committing formatting changes..."
        MAIN_COMMIT_MSG=$(git log -1 --pretty=%B $CI_COMMIT_SHA)
        git commit -m "style: Apply automatic formatting

        ${MAIN_COMMIT_MSG}
        ---
        CI Job: $CI_JOB_ID
        Source Commit: $CI_COMMIT_SHA
        [skip ci]"
        
        # Push the new branch to the repository
        git push --force "https://gitlab-ci-token:${GITLAB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" "${FORMATTED_BRANCH_NAME}"
      fi
  rules:
    # Only run this when pushing to a non-default branch, to avoid loops.
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      git commit -m "$MAIN_COMMIT_MSG
        - Source Commit: $CI_COMMIT_SHA
        - Date: $(date +"%Y-%m-%d %H:%M:%S")
        - Job ID: $CI_JOB_ID
        - Pipeline ID: $CI_PIPELINE_ID
        - [skip ci]"
    - git push --force https://davide.ricci:$GITLAB_TOKEN@www.ict.inaf.it/gitlab/davide.ricci/software-di-controllo.git ${FORMATTED_BRANCH_NAME}