LoginSignup
0
0

More than 3 years have passed since last update.

GitLabのwebhookからのjenkisビルド

Last updated at Posted at 2019-12-06

はじめに

プロジェクトの運用でアセットを作るときにjenkinsでビルドしてるんですが
画像などgitにpushしてjenkinsのパラメータをセットしてポチる
これだけなんですが、これを行う作業が毎日結構な回数あって
なんのアセットあげるんだっけな、パラメータどんなだっけなってのがちょくちょくあって
地味にめんどくさい

やりたいこと

gitにpushしたら勝手にjenkisビルドして欲しい
例)
test.pngをコミット
コミットされたファイル名をphpで解析し、jenkinsにコミットされたファイルが包まれるアセットビルドを依頼
アセットが出来上がる

※今回は勝手にビルドされると困ることもあるのでchatworkにjenkinsビルド用のリンクが飛んで来てそれをポチるとjenkinsが回る流れにした

GitLabのwebhook使ってみた

⑴ GitLabにログイン
⑵ プロジェクトを開く
⑶ Setting > Integrationsを選択
⑷ webhook用のURLを入力
⑸ TriggerのPush eventsを選択
⑹ Add webhook

20191206-104002.png

Gitから渡されるパラメータはどんなものか

GitLabのIntegrationsの下にコミット履歴があり、そこに渡されたパラメータの履歴も残ってます
スクリーンショット 2019-12-06 11.43.52.png

{
  "object_kind": "push",
  "event_name": "push",
  "before": "xxxxxx",
  "after": "********",
  "ref": "refs/heads/feature_1.0.0",
  "checkout_sha": "********",
  "message": null,
  "user_id": 1234,
  "user_name": "テスト太郎",
  "user_username": "testtaro",
  "user_email": "",
  "user_avatar": null,
  "project_id": 123,
  "project": {
    "id": 123,
    "name": "test_unity",
    "description": "テストのunity puroject",
    "web_url": "http://biz.xxxxxx.co.jp/*****/test_unity",
    -- 一部省略
  },
  "commits": [
    {
      "id": "***********",
      "message": "テストコミット\n",
      "timestamp": "2019-12-05T13:08:23Z",
      "url": "http://biz.xxxxxx.co.jp/*****/test_unity/commit/***************",
      "author": {
        "name": "テスト太郎",
        "email": ""
      },
      "added": [

      ],
      "modified": [
        "test.png",
        "test2.png"
      ],
      "removed": [

      ]
    }
  ],
  -- 一部省略
}
擬似コード(trigger.php)

コミットされたファイルを解析し、jenkinsに必要なパラメータを生成してチャットワークに流します

<?php

//------------------------------
// 変数
//------------------------------
$postdata = file_get_contents("php://input");
$postdata = json_decode($postdata, true);

$action                 = @$postdata['object_kind'];    //merge_request
$commitUserId           = @$postdata['user_id'];        //コミットユーザー
$targetBranch           = @$postdata['ref'];            //target branch
$commitList             = @$postdata['commits'];        //コミットファイル情報

//------------------------------
// jenkinsに必要なパラメータを生成
//------------------------------
$jenkinsParamList = array();
// $commitListからパラメータに必要な情報を抜き取る

//------------------------------
// チャットワークにリンクを投げる
//------------------------------
sendMsg( $chatWorkId, $url, $jenkinsParamList );


/**
 * メッセージ送信 
 */
function sendMsg($to, $url, $parameters = array()) {

        $msg = "[info][title]アセット回しますか?[/title]";
        $msg .= "[To:".$to."]\n";
        $msg .= "masterにコミットがされました\n";
        $msg .= "\n";
        $msg .= "↓クリックでビルド開始\n";
        $msg .= $url . '?' . http_build_query($parameters)."\n";
        $msg .= "\n";
        $msg .= "[/info]";


        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "https://api.chatwork.com/v2/rooms/*****/messages");
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-ChatWorkToken:xxxxxxxx'));
        $option = ;
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('body' => $msg ), '', '&'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
        $response = curl_exec($curl);
        curl_close($curl);

}

jenkinsリンク

このリンクをポチればビルド開始!!わーい
スクリーンショット 2019-12-06 11.35.49 1.png

チャットワークに流さずjenkinsを直で回す場合(php)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://[Jenkinsユーザ名]:@[Jenkinsホスト]:8080/job/TEST_ASSET_BUILD/buildWithParameters" . '?' . http_build_query($parameters));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
curl_close($ch);

参考

https://sendgrid.kke.co.jp/blog/?p=1851
https://qiita.com/shingo_kawahara/items/ac40592726f3756a5c3d

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0