ようへいの日々精進XP

よかろうもん

Jenkins CLI で Jenkins プラグインをインストールしたメモ

tl;dr

Jenkins の練習していたら CLI で操作出来るとのことなので、Jenkins プラグインCLI 利用してインストールしてみたメモ。


メモ

試した環境

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.3 LTS
Release:        14.04
Codename:       trusty

$ java -jar jenkins-cli.jar -s http://localhost:8080 version                                                                                                               
1.651.3

参考

CLI をダウンロード

wget http://localhost:8080/jnlpJars/jenkins-cli.jar

ヘルプ

java -jar jenkins-cli.jar -s http://localhost:8080 help

以下のように出力される。

  add-job-to-view
    Adds jobs to view.
  build
    Builds a job, and optionally waits until its completion.
  cancel-quiet-down
    Cancel the effect of the "quiet-down" command.
  clear-queue
    Clears the build queue.
  connect-node
    Reconnect to a node.
  console
    Retrieves console output of a build.
  copy-job
    Copies a job.

(snip)

  update-view
    Updates the view definition XML from stdin. The opposite of the get-view command.
  version
    Outputs the current version.
  wait-node-offline
    Wait for a node to become offline.
  wait-node-online
    Wait for a node to become online.
  who-am-i
    Reports your credential and permissions.

前後してしまうが、CLI は以下のように実行する。

java -jar jenkins-cli.jar [-s JENKINS_URL] command [options...] [arguments...]

プラグインのインストール

インストールしたいプラグイン

以下のように実行する。

java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin github
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin build-pipeline-plugin
java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin docker-build-publish

尚、引数として指定するプラグインの名前は Plugins ページから辿れる個々のプラグインページで確認するプラグイン ID を指定する。

f:id:inokara:20160702085300p:plain

プラグインのインストール後は Jenkins プロセスを再起動して設定を反映させる。

プラグインで何が出来そうか

適当に利用出来るオプションを叩いてみた。

  • バージョンを確認
$ java -jar jenkins-cli.jar -s http://localhost:8080 version
1.651.3
  • ジョブの一覧を取得
$ java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs
Docker_Build_Push
Serverspec_Handson
  • ジョブの詳細を取得
$ java -jar jenkins-cli.jar -s http://localhost:8080 get-job Docker_Build_Push 

以下のように出力される。

<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>

(snip)

  </builders>
  <publishers/>
  <buildWrappers/>
</project>
  • Jenkins の再起動
$ java -jar jenkins-cli.jar -s http://localhost:8080 restart

セキュアにアクセス

あくまでも試してみた感じだと Jenkins CLI でセキュアに Jenkins にアクセスしたい場合には以下を設定したほうが良さそう。

  • セキュリティを有効化(グローバルセキュリティの設定)
  • アクセス制御で「ユーザー情報」を環境に応じて設定(今回は「Jenkins のユーザーデータベース」を選択した)
  • 同じくアクセス制御の「権限管理」において「ログイン済みユーザーに許可」を選択

尚、上記の設定を行った場合、Jenkins のジョブの詳細(get-job コマンド)等は以下のように利用出来なくなる。

$ java -jar jenkins-cli.jar -s http://localhost:8080 get-job Docker_Build_Push

ERROR: anonymous is missing the Job/ExtendedRead permission

これを解決するには Jenkins に Jenkins CLI を実行するユーザーの公開鍵を Jenkins 上のユーザーに登録することで利用出来るようになる。

f:id:inokara:20160702085019p:plain

上記を設定した後に get-job コマンドを実行すると以下のようにジョブの詳細を確認することが出来る。

$ java -jar jenkins-cli.jar -s http://localhost:8080 get-job Docker_Build_Push
<?xml version='1.0' encoding='UTF-8'?>
<project>
  <actions/>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>

(snip)

  </builders>
  <publishers/>
  <buildWrappers/>
</project>

以上

素敵な Jenkins ライフをお送り下さいmm