Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 265
pablodcar Avatar asked May 18 '17 15:05

pablodcar


People also ask

How are environment variables used in declarative pipeline?

In declarative pipeline syntax, you do this in an environment block in the pipeline definition (Jenkinsfile). You can do this: at the top level, to define environment variables that will be shared across all pipeline stages. at the stage level, if you want to define environment variables local to a stage.

How do I set environment variable in Jenkins pipeline stage?

Setting Stage Level Environment Variable It is by using the env variable directly in the script block. We can define, let us say, USER_GROUP and display it. You will see that the underlying shell also has access to this environment variable. You can also set an environment variable using withEnv block.

How to print Jenkins Pipeline environment variables in logs?

The steps to do the same are : Create a new pipeline in Jenkins, named ‘ envvars ’. In the Pipeline Script, type the following groovy script. The windows batch command used here is “ set ”. This command lists all the Jenkins pipeline environment variables in logs. sh ‘printenv’ .

How to create a declarative style pipeline in Jenkins?

Step 1: Open Jenkins home page ( http://localhost:8080 in local) & click on New Item from the left side menu. Step 2: Enter Jenkins job name & choose the style as Pipeline & click OK. Step 3: Scroll down to the Pipeline section & copy-paste your first Declarative style Pipeline code from below to the script textbox.

How do I set variables in a declarative pipeline?

First, you need to initialise your variables. In declarative pipeline syntax, you do this in an environment block in the pipeline definition ( Jenkinsfile ). You can do this:

How to set global variables in Jenkins?

The global variables are set via the Jenkins console and via the groovy script of a pipeline. The ways to set these global environment variables are: Log in to the Jenkins Server first. On the upper left side, you will find a dropdown menu on the right of Jenkins; Select the dropdown menu.


3 Answers

This is definitely a bug with the declarative pipeline. You can track the issue related to this here: https://issues.jenkins-ci.org/browse/JENKINS-42369

If you move away from using the declarative pipeline and use the scripted pipelines instead, this won't occur, although your Jenkinsfile will be "wordier"

like image 110
Spencer Malone Avatar answered Oct 02 '22 21:10

Spencer Malone


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 .'
      }
    }
  }
}
like image 37
Max Paymar Avatar answered Oct 02 '22 20:10

Max Paymar


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()
        }
    }
like image 37
Karl Pickett Avatar answered Oct 02 '22 21:10

Karl Pickett