Trigger ci using gitlab api

Posted Jun 15, 20202 min read

gitlab is a configuration management service similar to github. Compared to github, gitlab is more professional, has more webhooks and APIs, and a more powerful continuous integration(ci) service.

We can trigger ci through webhook or api, and cooperate with tools such as gulp to build a more complete automated workflow.

This article will take you to learn how to use gitlab api. For more in-depth use, please refer to Official Document .

ready

First of all, for the project IDE that we need to trigger through the api, go to the project page and click on 1., 2., 3. in the figure in sequence.

image-20200615204733718.png

Then you can open the page for adding a trigger. After describing the purpose of the trigger, click the add trigger button to generate our token. We need to use this token to trigger ci.

image-20200615205600028.png

Then we can trigger ci with the following command.

curl -X POST -F token=TOKEN -F ref=REF_NAME https://gitlab.example.com/api/v4/projects/project_id/trigger/pipeline

If the page you open is different from the picture, it may be that you do not have the permission of the project, then you need to find a colleague to help you get a token and the above command.

In the above, we need to replace TOKEN with our own token and replace REF_NAME with the target branch name, then ci will be triggered on the target branch.

Among them, https://gitlab.example.com/api/v4/projects/project_id/trigger/pipeline is automatically generated by opening the above setting page, and we do not need to modify it. Different projects have different connections.

If you want to pass in environment variables to ci, you can use the following methods.

curl -X POST -F token=TOKEN -F ref=REF_NAME "variables[v_toolkit]=${version}" https://gitlab.example.com/api/v4/projects/project_id/trigger/pipeline

Then get the environment variables in our code in the following way:

console.log(`process.env.v_toolkit:${process.env.v_toolkit}`)

Through environment variables, we can also control to trigger only specific jobs in ci and bypass other jobs.

It should be noted that the method that requires manual triggering cannot be triggered through the API, so instead of setting the job to manual triggering, we can trigger specific jobs in ci by passing in environment variables.

Give a chestnut

We can trigger ci through gulp script

//gulpfile.js
const path = require('path')
const cp = require("child_process")
const gulp = require('gulp')

function runci(cb) {
  const versionIndex = process.argv.indexOf('--ve')
  const version = process.argv[versionIndex + 1]
  const branchIndex = process.argv.indexOf('--br')
  const branch = process.argv[branchIndex + 1]
  console.log(`version:${version}`)
  console.log(`branch:${branch}`)
  cp.execSync(`curl -X POST -F token=Your TOKEN -F "ref=${branch}" -F "variables[v_transformer]=${version}" https://gitlab.example.cn/api /v4/projects/1234/trigger/pipeline`)
  cb()
}

module.exports = {
  runci
}

Run from the command line:

npx gulp runci --ve 1.0.0 --br master

You can trigger ci!

image-20200615212814918.png