GitLab Pipelines
To use DCM with GitLab CI/CD, configure a GitLab pipeline in the pipeline editor,
or create a .gitlab-ci.yml
file and add a step that runs DCM commands.
GitLab uses a YAML file to define pipelines.
stages:
- test
include:
- template: Jobs/Code-Quality.gitlab-ci.yml
variables:
DCM_VERSION: "1.27.1-1" # Specify DCM version (e.g., "1.27.1-1")
dcm:
stage: test
image: dart:stable
before_script:
- apt-get update -qy
- apt-get install -y --no-install-recommends wget gnupg2
script:
- 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' | tee /etc/apt/sources.list.d/dart_stable.list
- apt-get update
- apt-get install dcm=$DCM_VERSION
- dcm run --analyze --unused-code --reporter=gitlab lib > gl-code-quality-report.json
#- dcm run --analyze --unused-code --reporter=console --ci-key="$DCM_CI_KEY" --email="$DCM_EMAIL" lib to # print in console and pass key and email
extends: code_quality
artifacts:
paths: [gl-code-quality-report.json]
-
Image and Stages: We use
dart:latest
as the base image and define a single stage (analyze
) where all jobs will run sequentially. -
Variables: Define any environment variables (
FLUTTER_VERSION
,DCM_VERSION
) that you need for Flutter and DCM versions. -
Before Script: This section installs the necessary packages (
wget
,gnupg2
) required for downloading and setting up Flutter and DCM. -
Install DCM: Adds the DCM repository's GPG key, sets up the repository source for DCM, updates package lists, and installs DCM using the specified version (
DCM_VERSION
). -
DCM Analyze: Runs the
dcm run --analyze --unused-code
command with required parameters (DCM_CI_KEY
andDCM_EMAIL
) to perform static code analysis on thelib
directory. Note if theDCM_CI_KEY
andDCM_EMAIL
are available in an environment variable, they can be ignored by passing as parameters. DCM automatically detects these values from the environment. -
Generate Reporter: you can create an artifacts to report back the code quality in GitLab by setting the report to
--reporter=gitlab
-
Define artifacts: The
artifacts.reports.codequality
usesgl-code-quality-report.json
to generate reports.
The result in the console (GitLab logs) will be:
The result in the PR will be:
Setting Environment Variables in GitLab​
Make sure you add your secrets to the GitLab setting under variables so that you can safely access sensitive information.
Notes​
-
Customization: Adjust
DCM_VERSION
according to your project's requirements. -
Security Considerations: Handle sensitive information (
DCM_CI_KEY
andDCM_EMAIL
) securely using GitLab CI/CD environment variables or GitLab's CI/CD secret management. -
Parallel Jobs: GitLab CI/CD allows parallel jobs within the same stage. If needed, you can split the DCM analysis into multiple jobs for different parts of your repository.
Integrating with DCM Dashboards​
To get an overview of all open issues and enabled code health metrics, and how they change over time, you can upload the results of the dcm run
command to DCM Dashboards.
To get started, check out this guide.