Codemagic Workflow
First, make sure you app is added and enabled in Codemagic CI/CD by integrating with your repository.
Then, you have two options to write your steps for the build pipeline in Codemagic, either use the "Workflow Editor" or you can switch to codemagic.yaml
file.
|---lib
|---analysis_options.yaml
|---pubspec.yaml
|---codemagic.yaml
In this doc, we will cover both modes.
Configuring Access to DCM Step​
First, you need to define environment variables, DCM_EMAIL
and DCM_KEY
.
Setup Environment Variables​
- codemagic.yaml
- Workflow Editor
In Codemagic, you can navigate to your app settings, then to the Environment variables tab and then add both variables. Then, group the variables under dcm_credentials
and make sure the Secure
option is enabled.
environment:
groups:
- dcm_credentials
In this mode, you can go to "Environment Variable" step and define your secure variables.
Installing DCM Step​
Codemagic can have different build machine including macOS, Linux and Windows.
To install the the DCM on Codemagic, depending on the build machine that you are using, follow the installation step.
For example, for macOS build machines,
you can use brew
as below right before all other steps. Ensure that you also install
flutter
pub packages.
- codemagic.yaml
- Workflow Editor
scripts:
- name: Install DCM
script: |
brew tap CQLabs/dcm
export HOMEBREW_NO_AUTO_UPDATE=1
brew install dcm
- name: Install flutter packages
script: flutter pub get
- name: Confirm DCM
script: dcm --version
#!/bin/sh
set -e
set -x
brew tap CQLabs/dcm
export HOMEBREW_NO_AUTO_UPDATE=1
brew install dcm
flutter pub get
dcm --version
You can add this script between "dependencies cache" and "tests" steps.
Read more about different DCM installation on Windows, macOS and Linux.
Run DCM Step​
It's up to you when to run DCM analyze command to generate the code quality report whether pre-test
or post-test
or pre-build
, however, we recommend you run and generate report as early as possible as it is good idea to run quick steps before slower steps to save up CI/CD minutes and get faster feedback on CI. Therefore, we will add "Run DCM Analysis" right after all dependencies are set and before every other steps.
- codemagic.yaml
- Workflow Editor
- name: Running DCM Analysis...
script: dcm analyze --ci-key=$DCM_KEY --email=$DCM_EMAIL lib --reporter=console
You can use the following script in the pre-test
step. The earlier you have the analyze in the CI steps, the better, it helps you to fail the steps without going through the entire CIÂ workflow steps.
#!/bin/sh
set -e
echo "Running DCM Analysis..."
dcm analyze --ci-key=$DCM_KEY --email=$DCM_EMAIL lib --reporter=console
After this setup and combing with your own steps, you are ready to start building and testing.
DCM Report Artifacts​
In case you need to download the DCM report from Codemagic, you can generate preferable format for example Markdown, JSON or XML and download if needed.
- codemagic.yaml
- Workflow Editor
You can do that with codemagic.yaml
as follow:
- name: Running DCM Analysis
script: |
echo "Running DCM Analysis..."
# Run DCM analysis, capture exit code and error message
DCM_OUTPUT=$(dcm analyze --ci-key=$DCM_KEY --email=$DCM_EMAIL lib --no-congratulate --reporter=console 2>&1)
DCM_EXIT_CODE=$?
# If analysis fails, create an error summary
if [ "$DCM_EXIT_CODE" -ne 0 ]; then
echo "DCM Analysis failed with exit code $DCM_EXIT_CODE."
echo "Error Message: $DCM_OUTPUT"
# Write error summary to a markdown file
cat <<EOF > dcm_error_summary.md
# DCM Analysis Failed
DCM Analysis failed with exit code $DCM_EXIT_CODE.
## Error Message
\`\`\`
$DCM_OUTPUT
\`\`\`
EOF
# Copy the error summary to the export directory
cp dcm_error_summary.md "$CM_EXPORT_DIR/dcm_error_summary.md"
# Exit the script with the DCM exit code to mark the CI/CD step as failed
exit $DCM_EXIT_CODE
fi
# Optionally add test scripts here if needed, for example:
- name: Run Flutter Tests
script: flutter test
artifacts:
- dcm_error_summary.md # Collect any error summary if generated
You can change your pre-test
script to the following:
#!/bin/sh
set -e
echo "Running DCM Analysis..."
# Run DCM analysis, capture exit code and error message
DCM_OUTPUT=$(dcm analyze --ci-key=$DCM_KEY --email=$DCM_EMAIL lib --no-congratulate --reporter=console 2>&1)
DCM_EXIT_CODE=$?
# If analysis fails, create an error summary
if [ "$DCM_EXIT_CODE" -ne 0 ]; then
echo "DCM Analysis failed with exit code $DCM_EXIT_CODE."
echo "Error Message: $DCM_OUTPUT"
# Write error summary to a markdown file
cat <<EOF > dcm_error_summary.md
# DCM Analysis Failed
DCM Analysis failed with exit code $DCM_EXIT_CODE.
## Error Message
\`\`\`
$DCM_OUTPUT
\`\`\`
EOF
# Copy the error summary to the export directory
cp dcm_error_summary.md "$CM_EXPORT_DIR/dcm_error_summary.md"
# Exit the script with the DCM exit code to mark the CI/CD step as failed
exit $DCM_EXIT_CODE
fi
Full Script​
Here is the final steps and script for codemagic.yaml
file.
workflows:
flutter-workflow:
name: Flutter Code Quality
triggering:
events:
- push
environment:
flutter: stable
groups:
- dcm_credentials
scripts:
- name: Install DCM
script: |
brew tap CQLabs/dcm
export HOMEBREW_NO_AUTO_UPDATE=1
brew install dcm
- name: Flutter packages
script: |
flutter pub get
- name: DCM verify
script: |
dcm --version
- name: Running DCM Analysis
script: >
echo "Running DCM Analysis..."
# Run DCM analysis, capture exit code and error message
DCM_OUTPUT=$(dcm analyze --ci-key=$DCM_KEY --email=$DCM_EMAIL lib --no-congratulate
--reporter=console 2>&1)
DCM_EXIT_CODE=$?
# If analysis fails, create an error summary
if [ "$DCM_EXIT_CODE" -ne 0 ]; then
echo "DCM Analysis failed with exit code $DCM_EXIT_CODE."
echo "Error Message: $DCM_OUTPUT"
# Write error summary to a markdown file
cat <<EOF > dcm_error_summary.md
# DCM Analysis Failed
DCM Analysis failed with exit code $DCM_EXIT_CODE.
## Error Message
\`\`\`
$DCM_OUTPUT
\`\`\`
EOF
# Copy the error summary to the export directory
cp dcm_error_summary.md "$CM_EXPORT_DIR/dcm_error_summary.md"
# Exit the script with the DCM exit code to mark the CI/CD step as failed
exit $DCM_EXIT_CODE
fi
- name: Run Flutter Tests
script: flutter test
artifacts:
- dcm_error_summary.md
- Executes
dcm analyze
and captures both the exit code and error message. - If the analysis fails (
DCM_EXIT_CODE
is non-zero), the script:- Outputs the error to the console.
- Writes the error details to
dcm_error_summary.md
in Markdown format.
- The
dcm_error_summary.md
file is added to the artifacts section, so Codemagic will include it in the build artifacts if it’s generated. The key step here is to copy the generated file into$CM_EXPORT_DIR
.