2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jenkins pipelineでSlave PCにあるファイルを読みこむ方法

Last updated at Posted at 2019-09-11

Jenkins pipelineを使っていると、SlaveとMasterの概念がごちゃごちゃになって、ついついMaster PCの中を見に行ってしまうようなコードを書いてハマったりします。
そういった点を解消しながら、Slave PCのテキストファイルを読み込む方法をご紹介したいと思います。

まず、前提としてbatコマンドを実行すると、Slave PC側で実行されるという基本を覚えておいてください。
一方、Pipeline scriptでGroovyによるコマンドでファイルを見に行こうとすると、Master PCの中身を見に行きますので、ご注意ください。

そのため、実現方法としては、

  1. batコマンドでファイルを読みこむ
  2. echoによる出力を戻り値で変数に返す

となります。

結果は以下です。

jenkinsfile


TEXT_PATH = WORKSPACE+'/text_directory'
TEXT_CONTENTS = bat returnStdout: true, script: '''@echo off 
for /f " usebackq tokens=*" %%a in ('''+TEXT_PATH+'''/contents.txt) do (
echo %%a
goto :exit_for
)
:exit_for'''

print( TEXT_CONTENTS )

これはテキストファイルの最初の1行だけ読みこむ方法ですが、応用すれば複数行もいけます。

注意点としては、batファイル内でechoを使って文字列を出力してしまうと、それも戻り値に含まれてしまいますので、考慮するようにして下さい。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?