1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Jenkinsの認証情報を用いてbatでNASにアクセスする

Last updated at Posted at 2025-05-09
stage('Copy to NAS with Login') {
    steps {
        withCredentials([usernamePassword(credentialsId: 'nas-credentials', usernameVariable: 'NAS_USER', passwordVariable: 'NAS_PASS')]) {
            bat '''
            chcp 65001
            set "NAS_PATH=\\\\NAS01\\coverage-reports"
            net use %NAS_PATH% /user:%NAS_USER% %NAS_PASS%
            set "src=%WORKSPACE%\\build\\reports\\jacoco"
            set "dst=%NAS_PATH%\\%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%"
            mkdir "%dst%"
            robocopy "%src%" "%dst%" /E
            '''
        }
    }
}

自分用

stage('Zip and Upload Coverage to NAS') {
    steps {
        withCredentials([usernamePassword(credentialsId: 'nas-credentials', usernameVariable: 'NAS_USER', passwordVariable: 'NAS_PASS')]) {
            bat '''
            chcp 65001

            rem ====== 日付フォルダ名を作成 ======
            for /f %%i in ('powershell -Command "Get-Date -Format yyyyMMdd"') do set DATE_DIR=%%i

            rem ====== ZIPファイル作成 ======
            powershell -Command "Compress-Archive -Path build\\reports\\jacoco\\* -DestinationPath build\\reports\\jacoco\\jacoco_%DATE_DIR%.zip -Force"

            rem ====== NASへ接続 ======
            set "NAS_PATH=\\\\NAS01\\coverage-reports"
            net use %NAS_PATH% /user:%NAS_USER% %NAS_PASS%

            rem ====== アップロード ======
            copy build\\reports\\jacoco\\jacoco_%DATE_DIR%.zip %NAS_PATH%\\jacoco_%DATE_DIR%.zip

            rem ====== 接続解除(必要に応じて) ======
            net use %NAS_PATH% /delete /yes
            '''
        }
    }
}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?