LoginSignup
13
16

More than 5 years have passed since last update.

Jenkins CLIをつかって操作

Posted at

目的

  • 自分用備忘録
  • 以外にJenkins CLI知らない人多いので公開

手順概要

  • ssh公開鍵をJenkinsに設定
  • jenkins-cli.jarをダウンロード
  • jenkins-cliを実行する

手順

ssh公開鍵をJenkinsに設定

右上から 設定 クリックして自分のユーザ設定を変更する。

Kobito.JwjZXC.png

SSH Public Keysに自分の公開鍵の情報を追加する。
以下は入力例(ダミー)

Kobito.ifiA4g.png

jenkins-cli.jarをダウンロード

Jenkinsの管理 をクリック。

Kobito.WakH9a.png

Jenkins CLI をクリック。

Kobito.h4nWIH.png

遷移先画面の jenkins-cli.jar リンクをクリックするとjenkins-cli.jarがダウンロードされる。

Kobito.L6fVDr.png

jenkins-cliを実行する

動作確認

ダウンロードしたファイルを適当な場所に配置して実行する。
ヘルプが表示されればOK。

jenkins-cliのヘルプを表示
$ java -jar jenkins-cli.jar -s http://{JenkinsのURL} help
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by hudson.remoting.RemoteClassLoader (file:/Users/01013548/repositories/chappie/chappie-infra-job-definition/jenkins-cli.jar) to method java.lang.ClassLoader.getClassLoadingLock(java.lang.String)
WARNING: Please consider reporting this to the maintainers of hudson.remoting.RemoteClassLoader
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
  add-job-to-view
    Adds jobs to view.
  build
    ジョブをビルドします。オプションで完了するまで待ちます。
  cancel-quiet-down
    "quiet-down"コマンドの処理をキャンセルします。
  clear-queue
    ビルドキューをクリアします。

~~~~~~~~~~~~中略~~~~~~~~~~~~~~~~~~~~

  update-node
    標準入力からの情報でノード定義XMLを更新します。get-nodeコマンドの正反対のことを行います。
  update-view
    Updates the view definition XML from stdin. The opposite of the get-view command.
  version
    バージョンを表示します。
  wait-node-offline
    ノードがオフラインになるのを待ちます。
  wait-node-online
    ノードがオンラインになるのを待ちます。
  who-am-i
    認証情報を表示します。

Java9を使うとWarningが大量に出るけど問題なし。

ジョブの確認

既存のジョブを確認するときは、以下のコマンドを実行する。
標準出力にジョブの内容が表示される。あとは煮るなり焼くなりお好きに。

既存ジョブの表示
$ java -jar jenkins-cli.jar -s http://{JenkinsのURL} get-job ジョブの名前
既存ジョブの表示(実行結果サンプル)
$ java -jar jenkins-cli.jar -s http://{JenkinsのURL} get-job sample
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by hudson.remoting.RemoteClassLoader (file:/Users/01013548/repositories/chappie/chappie-infra-job-definition/jenkins-cli.jar) to method java.lang.ClassLoader.getClassLoadingLock(java.lang.String)
WARNING: Please consider reporting this to the maintainers of hudson.remoting.RemoteClassLoader
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
<?xml version='1.0' encoding='UTF-8'?>
<project>
  <description>サンプル</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <org.datadog.jenkins.plugins.datadog.DatadogJobProperty plugin="datadog@0.6.1">
      <tagProperties></tagProperties>
      <tagFile></tagFile>
      <emitOnCheckout>false</emitOnCheckout>
    </org.datadog.jenkins.plugins.datadog.DatadogJobProperty>
  </properties>
  <scm class="hudson.scm.NullSCM"/>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders/>
  <publishers/>
  <buildWrappers>
    <hudson.plugins.ws__cleanup.PreBuildCleanup plugin="ws-cleanup@0.32">
      <deleteDirs>false</deleteDirs>
      <cleanupParameter></cleanupParameter>
      <externalDelete></externalDelete>
    </hudson.plugins.ws__cleanup.PreBuildCleanup>
    <hudson.plugins.ansicolor.AnsiColorBuildWrapper plugin="ansicolor@0.4.3">
      <colorMapName>xterm</colorMapName>
    </hudson.plugins.ansicolor.AnsiColorBuildWrapper>
    <hudson.plugins.timestamper.TimestamperBuildWrapper plugin="timestamper@1.8.7"/>
  </buildWrappers>

ジョブの作成

リダイレクトを使って標準入力からxmlファイルを読み込んでジョブを作成する。
XMLファイルを全部イチから描くのは辛いので、
他のジョブをget-jobした結果を保管 & 一部変更 -> create-jobするのが楽そう。

ジョブの作成
$ java -jar jenkins-cli.jar -s http://{JenkinsのURL} create-job {ジョブ名} < ジョブの定義ファイル(xml)

まとめ

これでJenkinsの画面をぽちぽちしてジョブ作成するとかはしなくてよくなる。
似たようなジョブを大量に作るとか、刺身タンポポ的なお仕事を多少は楽にできるはず。。。

XML-yamlの相互変換とかしちゃうとと人間にも優しくなれそうですね。
(できるかどうかは知らない)

13
16
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
13
16