diff --git a/app/services/ci/create_commit_status_service.rb b/app/services/ci/create_commit_status_service.rb
index 00c9dc1cd21021088c2bfb9f8f82adf43b1aed4e..6688ac75b2ef1e0aafd6138b0558965787a222c4 100644
--- a/app/services/ci/create_commit_status_service.rb
+++ b/app/services/ci/create_commit_status_service.rb
@@ -8,6 +8,9 @@ class CreateCommitStatusService < BaseService
 
     delegate :sha, to: :commit
 
+    # Default number of pipelines to return
+    DEFAULT_LIMIT_PIPELINES = 100
+
     def execute(optional_commit_status_params:)
       in_lock(pipeline_lock_key, **pipeline_lock_params) do
         @optional_commit_status_params = optional_commit_status_params
@@ -60,7 +63,8 @@ def commit
     strong_memoize_attr :commit
 
     def first_matching_pipeline
-      pipelines = project.ci_pipelines.newest_first(sha: sha, limit: 100)
+      limit = params[:pipeline_id] ? nil : DEFAULT_LIMIT_PIPELINES
+      pipelines = project.ci_pipelines.newest_first(sha: sha, limit: limit)
       pipelines = pipelines.for_ref(params[:ref]) if params[:ref]
       pipelines = pipelines.id_in(params[:pipeline_id]) if params[:pipeline_id]
       pipelines.first
diff --git a/spec/services/ci/create_commit_status_service_spec.rb b/spec/services/ci/create_commit_status_service_spec.rb
index 59a20b81cae4fa6fb56e4c88dc56e32dd1c48cf2..664ea438fea01c1efc0567b18a228a7db9697908 100644
--- a/spec/services/ci/create_commit_status_service_spec.rb
+++ b/spec/services/ci/create_commit_status_service_spec.rb
@@ -257,6 +257,10 @@
         }
       end
 
+      before do
+        stub_const("#{described_class}::DEFAULT_LIMIT_PIPELINES", 3)
+      end
+
       it 'update the correct pipeline', :sidekiq_might_not_need_inline do
         expect { response }
           .to not_change { ::Ci::Pipeline.count }.from(2)
@@ -267,6 +271,24 @@
         expect(other_pipeline.reload.status).to eq('success')
       end
 
+      it 'create a status on an old pipeline', :sidekiq_might_not_need_inline do
+        # 3 pipelines more are created to validate that it is possible to set a status on the 4th.
+        (0..2).each do |_|
+          project.ci_pipelines.build(source: :push, sha: commit.id, ref: 'master', status: 'created').tap do |p|
+            p.ensure_project_iid!
+            p.save!
+          end
+        end
+
+        expect { response }
+          .to not_change { ::Ci::Pipeline.count }.from(5)
+          .and change { ::Ci::Stage.count }.by(1)
+          .and change { ::CommitStatus.count }.by(1)
+
+        expect(first_pipeline.reload.status).to eq('created')
+        expect(other_pipeline.reload.status).to eq('success')
+      end
+
       context 'when pipeline_id and sha do not match' do
         let(:other_commit) { create(:commit) }
         let(:sha) { other_commit.id }