Search code examples
dockerjenkinsenvironment-variablesjenkins-pipeline

How to use docker image internal environment variable in Jenkinsfile?


I'm currently facing the problem that Jenkins tries to access/replace the env variable in a sh script from outside, while the property is defined inside the container:

    stage('Run phpunit') {
      agent {
        docker { image 'php:7.2-alpine' }
      }
      steps {
        sh """
          apk add --no-cache ${PHPIZE_DEPS}
          pecl install xdebug
          docker-php-ext-enable xdebug
          php vendor/bin/phpunit --colors=never --log-junit build/junit.xml --coverage-clover build/clover.xml
        """
      }
    }

Jenkins fails this step with the message:

groovy.lang.MissingPropertyException: No such property: PHPIZE_DEPS for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:242)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)

Solution

  • The problem is that Groovy uses the same "${VAR}" syntax that you'd like to be interpreted by Bash. Just escape the $ with a \ and it will work:

    sh """ apk add --no-cache \${PHPIZE_DEPS} ...