Skip to content

Commit

Permalink
Skip rails-ci pipeline if commit message contains skip text
Browse files Browse the repository at this point in the history
  • Loading branch information
zzak committed May 9, 2024
1 parent 004cfba commit 5c8973d
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/buildkite/config/build_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def nightly?
ENV.has_key?("RAILS_CI_NIGHTLY")
end

def skip?
# [ci skip], [skip ci], [ci-skip], or [skip-ci]
[ENV["BUILDKITE_MESSAGE"]].grep(/(ci skip|skip ci|ci-skip|skip-ci)/i).any?
end

def rails_root
Pathname.pwd
end
Expand Down
1 change: 1 addition & 0 deletions pipelines/rails-ci/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ steps:
-e BUILDKITE_AGENT_META_DATA_QUEUE
-e BUILDKITE_BRANCH
-e BUILDKITE_BUILD_ID
-e BUILDKITE_MESSAGE
-e BUILDKITE_PULL_REQUEST
-e BUILDKITE_PULL_REQUEST_BASE_BRANCH
-e BUILDKITE_REBUILT_FROM_BUILD_ID
Expand Down
9 changes: 9 additions & 0 deletions pipelines/rails-ci/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
plugin :docker_compose, "docker-compose#v4.16.0"
plugin :artifacts, "artifacts#v1.9.3"

if build_context.skip?
command do
label "skip"
command "echo 'Build skipped'"
end

next
end

if build_context.nightly?
build_context.rubies << Buildkite::Config::RubyConfig.master_ruby
build_context.rubies << Buildkite::Config::RubyConfig.yjit_ruby
Expand Down
70 changes: 70 additions & 0 deletions test/buildkite_config/test_build_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,76 @@ def test_ci_env_ci
ENV["CI"] = @before_env_ci
end

def test_nightly
@before_env_nightly = ENV["RAILS_CI_NIGHTLY"]
ENV["RAILS_CI_NIGHTLY"] = "true"

sub = create_build_context
assert_predicate sub, :nightly?
ensure
ENV["RAILS_CI_NIGHTLY"] = @before_env_nightly
end

def test_nightly_false
@before_env_nightly = ENV["RAILS_CI_NIGHTLY"]
ENV["RAILS_CI_NIGHTLY"] = nil

sub = create_build_context
assert_not_predicate sub, :nightly?
ensure
ENV["RAILS_CI_NIGHTLY"] = @before_env_nightly
end

def test_skip_ci_skip
@before_env_buildkite_message = ENV["BUILDKITE_MESSAGE"]
ENV["BUILDKITE_MESSAGE"] = "[ci skip] test_skip"

sub = create_build_context
assert_predicate sub, :skip?
ensure
ENV["BUILDKITE_MESSAGE"] = @before_env_buildkite_message
end

def test_skip_ci
@before_env_buildkite_message = ENV["BUILDKITE_MESSAGE"]
ENV["BUILDKITE_MESSAGE"] = "[skip ci] test_skip"

sub = create_build_context
assert_predicate sub, :skip?
ensure
ENV["BUILDKITE_MESSAGE"] = @before_env_buildkite_message
end

def test_skip_ci_dash_skip
@before_env_buildkite_message = ENV["BUILDKITE_MESSAGE"]
ENV["BUILDKITE_MESSAGE"] = "[ci-skip] test_skip"

sub = create_build_context
assert_predicate sub, :skip?
ensure
ENV["BUILDKITE_MESSAGE"] = @before_env_buildkite_message
end

def test_skip_skip_dash_ci
@before_env_buildkite_message = ENV["BUILDKITE_MESSAGE"]
ENV["BUILDKITE_MESSAGE"] = "[skip-ci] test_skip"

sub = create_build_context
assert_predicate sub, :skip?
ensure
ENV["BUILDKITE_MESSAGE"] = @before_env_buildkite_message
end

def test_skip_false
@before_env_buildkite_message = ENV["BUILDKITE_MESSAGE"]
ENV["BUILDKITE_MESSAGE"] = "not a skip commit message for ci"

sub = create_build_context
assert_not_predicate sub, :skip?
ensure
ENV["BUILDKITE_MESSAGE"] = @before_env_buildkite_message
end

def test_rails_root
sub = create_build_context
assert_equal Pathname.new(Dir.pwd), sub.rails_root
Expand Down

0 comments on commit 5c8973d

Please sign in to comment.