Jenkins pipeline bad replacement

A step in my pipeline uploads .tar to an artificial server. I get a "Basic replacement" error when passing to env.BUILD_NUMBER, but the same commands work when the number is hardcoded. The script is written to groovy through jenkins and runs in the jenkins workspace.

sh 'curl -v --user user:password --data-binary ${buildDir}package${env.BUILD_NUMBER}.tar -X PUT "http://artifactory.mydomain.com/artifactory/release-packages/package${env.BUILD_NUMBER}.tar"' 

returns errors:

 [Pipeline] sh [Package_Deploy_Pipeline] Running shell script /var/lib/jenkins/workspace/ Package_Deploy_Pipeline@tmp /durable-4c8b7958/script.sh: 2: /var/lib/jenkins/workspace/ Package_Deploy_Pipeline@tmp /durable-4c8b7958/script.sh: Bad substitution [Pipeline] } //node [Pipeline] Allocate node : End [Pipeline] End of Pipeline ERROR: script returned exit code 2 

If the hard code in the build number and the swap is ${env.BUILD_NUMBER} , I get no errors and the code works successfully.

 sh 'curl -v --user user:password --data-binary ${buildDir}package113.tar -X PUT "http://artifactory.mydomain.com/artifactory/release-packages/package113.tar"' 

I use $ {env.BUILD_NUMBER} in other sh commands within the same script and have no problems elsewhere.

+5
source share
5 answers

This turned out to be a syntax issue. Completing the command in ' caused ${env.BUILD_NUMBER to be passed instead of a value. I wrapped the whole team in " and avoided nesting. Now it works fine.

 sh "curl -v --user user:password --data-binary ${buildDir}package${env.BUILD_NUMBER}.tar -X PUT \"http://artifactory.mydomain.com/artifactory/release-packages/package${env.BUILD_NUMBER}.tar\"" 
+22
source

Actually, you seem to misunderstood the env variable. In the sh block, you must access ${BUILD_NUMBER} directly.

Reason / Explanation: env represents the environment inside the script. This environment is used / available directly for everything that is being executed, for example. shell scripts.

Also note that do not write anything env.* , But use withEnv{} blocks withEnv{} .

+6
source

Usually the most common problem for:

Poor replacement

the mistake is to use sh instead of bash .

Especially when using Jenkins, if you use the Execute shell, make sure your command starts with shebang , for example. #!/bin/bash -xe or #!/usr/bin/env bash .

+4
source

I can definitely tell you all about sh shell and bash shell. I fixed this problem by specifying #!/bin/bash -xe as follows:

 node { stage("Preparing"){ sh'''#!/bin/bash -xe colls=( col1 col2 col3 ) for eachCol in ${colls[@]} do echo $eachCol done ''' } } 
+4
source

I am having trouble displaying {env.MAJOR_VERSION} in the artifactory jar file. show that I fit by saving the environment step in a Jenkins file.

 pipeline { agent any environment { MAJOR_VERSION = 1 } stages { stage('build') { steps { sh 'ant -f build.xml -v' } } } post { always{ archiveArtifacts artifacts: 'dist/*.jar', fingerprint: true } } } 

I solved the problem, and then it did not show me a bad replacement in my release of the Jenkins build. therefore, a step in the environment plays a large role in the Jenkins file.

0
source

All Articles