LoginSignup
17
20

More than 5 years have passed since last update.

[ jenkins ] リモートからジョブの実行

Posted at

Jenkinsのwikiを見てリモートからのジョブ実行を試行錯誤メモ。

0. 基本

ご存じのとおり基本的にはこんな感じで起動できる

curl http://[jenkins-server]:8080/job/[job_name]/build

以下は応用編。
ネタ元はjenkinsのwiki[https://wiki.jenkins-ci.org/display/JA/Remote+access+API]

1. tokenを使ってみる

jenkinsのグローバルセキュリティの設定でsecurityをONにしている場合、もうちょっと頑張る必要あり。
あと、プロキシとかを経由しないとjenkinsサーバにたどり着かない場合も多々あり。。。。。
そんなときはtokenを使用する。(ほかにもcurlのuオプションでユーザ認証するなど別の方法もある。)

Build TriggersセクションにTrigger builds remotely (e.g., from scripts)というチェック項目がある。
これにチェックを入れてtokenを適当な文字列で設定する。いま仮にtestと設定。

上記の前提だとジョブはこんな感じで起動できる

export proxy="[proxyserver]:[port]"
export url="http://[jenkins-server]:8080/job/[job_name]/build"
curl -x $proxy -X POST $url -d token=test

2. パラメータも一緒に渡す。

ジョブの設定でパラメータを使う場合。
param1が設定したパラメータ名でtestvalue1がジョブ実行時に渡す値。

export json="{\"parameter\": [{\"name\": \"param1\", \"value\": \"testvalue1\"}] }"
curl -x $proxy -X POST $url -d token=test --data-urlencode json="$json"

整形済みのjsonファイルで渡してみる

cat testfile2.json
{
  "parameter": [
    {
      "name": "param1",
      "value": "testvalue1"
    }
  ]
}
curl -x $proxy -X POST $url -d token=test --data-urlencode json="`cat testfile2.json`"

出来た。

最後に複数のパラメータを渡して実行してみる

cat testfile2.json
{
  "parameter": [
    {
      "name": "param1",
      "value": "testvalue1"
    },
    {
      "name": "param2",
      "value": "testvalue2"
    }
  ]
}
curl -x $proxy -X POST $url -d token=test --data-urlencode json="`cat testfile2.json`"

ちゃんとジョブがリモート実行できました。

17
20
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
17
20