Kostas Demiris
Kostas Demiris

Reputation: 3621

Gitlab pipeline trigger does not work with master branch [Status code: 400]

I'm using GitLab Enterprise Edition 11.1.4-ee and I have a project that I want to start its pipeline via a trigger.

In this repo, I have more than 2 branches and of course the master. No branch is protected.

I have created a pipeline trigger per the documentation and I'm trying to trigger the pipeline by making a POST request with NodeJS. This is the code.

const gitlab_url = 'my-gitlab-url.com';
const project_name = 'the-project-name'
const branch_name = 'master'
const id    = 50;
const token = 'some-peculiar-token';
const trigger_path  = '/api/v4/projects/'+id+'/ref/'+branch_name+'/trigger/pipeline?token='+token;

// code that makes the POST request to Gitlab
var http = require("http");
var options = {
  hostname: gitlab_url,
  port: 80,
  path: trigger_path,
  method: 'POST',
  headers: {
      'Content-Type': 'application/json',
  }
};
var req = http.request(options, function(res) {
    console.log('Status: \n' + res.statusCode);
    console.log('Headers: \n' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (body) {
    console.log('Body: ' + body);
  });
});
req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('{"string": "Hello, World"}');
req.end();

I can successfully trigger any other branch except master.

When I trigger eg the devops branch, I get the following response.

Status: 201
Headers: {"server":"nginx","date":"Fri, 05 Oct 2018 07:20:46 GMT","content-type":"application/json","content-length":"604","connection":"close","cache-control":"max-age=0, private, must-revalidate","etag":"W/\"40c1533ca55d0150e3f84c4a1e8b92\"","vary":"Origin","x-content-type-options":"nosniff","x-frame-options":"SAMEORIGIN","x-request-id":"57e9b458-92-4621-bf7a-ebafaa7697","x-runtime":"2.437610","strict-transport-security":"max-age=31536000"}
Body: {"id":3799,"sha":"fabc4d9c07e3d3b1645fd6445091fe3304b74a","ref":"devops","status":"pending","before_sha":"0000000000000000000000000000000000000000","tag":false,"yaml_errors":null,"user":{"id":1,"name":"Administrator","username":"something","state":"active","avatar_url":"http://my-gitlab-url.com/uploads/-/system/user/avatar/1/avatar.png","web_url":"http://gimy-gitlab-url.com/something"},"created_at":"2018-10-05T07:20:44.340Z","updated_at":"2018-10-05T07:20:46.039Z","started_at":null,"finished_at":null,"committed_at":null,"duration":null,"coverage":null}

However, when I try to trigger the master branch, I get this.

Status:
400
Headers:
{"server":"nginx","date":"Fri, 05 Oct 2018 07:42:31 GMT","content-type":"application/json","content-length":"60","connection":"close","cache-control":"no-cache","vary":"Origin","x-content-type-options":"nosniff","x-frame-options":"SAMEORIGIN","x-request-id":"20f46c-5a5-444a-ae2f-829545a49022","x-runtime":"0.161319"}
Body: {"message":{"base":["No stages / jobs for this pipeline."]}}

The 400 response status is for Malformed request, however, I don't think that it has something to do with the POST request, but rather with Gitlab configuration for the specific branch. That is just a guess, because the requests for the other branches work well.

Any insight would be helpful.

Upvotes: 1

Views: 5156

Answers (1)

Kostas Demiris
Kostas Demiris

Reputation: 3621

My bad !

I was trying to trigger the master branch but I did not have it in my gitlab-ci.yml file.

  # run only for the specific branch
  only:
    - production
    - multi
  # the job will be handled by the same tagged runner
  tags:
    - onerunner

Upvotes: 4

Related Questions