0

I'm over this for a few minutes and I can't figure out why jenkins isn't able to find npmrc.template file when the file currently exists at the repository:

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})")
        }
    }
}

Output:

jenkins output:

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)

What I could be missing?

EDit: as sugested, init stage modified to:

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:'*'
    }
}

Output:

[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)
thclpr
  • 5,778
  • 10
  • 54
  • 87

2 Answers2

1

After the checkout, move to the workspace directory (or wherever you need relatively to the workspace root, as you may have a config where the checkout is in a subfolder):

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

The ls call should indicate if the checkout was done in this folder and if indeed you have the file where you expect it.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • 1
    The problems was the sandbox execution, after i changed the logic for a declarative pipeline friendly method, it worked. – thclpr Oct 31 '18 at 15:20
0

Solved with

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
}
thclpr
  • 5,778
  • 10
  • 54
  • 87