Jenkins 作业返回尝试执行文本替换时找不到的文件



我讨论了几分钟,我不明白为什么当文件当前存在于存储库中时,jenkins 无法找到npmrc.template文件:

pipeline {
agent { label 'x86'}
environment {
//CI scripts
REGISTRY_AUTH = 'artifactory-ce'
COMMITER_NAME = "${env.GIT_COMMITTER_NAME}"
COMMITER_EMAIL = "${env.GIT_COMMITTER_EMAIL}"
}
options {
buildDiscarder(logRotator(numToKeepStr: '5'))
disableConcurrentBuilds()
skipDefaultCheckout(true)
}
stages {
stage('Init') {
steps {
checkout scm
withCredentials([usernamePassword(credentialsId: "${REGISTRY_AUTH}", usernameVariable: 'REGISTRY_LOGIN', passwordVariable: 'REGISTRY_PWD')]) {
script {
ofile = new File('npmrc.template')
fText = ofile.text;
fText = fText.replaceAll('USERNAME', $REGISTRY_LOGIN)
fText = fText.replaceAll('BASE64_PASSWORD', $REGISTRY_PWD)
fText = fText.replaceAll('youremail@email.com', $REGISTRY_LOGIN)
tFile = new File('.npmrc')
tFile.write(fTest)
}
}
stash name:'scm', includes:'*'
}
}
stage('Build') {
agent {
docker {
image 'node:8'
}
}
steps {
unstash 'scm'
sh 'npm run full-compile'
}
}
stage('version patch') {
agent {
docker {
image 'node:8'
reuseNode true
}
}
steps {
sh """
git config --global user.email '${COMMITER_NAME} && 
git config --global user.name '${COMMITER_EMAIL}'
"""
sh 'npm version patch'
}
}
}
post {
unstable {
slackSend (color: 'warning', message: "UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
failure {
slackSend (color: 'danger', message: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' (${env.BUILD_URL})")
}
}
}

输出:

詹金斯输出:

Commit message: "debug list"
[Pipeline] sh
[test-job-PR-33-3J4KAQ5TC4B5HJTBWWHJHBFJTERYSSF4MHFI3M4W4EQBPFQPLBPA] Running shell script
+ ls -ltra
total 508
drwxr-xr-x 36 root root   4096 Oct 26 09:41 ..
-rw-r--r--  1 root root    502 Oct 26 10:43 npmrc.template
-rwxr-xr-x  1 root root    391 Oct 26 10:43 build.sh
-rw-r--r--  1 root root   4244 Oct 26 10:43 angular.json
-rw-r--r--  1 root root   1687 Oct 26 10:43 README.md
-rw-r--r--  1 root root   2276 Oct 26 10:43 Jenkinsfile
-rw-r--r--  1 root root    575 Oct 26 10:43 .gitignore
-rw-r--r--  1 root root     22 Oct 26 10:43 .gitattributes
-rw-r--r--  1 root root    245 Oct 26 10:43 .editorconfig
-rw-r--r--  1 root root   2397 Oct 26 10:43 proxy.conf.js
-rw-r--r--  1 root root   3008 Oct 26 10:43 package.json
-rw-r--r--  1 root root 443221 Oct 26 10:43 package-lock.json
-rw-r--r--  1 root root   2807 Oct 26 10:43 tslint.json
-rw-r--r--  1 root root    408 Oct 26 10:43 tsconfig.json
drwxr-xr-x  5 root root   4096 Oct 26 10:43 src
drwxr-xr-x  5 root root   4096 Oct 26 10:43 .
drwxr-xr-x  8 root root   4096 Oct 26 10:43 .git
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
Stage "Build" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Publish)
Stage "Publish" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] slackSend
run slackstepsend, step null:false, desc :true
Slack Send Pipeline step configured values from global config - baseUrl: true, teamDomain: true, token: true, channel: true, color: false
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
GitHub has been notified of this commit’s build result
java.io.FileNotFoundException: npmrc.template (No such file or directory)

我可能错过了什么?

EDit:如 sugested,初始化阶段修改为:

stage('Init') {
steps {
checkout scm
sh "cd ${env.WORKSPACE} && pwd &&ls -ltra "
withCredentials([usernamePassword(credentialsId: "${REGISTRY_AUTH}", usernameVariable: 'REGISTRY_LOGIN', passwordVariable: 'REGISTRY_PWD')]) {
script {
ofile = new File("${env.WORKSPACE}/npmrc.template")
fText = ofile.text;
fText = fText.replaceAll('USERNAME', $REGISTRY_LOGIN)
fText = fText.replaceAll('BASE64_PASSWORD', $REGISTRY_PWD)
fText = fText.replaceAll('youremail@email.com', $REGISTRY_LOGIN)
tFile = new File('.npmrc')
tFile.write(fTest)
}
}
stash name:'scm', includes:'*'
}
}

输出:

[test-PR-33-3J4KAQ5TC4B5HJTBWWHJHBFJTERYSSF4MHFI3M4W4EQBPFQPLBPA] Running shell script
+ cd /var/lib/jenkins/workspace/test-PR-33-3J4KAQ5TC4B5HJTBWWHJHBFJTERYSSF4MHFI3M4W4EQBPFQPLBPA
+ pwd
/var/lib/jenkins/workspace/test-PR-33-3J4KAQ5TC4B5HJTBWWHJHBFJTERYSSF4MHFI3M4W4EQBPFQPLBPA
+ ls -ltra
total 560
drwxr-xr-x 7 root root  53248 Oct 26 11:17 ..
-rw-r--r-- 1 root root    502 Oct 26 11:19 npmrc.template
drwxr-xr-x 3 root root   4096 Oct 26 11:19 e2e
-rwxr-xr-x 1 root root    391 Oct 26 11:19 build.sh
-rw-r--r-- 1 root root   4244 Oct 26 11:19 angular.json
-rw-r--r-- 1 root root   1687 Oct 26 11:19 README.md
-rw-r--r-- 1 root root   2428 Oct 26 11:19 Jenkinsfile
-rw-r--r-- 1 root root    575 Oct 26 11:19 .gitignore
-rw-r--r-- 1 root root     22 Oct 26 11:19 .gitattributes
-rw-r--r-- 1 root root    245 Oct 26 11:19 .editorconfig
-rw-r--r-- 1 root root   2397 Oct 26 11:19 proxy.conf.js
-rw-r--r-- 1 root root   3008 Oct 26 11:19 package.json
-rw-r--r-- 1 root root 443221 Oct 26 11:19 package-lock.json
-rw-r--r-- 1 root root   2807 Oct 26 11:19 tslint.json
-rw-r--r-- 1 root root    408 Oct 26 11:19 tsconfig.json
drwxr-xr-x 5 root root   4096 Oct 26 11:19 src
drwxr-xr-x 5 root root   4096 Oct 26 11:19 .
drwxr-xr-x 8 root root   4096 Oct 26 11:19 .git
/var/lib/jenkins/workspace/test-_PR-33-3J4KAQ5TC4B5HJTBWWHJHBFJTERYSSF4MHFI3M4W4EQBPFQPLBPA/npmrc.template (No such file or directory)

签出后,移动到工作区目录(或相对于工作区根目录所需的任何位置,因为您可能有一个配置,其中签出位于子文件夹中(:

dir('${workspace}') {
sh 'pwd'
sh 'ls'
// Rest of the stage
}

ls调用应指示签出是否在此文件夹中完成,以及您是否确实在预期的位置拥有文件。

求解方式

script {
iFile = readFile file: "npmrc.template"
t = iFile.replaceAll('USERNAME', "${REGISTRY_LOGIN}")
t = iFile.replaceAll('BASE64_PASSWORD', "${REGISTRY_PWD}")
t = iFile.replaceAll('youremail@email.com', "${REGISTRY_LOGIN}")
writeFile file: ".npmrc", text: t
}

最新更新