3

I would like to start builds in GitLab CI in response to issue transitions in JIRA.

I have not found a direct way to do that from searching online. I have found ways to get GitLab to tell JIRA what to do, but seemingly no solutions in the other direction.

We have a constraint which would make JIRA to GitLab messages very helpful: we must deploy to servers under the rule that a branch gets deployed

  1. to ...test.example.com when the JIRA issue has status QA
  2. to ...stage.example.com when the JIRA issue has status Stage
  3. to ...beta.example.com when the JIRA issue has status Beta
  4. to ....example.com when the JIRA issue has status Done

Note that we have added a Merge Request field to issues in our JIRA, so we know "the branch for an issue" (and "the issues for a branch").

To satisfy this constraint we run a python script to do deploys for QA, Stage, Beta every hour in a crontab for every project. That script has too many top-level loops, and too many features (being old).

Instead I would prefer to have JIRA notify that an issue is ready for re-build, and then ask our GitLab CI to re-build the relevant Merge Request.

In a JIRA workflow status transitions can "Trigger A Webhook", but it seems we will have to supply our own REST api to which JIRA can post messages, and which can then ping GitLab. That's a new internal app, which will need maintenance, and gain features, so we would like to find out if there are any other solutions.

2 Answers 2

3
+50

I didn't get what's wrong with webhooks You just create a webhook on Gitlab CI side and provide this URL to your Jira account.

https://gitlab.com/api/v4/projects/PROJECT_ID/ref/REF_NAME/trigger/pipeline?token=TOKEN

PROJECT_ID - unique project id on Gitlab

REF_NAME - your qa / stage / beta branches within repository

TOKEN - uniquely generated access token (gitlab provided)

Look here for more details how to trigger build on Gitlab via this call

1
  • 1
    Thanks - that seems to be exactly what we need.
    – jalanb
    Commented Jan 21, 2021 at 21:56
1

One option for doing this is to create a database trigger.

In Mysql

In MySQL and MariaDB, you can do this with mysql-udf-http. From perodriguezl answer from Stack Overflow:

You can use a mysql-udf-http and then create a trigger like this:

delimiter $$
CREATE TRIGGER upd_check BEFORE UPDATE ON account 
FOR EACH ROW 
  BEGIN 
    IF NEW.amount > 0 THEN 
      set @json = select json_object(account_id,amount) 
      select http_post('http://restservice.example.com/account/post',@json); 
    END IF; 
  END;$$ 

delimiter;

In Postgres

In Postgres, you can write a stored procedure and use plperlu with REST::Client

From JustMe's Answer on Stack Overflow:

...the fastest way is to use plperlu with REST::Client package, e.g.:

CREATE OR REPLACE FUNCTION restful.put(auri character varying, ajson_text text)
 RETURNS text
 LANGUAGE plperlu
 SECURITY DEFINER
AS $function$
  use REST::Client;  
  use Encode qw(encode);
  my $client = REST::Client->new();    
  $client->getUseragent()->proxy( 'https', 'http://some-proxy/' ); # use for proxy authentication
  $client->addHeader('Content-Type', 'application/json');          # headers
  $client->POST( $_[0], encode('UTF-8', $_[1]));                   # encoding
  return $client->responseContent();  
$function$

Using Oracle

On an Oracle database, You will need to use UTL_HTTP - Lucas Jellema's blog has a good example. There are also several others available.

Conclusion

In each case, You would then presumably want to tweak your trigger to watch for an update statement on the table and check for the status you want to trigger off of (eg, you probably don't want to trigger off of new/closed) as well as make sure it is for the right project (Not quite sure how JIRA's schema is laid out - it might do one table per project. Eg, you probably don't want to trigger builds off of IT's tickets, but only off of dev or QA's JIRA project if you have multiple.)

4
  • We don't have MySQL installed on any of the relevant servers. We don't want to set a status in JIRA, we want to trigger a build in GitLab.
    – jalanb
    Commented Jan 18, 2021 at 23:05
  • @jalanb - I understand you do not want to set a status in JIRA. When your JIRA status transitions to something like "Ready for Testing", you want Gitlab CI to kick of a build and/or testing suite. You can do this with a REST call to Gitlab from your JIRA database server using a store procedure upon transition when a user changes the state in JIRA. Commented Jan 19, 2021 at 2:42
  • Valid answer. you should upvote
    – schmichri
    Commented Jan 19, 2021 at 23:22
  • 1
    We use JIRA Cloud, and do not have access to a "JIRA database server". Seems like overkill anyway, when I can just "Tigger a Webhook" from the relevant Transition page in the UI. The question is about the "REST call to Gitlab", not JIRA
    – jalanb
    Commented Jan 21, 2021 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.