diff --git a/lib/api/commit_statuses.rb b/lib/api/commit_statuses.rb
index 1785362656e40b0f44df164b81a57b5f90cf8328..c89abf72e2de41fca85b4da0d8b614215595735f 100644
--- a/lib/api/commit_statuses.rb
+++ b/lib/api/commit_statuses.rb
@@ -78,13 +78,16 @@ class CommitStatuses < ::API::Base
 
         name = params[:name] || params[:context] || 'default'
 
-        pipeline ||= user_project.ci_pipelines.create!(
+        pipeline ||= user_project.ci_pipelines.build(
           source: :external,
           sha: commit.sha,
           ref: ref,
           user: current_user,
           protected: user_project.protected_for?(ref))
 
+        pipeline.ensure_project_iid!
+        pipeline.save!
+
         authorize! :update_pipeline, pipeline
 
         status = GenericCommitStatus.running_or_pending.find_or_initialize_by(
diff --git a/spec/requests/api/commit_statuses_spec.rb b/spec/requests/api/commit_statuses_spec.rb
index 47bc3eb74a644ede8a3b234176cf7115a7104840..155d9a36e176312fee7b9abef56df15ec10829dd 100644
--- a/spec/requests/api/commit_statuses_spec.rb
+++ b/spec/requests/api/commit_statuses_spec.rb
@@ -131,7 +131,7 @@ def create_status(commit, opts = {})
         %w[pending running success failed canceled].each do |status|
           context "for #{status}" do
             context 'when pipeline for sha does not exists' do
-              it 'creates commit status' do
+              it 'creates commit status and sets pipeline iid' do
                 post api(post_url, developer), params: { state: status }
 
                 expect(response).to have_gitlab_http_status(:created)
@@ -145,6 +145,8 @@ def create_status(commit, opts = {})
                 if status == 'failed'
                   expect(CommitStatus.find(json_response['id'])).to be_api_failure
                 end
+
+                expect(::Ci::Pipeline.last.iid).not_to be_nil
               end
             end
           end