[JIRA] (JENKINS-43911) Environment variables can't be used in agent configuration

2 views
Skip to first unread message

calebmayeux@hotmail.com (JIRA)

unread,
Feb 21, 2018, 1:42:06 PM2/21/18
to jenkinsc...@googlegroups.com
Caleb Mayeux commented on Bug JENKINS-43911
 
Re: Environment variables can't be used in agent configuration

My use case is that I have a pipeline, and part of the way through it I check out a git repo, and set the latest git commit hash to a variable which is used to tag and push a docker image. Later on I have a stage that uses that image, and thus I need the hash to run the stage using an agent based on that image. The only workaround to do this is to take the stage that checks out the code out of the declarative pipeline and do it before. If there were other steps that had to happen before that, I'd have to take them out of the pipeline too, so at that point I'd be basically just not using declarative pipeline because of this issue.

 

I think being able to dynamically assign agents based on the pipeline is an important and powerful ability, and is something that exists in both scripted pipeline and freestyle jobs (you can assign node/label as a parameter for a freestyle job, and a pipeline of freestyle jobs effectively makes each job the equivalent of a "stage" in pipeline terminology).

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.3.0#73011-sha1:3c73d0e)
Atlassian logo

andrew.bayer@gmail.com (JIRA)

unread,
Apr 2, 2018, 3:53:04 PM4/2/18
to jenkinsc...@googlegroups.com

allan.lewis@youview.com (JIRA)

unread,
Oct 11, 2018, 5:31:02 AM10/11/18
to jenkinsc...@googlegroups.com
Allan Lewis commented on Bug JENKINS-43911
 
Re: Environment variables can't be used in agent configuration

I have a very similar use case to Caleb Mayeux.
This has been in progress for quite a while, can we please have a status update?

This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

allan.lewis@youview.com (JIRA)

unread,
Oct 18, 2018, 7:19:02 AM10/18/18
to jenkinsc...@googlegroups.com

I finally found a workaround for this:

registry = 'my-registry'
imageRepo = 'my-repo-name'

def getImageName(def doCheckout = false) {
  def treeHash = node {
    if (doCheckout) checkout scm
    sh(script: 'git rev-parse HEAD:', returnStdout: true).trim()
  }
  "$registry/$imageRepo:$treeHash"
}

pipeline {
  agent {
    label 'docker-builder'
  }

  stages {
    stage('Build & Push Docker Image') {
      steps {
        script {
          docker.build(getImageName()).push()
        }
      }
    }
    stage('Do Stuff') {
      agent {
        docker {
          image getImageName(true)
        }
      }
      steps {
        sh 'echo hello'
      }
    }
  }
}

andrew.bayer@gmail.com (JIRA)

unread,
Nov 15, 2018, 12:02:07 PM11/15/18
to jenkinsc...@googlegroups.com
Andrew Bayer updated an issue
 
Change By: Andrew Bayer
Labels: declarative-variable-and-method-resolution triaged-2018-11

eplotkin@drivenets.com (JIRA)

unread,
Dec 9, 2018, 5:40:03 AM12/9/18
to jenkinsc...@googlegroups.com
efo plo commented on Bug JENKINS-43911
 
Re: Environment variables can't be used in agent configuration

Would also like to have this implemented.

 

We need to dynamically assign a label based on branch, so that branches of Team A run on the resources of that team, etc. We contact Consul for the mapping of branch to label. Currently this is not possible as the label expression is evaluated before the pipeline is run, not at the beginning of the relevant step, so any changes to variables or environment do not affect the node where the step is run.

jack.pamely@gmail.com (JIRA)

unread,
Mar 21, 2019, 5:37:06 AM3/21/19
to jenkinsc...@googlegroups.com
Jack P commented on Bug JENKINS-43911

 I would like to bump interest in this as well recently got diverted here from posting a [similar question on stack overflow|https://stackoverflow.com/questions/55267427/how-can-i-use-different-private-docker-agents-based-on-parameter-in-jenkins-decl.]

 

The solution seems to mean restarting the same environment between stages which feels a bit off and results in me having to merge stages that would be nice to keep separate.

eplotkin@drivenets.com (JIRA)

unread,
Mar 21, 2019, 6:04:02 AM3/21/19
to jenkinsc...@googlegroups.com
efo plo commented on Bug JENKINS-43911

Jack P We worked around this limitation by implementing a scripted pipeline before declarative:

node('master') {
    stage('Choose Label') {
        LABEL = 'my_desired_label' // script it anyway you want
    }
}


pipeline {
    agent {
        node {
            label "${LABEL}"
        }
    }
// etc.
}

jack.pamely@gmail.com (JIRA)

unread,
Mar 21, 2019, 7:48:03 AM3/21/19
to jenkinsc...@googlegroups.com
Jack P commented on Bug JENKINS-43911

efo plo Thank you! I am eternally greatful. Totally works with docker and azure storage plugin, here's my stackoverflow example fixed as per yours.

 
 

node('master') { stage('Choose Label') { URL_VAR = "${env.registrySelection == "PROD" ? "urlProd.azure.io" : "urlTest.azure.io"}" CREDS_VAR = "${env.registrySelection == "PROD" ? "credsProd" : "credsTest"}" } } pipeline { parameters { choice( name: 'registrySelection', choices: ['TEST', 'PROD'], description: 'Is this a deployment to STAGING or PRODUCTION environment?' ) } agent { docker { image "${URL_VAR}/image:tag" registryUrl "https://${URL_VAR}" registryCredentialsId "${CREDS_VAR}" } } stages{ stage('test'){ steps{ echo "${URL_VAR}" echo "${CREDS_VAR}" } } } }

 

jack.pamely@gmail.com (JIRA)

unread,
Mar 21, 2019, 7:50:05 AM3/21/19
to jenkinsc...@googlegroups.com
Jack P edited a comment on Bug JENKINS-43911
[~eplodn1] Thank you! I am eternally greatful. Totally works with docker and azure storage plugin, here's my stackoverflow example fixed as per yours.

 
 
{code:java}
node('master') {

stage('Choose Label') {
URL_VAR = "${env.registrySelection == "PROD" ? "urlProd.azure.io" : "urlTest.azure.io"}"
CREDS_VAR = "${env.registrySelection == "PROD" ? "credsProd" : "credsTest"}"
}
}
pipeline {
parameters {
choice(
name: 'registrySelection',
choices: ['TEST', 'PROD'],
description: 'Is this a deployment to STAGING or PRODUCTION environment?' )
}
agent {
docker {
image "${URL_VAR}/image:tag"
registryUrl "https://${URL_VAR}" registryCredentialsId "${CREDS_VAR}"
}
}
stages{
stage('test'){
steps{
echo "${URL_VAR}"
echo "${CREDS_VAR}"
}
}
}
}{code}
 

jack.pamely@gmail.com (JIRA)

unread,
Mar 21, 2019, 7:51:03 AM3/21/19
to jenkinsc...@googlegroups.com

jack.pamely@gmail.com (JIRA)

unread,
Mar 21, 2019, 7:57:03 AM3/21/19
to jenkinsc...@googlegroups.com

bitwiseman@gmail.com (JIRA)

unread,
Aug 30, 2019, 2:57:05 PM8/30/19
to jenkinsc...@googlegroups.com
Liam Newman stopped work on Bug JENKINS-43911
 
Change By: Liam Newman
Status: In Progress Open
Reply all
Reply to author
Forward
0 new messages