How to use Danger with's CI/CD?

GitLab CI/CD is already set up to run Danger, you have to add this to your .gitlab-ci.yml file:

  - project: 'gitlab-org/quality/pipeline-common'
    file: '/ci/danger-review.yml'

Reference: /ci/danger-review.yml

I’m also overriding the ruby image via:

  image: ruby:3.2.2

My Gemfile references the danger-gitlab gem:

# frozen_string_literal: true

source ''
git_source(:github) { |repo| "{repo}.git" }

ruby '3.2.2'

# ...

gem 'danger-gitlab', '~> 8.0.0'

You should be able to add a Ruby-flavored Dangerfile and it should work:

# frozen_string_literal: true

# ignore inline messages that are outside of the current diff

message "Author @#{gitlab.mr_author}"

# Make it more obvious that a PR is a work in progress and shouldn't be merged yet
warn('This pull request is a Work in Progress and not ready to merge') if gitlab.mr_title.include? 'WIP'

# Large MR
warn('Big MR! Big changes, big things may happen! Check them.') if git.lines_of_code > 100

# Encourage contributors to write useful descriptions
warn('Please provide a Merge Request description') if gitlab.mr_body.length < 5

# If these are all empty something has gone wrong, better to raise it in a comment
if git.modified_files.empty? && git.added_files.empty? && git.deleted_files.empty?
  fail 'This PR has no changes at all, this is likely a developer issue.'

if git.deletions > git.insertions
  message '🗑 Code Cleanup!'

However, in the danger-review stage it cannot resolve the gitlab references:

bundler: failed to load command: danger (/usr/local/bundle/bin/danger)
/usr/local/bundle/gems/danger-9.3.0/lib/danger/danger_core/dangerfile.rb:73:in `method_missing':  (Danger::DSLError)
[!] Invalid `Dangerfile` file: undefined local variable or method `gitlab' for #<Danger::Dangerfile:0x00007f2243d7dcc0 ...

Any ideas?

I got this working upon following Orta Therox suggestion that I review how GitLab’s repo uses Danger.

Apparently, their repo uses their own gitlab-dangerfiles gem which also packages their own default plugins and rules. (I believe I should be able to add my custom rules too.) I did get it to work using that.


# frozen_string_literal: true

require 'gitlab-dangerfiles'

Gitlab::Dangerfiles.for_project(self) do |dangerfiles|
  # Imports all plugins, rules and the default reviewer roulette


# frozen_string_literal: true

source ''
git_source(:github) { |repo| "{repo}.git" }

ruby '3.2.2'

# ...

gem 'gitlab-dangerfiles', require: false

I also created an Access Token with the Developer role and api scope. And added the token to a new DANGER_GITLAB_API_TOKEN CI/CD variable. This allowed the messages to show as comments on the MR.