almenscorner / IntuneCD

Tool to backup, update and document configurations in Intune
MIT License
289 stars 38 forks source link

[FEATURE] Exit code handling #206

Closed AFE88 closed 4 months ago

AFE88 commented 4 months ago

Is your feature request related to a problem? Please describe. When executing the GitLab pipeline, it shows as "successful" even though IntuneCD logs errors when creating objects. This causes confusion and potential issues as the errors are not immediately visible.

Describe the solution you'd like I would like the GitLab pipeline to be marked as "failed" if IntuneCD logs errors. This can be achieved by checking the logfile for errors and exiting with a failure status if any are found. Additionally, setting the appropriate exit codes in the Python scripts that update configurations would help ensure that the pipeline correctly reflects the execution status.

Describe alternatives you've considered The current workaround involves using a Bash script that checks the output of IntuneCD for errors and fails the pipeline if errors are detected. However, this is cumbersome and could be improved by an integrated solution within the pipeline configuration.

Additional context Here is the current workaround being used:

.recover:
  script:
    - source venv/bin/activate
    - |
      function run_recover {
        output=$(mktemp)
        IntuneCD-startupdate -m 1 --exclude DeviceManagementSettings ConditionalAccess Roles WindowsEnrollmentProfile 2>&1 | tee $output
        if grep -q "\[ERROR\]" $output; then
          echo "Error detected in recover, exiting with status 1"
          rm -f $output
          exit 1
        fi
        rm -f $output
      }
      run_recover

Code Explanation:

  1. Activate Virtual Environment: source venv/bin/activate activates the virtual environment where the dependencies for IntuneCD are installed.
  2. Create Temporary File: output=$(mktemp) creates a temporary file to store the output of IntuneCD.
  3. Run IntuneCD and Save Output: IntuneCD-startupdate -m 1 --exclude DeviceManagementSettings ConditionalAccess Roles WindowsEnrollmentProfile 2>&1 | tee $output runs IntuneCD and redirects the output to both the console and the temporary file.
  4. Error Checking: if grep -q "\[ERROR\]" $output; then ... checks if the temporary file contains lines with "[ERROR]".
  5. Set Error Status: If errors are found, it prints an error message and exits the script with status 1 (exit 1), causing the pipeline to be marked as failed.
  6. Clean Up: The temporary file is deleted (rm -f $output) to avoid leaving temporary files behind.

Additionally, many Python scripts used to update configurations contain the following code, which can be modified to set appropriate exit codes:

Original Code:

except Exception as e:
    self.log(
        tag="error",
        msg=f"Error updating {self.config_type} {self.name}: {e}",
    )

Modified Code:

import sys

except Exception as e:
    self.log(
        tag="error",
        msg=f"Error updating {self.config_type} {self.name}: {e}",
    )
    sys.exit(1)  # Set exit code to 1 to indicate an error

This change ensures that the script exits with an error code if an exception occurs, leading the pipeline to correctly reflect the error status.

AFE88 commented 4 months ago

thanks for the update, the feature has been tested successfully