Azure DevOps Pipelines
To use DCM with Azure DevOps CI/CD, create an azure-pipelines.yml
file in the root of your application (or use the Azure DevOps pipeline setup in the dashboard) and add a step that runs DCM commands.
Here’s how you can set up a pipeline configuration for Azure DevOps:
trigger:
branches:
exclude:
- '*'
pr:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
jobs:
- job: Code_Quality
displayName: 'Code Quality'
steps:
- script: |
set -e # Exit on any command failure
# Install Flutter
echo "Installing Flutter..."
sudo apt-get update
sudo apt-get install -y git curl
git clone https://github.com/flutter/flutter.git -b stable --depth 1
export PATH="$PATH:`pwd`/flutter/bin"
flutter doctor
flutter pub get
# Install DCM
echo "Installing DCM..."
sudo apt-get install -y --no-install-recommends wget gnupg2
wget -qO- https://dcm.dev/pgp-key.public | gpg --dearmor -o /usr/share/keyrings/dcm.gpg
echo 'deb [signed-by=/usr/share/keyrings/dcm.gpg arch=amd64] https://dcm.dev/debian stable main' | sudo tee /etc/apt/sources.list.d/dart_stable.list
sudo apt-get update
sudo apt-get install -y dcm=1.27.1-1 # Adjust DCM version as needed
# Verify DCM installation
echo "Verifying DCM installation..."
dcm --version
# Run DCM Analysis
echo "Running DCM Analysis..."
set +e # Do not exit on error
dcm run --analyze --unused-code --reporter=checkstyle lib > checkstyle-result.xml
DCM_EXIT_CODE=$?
DCM_ERROR_MESSAGE=$(dcm run --analyze --unused-code lib 2>&1)
echo "DCM_EXIT_CODE=$DCM_EXIT_CODE" > dcm_exit_code.txt
echo "DCM_ERROR_MESSAGE=\"$DCM_ERROR_MESSAGE\"" > dcm_error_message.txt
set -e # Exit on any command failure
# Write error message to Markdown file
if [ $DCM_EXIT_CODE -ne 0 ]; then
echo "# DCM Analysis Failed" > $(System.DefaultWorkingDirectory)/dcm_error_summary.md
echo "DCM Analysis failed with exit code $DCM_EXIT_CODE." >> $(System.DefaultWorkingDirectory)/dcm_error_summary.md
echo "## Error Message" >> $(System.DefaultWorkingDirectory)/dcm_error_summary.md
echo "\`\`\`" >> $(System.DefaultWorkingDirectory)/dcm_error_summary.md
echo "$DCM_ERROR_MESSAGE" >> $(System.DefaultWorkingDirectory)/dcm_error_summary.md
echo "\`\`\`" >> $(System.DefaultWorkingDirectory)/dcm_error_summary.md
fi
displayName: 'Install Flutter, Install DCM, and Run DCM Analysis'
env:
DCM_CI_KEY: $(DCM_CI_KEY)
DCM_EMAIL: $(DCM_EMAIL)
- script: |
if [ -f checkstyle-result.xml ]; then
echo "checkstyle-result.xml exists, proceeding to publish."
else
echo "checkstyle-result.xml does not exist, skipping publish step."
exit 1
fi
displayName: 'Check for Checkstyle Result'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: 'checkstyle-result.xml'
ArtifactName: 'CheckstyleReports'
publishLocation: 'Container'
condition: succeededOrFailed()
displayName: 'Publish Checkstyle Report'
- script: |
source dcm_exit_code.txt
source dcm_error_message.txt
if [ "$DCM_EXIT_CODE" -ne 0 ]; then
echo "DCM Analysis failed with exit code $DCM_EXIT_CODE. Error message: $DCM_ERROR_MESSAGE"
##vso[task.uploadsummary]$(System.DefaultWorkingDirectory)/dcm_error_summary.md
##vso[task.logdetail id=dcm_log;name=DCM Analysis Log;type=Build;order=1;state=Completed]DCM Analysis log details
##vso[task.addattachment type=Distributedtask.Core.Summary;name=DCM Analysis Summary;]$(System.DefaultWorkingDirectory)/dcm_error_summary.md
exit $DCM_EXIT_CODE
fi
displayName: 'Fail Build if DCM Analysis Failed'
condition: always()
Explanation
-
Trigger Configuration:
-
The pipeline triggers on pull requests (PRs) targeting the
main
branch. -
Excludes all other branches from triggering the pipeline.
-
Pool Configuration:
- Uses the
ubuntu-latest
virtual machine image for running the pipeline.
- Uses the
-
Job: Code_Quality:
- Display Name: 'Code Quality'
- Steps:
- Install Flutter:
- Updates the package list.
- Clones the Flutter repository.
- Adds Flutter to the PATH.
- Runs
flutter doctor
to verify the installation. - Runs
flutter pub get
to fetch Dart packages.
- Install DCM:
- Installs necessary dependencies (
wget
,gnupg2
). - Downloads and installs DCM's GPG key.
- Adds the DCM repository configuration.
- Updates the package list.
- Installs DCM (version
1.27.1-1
).
- Installs necessary dependencies (
- Verify DCM Installation:
- Checks the installed version of DCM to ensure it's correctly installed.
- Run DCM Analysis:
- Runs the DCM analysis and captures the exit code and error message.
- Logs a message if DCM exits with a non-zero code.
- Writes the error message to a Markdown file if the DCM analysis fails.
- Check for Checkstyle Result:
- Verifies if the
checkstyle-result.xml
file exists before attempting to publish it. - Skips the publish step if the file does not exist.
- Verifies if the
- Publish Checkstyle Report:
- Publishes the
checkstyle-result.xml
file as a build artifact namedCheckstyleReports
. - Ensures the artifact is published regardless of the previous job's success or failure.
- Publishes the
- Fail Build if DCM Analysis Failed:
- Sources the captured exit code and error message.
- Logs the failure and error message.
- Uploads the Markdown summary to the build summary.
- Logs detailed information and attaches the summary file.
- Fails the build with the captured error message.
- Install Flutter:
Azure DevOps Jobs
Azure DevOps Report in Logs
Setup Branch Policy
In AzureDevOps, you need to make sure that you have a build policy for each branch that is considered the main which all PRs are going to merge.
Check the status of PRs
In each PR, you can see the analysis status that runs via Pipeline.
Setting Environment Variables in Azure DevOps
Ensure that DCM_CI_KEY
and DCM_EMAIL
are set as environment variables in your Azure DevOps project:
- Navigate to your Azure DevOps project.
- Go to the pipelines, select your pipeline and Edit.
- Then in Edit mode, select Variables and add DCM secrets.
- Add
DCM_CI_KEY
andDCM_EMAIL
with their respective values.