LoginSignup
0
0

More than 5 years have passed since last update.

windowsに置いたjenkinsからファイルをscpしたい

Last updated at Posted at 2015-10-22

前提

windowsで起動したjenkinsの「windowsバッチコマンドの実行」にファイル転送バッチを書きたい
ファイル転送にはwinscpを使う
winscpはC:\Program Files (x86)\WinSCP\WinSCP.comにある
秘密鍵はD:\key_rsa.pri.ppkにある

jenkins側のパラメータの設定

USER...転送先ユーザ名
TARGET_HOST...転送先のサーバ
TARGET_HOSTKEY...ホストキー(後述)
TARGET_FILE...転送したいファイル名
TARGET_WORKDIR...転送したいファイルがおいてあるローカルディレクトリ
TARGET_DIR...配置したい転送先のディレクトリ
TMP_FILE...転送コマンド群の一時ファイル、最後消される

実装

@echo off
echo option batch on > %TMP_FILE%
echo open %USER%@%TARGET_HOST% -hostkey="%TARGET_HOSTKEY%" >> %TMP_FILE%
echo cd %TARGET_DIR% >> %TMP_FILE%
echo rm %TARGET_FILE% >> %TMP_FILE%
echo put %TARGET_WORKDIR%%TARGET_FILE% >> %TMP_FILE%
echo exit >> %TMP_FILE%
@echo on

:: type %TMP_FILE%

"C:\Program Files (x86)\WinSCP\WinSCP.com" /privatekey="D:\key_rsa.pri.ppk"  /script="%TMP_FILE%"

del %TMP_FILE%

説明

@echo off
色々
@echo on

echo offで結果表示しない
色々の部分でwinscpに対するコマンド群ファイルを作成。
サーバー接続
転送先ディレクトリに移動
古い転送したいファイルを消す(この時ファイルが無くてもOK)
ファイル置く
終わり
echo onで結果表示する

ファイルを置く、で強制的に上書きするには

option confirm off

でいけるかも。(確認OFFしてるけど…)

:: type %TMP_FILE%

デバッグ用。::でコメントアウトされている。コメントアウトを外すとTMP_FILEの中身を表示

そしてwinscpにコマンド発行
最後にTMP_FILE消す

ホストキーについて

必要。
適当な値で一度動かしてみると教えてくれる。

D:\jenkins\jobs\test\workspace>"C:\Program Files (x86)\WinSCP\WinSCP.com" /privatekey="D:\key_rsa.pri.ppk"  /script="tmp.txt" 
batch           on        
Searching for host...
Connecting to host...
Authenticating...
The server's host key was not found in the cache. You have no guarantee that the server is the computer you think it is.
The server's rsa2 key fingerprint is:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ←この部分
If you trust this host, press Yes. To connect without adding host key to the cache, press No. To abandon the connection press Cancel.
Continue connecting and add host key to the cache?

応用編

ついでにtomcatの再起動などサーバー側でコマンドを発行したい場合は

echo call 実行したいコマンド >> %TMP_FILE%

をコマンド群に追加する

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