How to use an environment variable in the agent section of a Jenkins Declarative Pipeline?

Viewed 8640

I'm building a Docker image for an application based in node.js where some of the dependencies requires an NPM token for a private NPM registry, but when building the image the variable containing the token is null, e.g.

docker build -t 3273e0bfe8dd329a96070382c1c554454ca91f96 --build-args NPM_TOKEN=null -f Dockerfile

a simplified pipeline is:

pipeline {

  environment {
    NPM_TOKEN = credentials('npm-token')
  }

  agent {
    dockerfile {
      additionalBuildArgs "--build-args NPM_TOKEN=${env.NPM_TOKEN}"
    }
  }

  stages {
    stage('Lint') { 
      steps { 
        sh 'npm run lint' 
      }
    }
  }

}

Is there a way to use the env variable in that section or it is not currently supported?

BTW, I've followed the suggestions in Docker and private modules related to how to use a NPM token to build a docker image

3 Answers

found a solution for this. Use credentials manager to add NPM_TOKEN. Then you can do

pipeline {
  agent {
    docker {
      image 'node:latest'
      args '-e NPM_TOKEN=$NPM_TOKEN'
    }

  }
  stages {
    stage('npm install') {
      steps {
        sh 'npm install'
      }
    }
    stage('static code analysis') {
      steps {
        sh 'npx eslint .'
      }
    }
  }
}

I came up with a workaround for this and it still uses declarative pipeline. I'm using this technique to download private github repos with pip.

// Workarounds for https://issues.jenkins-ci.org/browse/JENKINS-42369
// Warning: The secret will show up in your build log, and possibly be in your docker image history as well.
// Don't use this if you have a super-confidential codebase

def get_credential(name) {
  def v;
  withCredentials([[$class: 'StringBinding', credentialsId: name, variable: 'foo']]) {
      v = env.foo;
  }
  return v
}

def get_additional_build_args() {
    return "--build-arg GITHUB_ACCESS_TOKEN=" + get_credential("mysecretid")
}


pipeline {
    agent {
        dockerfile {
            filename 'Dockerfile.test'
            additionalBuildArgs get_additional_build_args()
        }
    }
Related