Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-54286

Unable to use variable tokens as build property values in Create Package in Declarative Script

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Critical Critical
    • None
    • Jenkins 2.138.2
      BM plugin 2.0.2

      I have job in declarative script and in the create package I need to pass some variables and values but I cannot use Token variables here or at least I have been unable to find a way to use them.

      I've tried property=${varname}  ,  property =$varname  , property = "${varname}" and property = varname  but the values are just never replaced with the values of the variables.

      I also tried setting them to new variables within the script block and then using the above options and again it never replaces them.

      I can use tokens in the Release Number , app id, deployable id just fine but I cannot as buildvariables.  

      These are dynamic values so they cannot be hard coded.

      If there is already a way to do this you might want to note it in the docs. 

          [JENKINS-54286] Unable to use variable tokens as build property values in Create Package in Declarative Script

          Andrew Sumner added a comment - - edited

          I've released a new version as I didn't like the syntax for adding variables to a package - you'll need to install version 2.1.1 of the plugin.

          Syntax should be:  ... = buildMasterCreatePackage(..., packageVariables: [variables: "hello=world\nsource=$BUILD_URL"])

          Let me know how that goes. If this is not what you're after can you provide an example of what you're doing thanks.

          Also: I've just upgraded to BuildMaster 6.0.11 and there has been a change in the security model which is preventing access to the APIs - so this is just a friendly hint not to upgrade BuildMaster just yet 

          Andrew Sumner added a comment - - edited I've released a new version as I didn't like the syntax for adding variables to a package - you'll need to install version 2.1.1 of the plugin. Syntax should be:  ... = buildMasterCreatePackage(..., packageVariables: [variables: "hello=world\nsource=$BUILD_URL"] ) Let me know how that goes. If this is not what you're after can you provide an example of what you're doing thanks. Also: I've just upgraded to BuildMaster 6.0.11 and there has been a change in the security model which is preventing access to the APIs - so this is just a friendly hint not to upgrade BuildMaster just yet 

          Jon Starbird added a comment -

          andrewsumner as usual thanks for the quick turnaround!  I will test it a bit later today.

          One important note though, when I installed the update it wiped all the pre-existing variable settings for any existing Classic jobs. Fortunately on this server I only had two but on others it will be many more. Not sure if there is a way to migrate them or not for you, if not having the Warning message about changed formats in the updates area would be helpful.

          Jon Starbird added a comment - andrewsumner as usual thanks for the quick turnaround!  I will test it a bit later today. One important note though, when I installed the update it wiped all the pre-existing variable settings for any existing Classic jobs. Fortunately on this server I only had two but on others it will be many more. Not sure if there is a way to migrate them or not for you, if not having the Warning message about changed formats in the updates area would be helpful.

          Andrew Sumner added a comment -

          Unfortunately I could not find any way of updating the settings and had to change the format to support pipeline script. My only suggestion is editing the Jenkins job xml definition and manually to get it into the correct format. Not ideal I know.

          I'll try updating the compatibleSinceVersion property (it's currently set to 1.7, but should be 2.0) which might display a warning message for the upgrade. There is a warning on the Wiki but it is very easily overlooked.

          Andrew Sumner added a comment - Unfortunately I could not find any way of updating the settings and had to change the format to support pipeline script. My only suggestion is editing the Jenkins job xml definition and manually to get it into the correct format. Not ideal I know. I'll try updating the compatibleSinceVersion property (it's currently set to 1.7, but should be 2.0) which might display a warning message for the upgrade. There is a warning on the Wiki but it is very easily overlooked.

          Jon Starbird added a comment -

          Not working for me, maybe the format isn't right. I tried it with double quotes around the variables as well as the original method of triple single quotes and neither worked.

          Build_Number and Job_Name are both environment variables but the gitprops.gitversion_fullsemver is from importing the gitversion properties into the job earlier. I do use it elsewhere  just fine even here for the ReleaseNumber.

          Here is what I have currently:

          				buildMasterWithApplicationRelease(applicationId: "${env.BuildMaster_appId}", deployableId: "${env.BuildMaster_deployableId}") {
          					script {
          						BUILDMASTER_PACKAGE_NUMBER = buildMasterCreatePackage applicationId: BUILDMASTER_APPLICATION_ID, deployToFirstStage: true, packageNumber: BUILDMASTER_PACKAGE_NUMBER, releaseNumber: "${gitprops.GitVersion_MajorMinorPatch}", 
          						setBuildVariables: [
          							preserveVariables: false, 
          							variables: "JenkinsBuildNumber=$BUILD_NUMBER\nBuildVersion=$gitprops.GitVersion_FullSemVer\nJenkinsProject=$JOB_NAME"], 
          						waitTillBuildCompleted: [printLogOnFailure: false]			
          					}
          				}
          

           When it runs I get no errors on the Jenkins but on the BuildMaster it fails and doesn't show any values for the passed variables.

          Jon Starbird added a comment - Not working for me, maybe the format isn't right. I tried it with double quotes around the variables as well as the original method of triple single quotes and neither worked. Build_Number and Job_Name are both environment variables but the gitprops.gitversion_fullsemver is from importing the gitversion properties into the job earlier. I do use it elsewhere  just fine even here for the ReleaseNumber. Here is what I have currently: buildMasterWithApplicationRelease(applicationId: "${env.BuildMaster_appId}" , deployableId: "${env.BuildMaster_deployableId}" ) { script { BUILDMASTER_PACKAGE_NUMBER = buildMasterCreatePackage applicationId: BUILDMASTER_APPLICATION_ID, deployToFirstStage: true , packageNumber: BUILDMASTER_PACKAGE_NUMBER, releaseNumber: "${gitprops.GitVersion_MajorMinorPatch}" , setBuildVariables: [ preserveVariables: false , variables: "JenkinsBuildNumber=$BUILD_NUMBER\nBuildVersion=$gitprops.GitVersion_FullSemVer\nJenkinsProject=$JOB_NAME" ], waitTillBuildCompleted: [printLogOnFailure: false ] } }  When it runs I get no errors on the Jenkins but on the BuildMaster it fails and doesn't show any values for the passed variables.

          Andrew Sumner added a comment -

          $gitprops.GitVersion_FullSemVer probably needs to be ${$gitprops.GitVersion_FullSemVer}

          Can you turn on API logging in buildmaster section of Jenkins configuration page and reply back with the log output?

          How are you setting ${env.BuildMaster_appId}" and "${env.BuildMaster_deployableId}"

          I'm also concerned that the Jenkins job is succeeding when buildmaster is failing. That definitely should not happen. I'll try to replicate that tonight.

          Andrew Sumner added a comment - $gitprops.GitVersion_FullSemVer probably needs to be ${$gitprops.GitVersion_FullSemVer} Can you turn on API logging in buildmaster section of Jenkins configuration page and reply back with the log output? How are you setting ${env.BuildMaster_appId}" and "${env.BuildMaster_deployableId}" I'm also concerned that the Jenkins job is succeeding when buildmaster is failing. That definitely should not happen. I'll try to replicate that tonight.

          Jon Starbird added a comment -

          The appid and deployableid are being set at the top of the script in the environment section of the full script. These to values work though, it's getting to the app in BuildMaster.

          I will try the brackets around the git props and the API logging.

          I also should have been more clear, the Jenkins job does fail. I only meant I don't get errors about the script itself. It does execute the call to BuildMaster which fails with BuildMaster failing on the passed variables not being set and then the Jenkins job will fail at that stage.

          Jon Starbird added a comment - The appid and deployableid are being set at the top of the script in the environment section of the full script. These to values work though, it's getting to the app in BuildMaster. I will try the brackets around the git props and the API logging. I also should have been more clear, the Jenkins job does fail. I only meant I don't get errors about the script itself. It does execute the call to BuildMaster which fails with BuildMaster failing on the passed variables not being set and then the Jenkins job will fail at that stage.

          Jon Starbird added a comment -

          Just the change of the brackets didn't make a difference. I used it just like I posted above but with the ${gitprops.GitVersion_FullSemVer}  change.

          Here is the Jenkins log output for the entire Create Package stage, as you can see the app id and deployable id are set correctly.

          [Pipeline] { (Create Buildmaster package)
          [Pipeline] wrap
          14:29:59 [BuildMaster] Sending POST to http://ca-builder3:8181/api/releases
          14:29:59 [BuildMaster]   With application/x-www-form-urlencoded content:
          14:29:59 [BuildMaster]     key=xxx&applicationId=44&status=active
          14:29:59 [BuildMaster] Inject environment variable BUILDMASTER_APPLICATION_ID=44
          14:29:59 [BuildMaster] Inject environment variable BUILDMASTER_RELEASE_NUMBER=1.1.0
          14:29:59 [BuildMaster] Inject environment variable BUILDMASTER_DEPLOYABLE_ID=1058
          [Pipeline] {
          [Pipeline] script
          [Pipeline] {
          [Pipeline] buildMasterCreatePackage
          14:29:59 [BuildMaster] Sending GET to http://ca-builder3:8181/api/json/Applications_GetApplication?API_Key=xxx&Application_Id=44
          14:29:59 [BuildMaster] Create BuildMaster package
          14:29:59 [BuildMaster] Sending PUT to http://ca-builder3:8181/api/releases/packages/create
          14:29:59 [BuildMaster]   With application/x-www-form-urlencoded content:
          14:29:59 [BuildMaster]     key=xxx&applicationId=44&releaseNumber=1.1.0
          14:29:59 [BuildMaster] Wait for any active deployments to complete
          14:29:59 [BuildMaster] Sending POST to http://ca-builder3:8181/api/releases/packages/deployments
          14:29:59 [BuildMaster]   With application/x-www-form-urlencoded content:
          14:29:59 [BuildMaster]     key=xxx&applicationId=44&releaseNumber=1.1.0&status=pending
          14:29:59 [BuildMaster] Sending POST to http://ca-builder3:8181/api/releases/packages/deployments
          14:29:59 [BuildMaster]   With application/x-www-form-urlencoded content:
          14:29:59 [BuildMaster]     key=xxx&applicationId=44&releaseNumber=1.1.0&status=executing
          14:29:59 [BuildMaster] Deploy package to next stage
          14:29:59 [BuildMaster] Sending PUT to http://ca-builder3:8181/api/releases/packages/deploy
          14:29:59 [BuildMaster]   With application/x-www-form-urlencoded content:
          14:29:59 [BuildMaster]     key=xxx&applicationId=44&releaseNumber=1.1.0&packageNumber=181031006
          14:30:00 [BuildMaster] Wait till deployment completed
          14:30:00 [BuildMaster] Sending POST to http://ca-builder3:8181/api/releases/packages/deployments
          14:30:00 [BuildMaster]   With application/x-www-form-urlencoded content:
          14:30:00 [BuildMaster]     key=xxx&applicationId=44&releaseNumber=1.1.0&packageNumber=181031006&deploymentId=17131
          14:30:07 [BuildMaster] Sending POST to http://ca-builder3:8181/api/releases/packages/deployments
          14:30:07 [BuildMaster]   With application/x-www-form-urlencoded content:
          14:30:07 [BuildMaster]     key=xxx&applicationId=44&releaseNumber=1.1.0&packageNumber=181031006&deploymentId=17131
          

           

          Jon Starbird added a comment - Just the change of the brackets didn't make a difference. I used it just like I posted above but with the ${gitprops.GitVersion_FullSemVer}  change. Here is the Jenkins log output for the entire Create Package stage, as you can see the app id and deployable id are set correctly. [Pipeline] { (Create Buildmaster package ) [Pipeline] wrap 14:29:59 [BuildMaster] Sending POST to http: //ca-builder3:8181/api/releases 14:29:59 [BuildMaster] With application/x-www-form-urlencoded content: 14:29:59 [BuildMaster] key=xxx&applicationId=44&status=active 14:29:59 [BuildMaster] Inject environment variable BUILDMASTER_APPLICATION_ID=44 14:29:59 [BuildMaster] Inject environment variable BUILDMASTER_RELEASE_NUMBER=1.1.0 14:29:59 [BuildMaster] Inject environment variable BUILDMASTER_DEPLOYABLE_ID=1058 [Pipeline] { [Pipeline] script [Pipeline] { [Pipeline] buildMasterCreatePackage 14:29:59 [BuildMaster] Sending GET to http: //ca-builder3:8181/api/json/Applications_GetApplication?API_Key=xxx&Application_Id=44 14:29:59 [BuildMaster] Create BuildMaster package 14:29:59 [BuildMaster] Sending PUT to http: //ca-builder3:8181/api/releases/packages/create 14:29:59 [BuildMaster] With application/x-www-form-urlencoded content: 14:29:59 [BuildMaster] key=xxx&applicationId=44&releaseNumber=1.1.0 14:29:59 [BuildMaster] Wait for any active deployments to complete 14:29:59 [BuildMaster] Sending POST to http: //ca-builder3:8181/api/releases/packages/deployments 14:29:59 [BuildMaster] With application/x-www-form-urlencoded content: 14:29:59 [BuildMaster] key=xxx&applicationId=44&releaseNumber=1.1.0&status=pending 14:29:59 [BuildMaster] Sending POST to http: //ca-builder3:8181/api/releases/packages/deployments 14:29:59 [BuildMaster] With application/x-www-form-urlencoded content: 14:29:59 [BuildMaster] key=xxx&applicationId=44&releaseNumber=1.1.0&status=executing 14:29:59 [BuildMaster] Deploy package to next stage 14:29:59 [BuildMaster] Sending PUT to http: //ca-builder3:8181/api/releases/packages/deploy 14:29:59 [BuildMaster] With application/x-www-form-urlencoded content: 14:29:59 [BuildMaster] key=xxx&applicationId=44&releaseNumber=1.1.0&packageNumber=181031006 14:30:00 [BuildMaster] Wait till deployment completed 14:30:00 [BuildMaster] Sending POST to http: //ca-builder3:8181/api/releases/packages/deployments 14:30:00 [BuildMaster] With application/x-www-form-urlencoded content: 14:30:00 [BuildMaster] key=xxx&applicationId=44&releaseNumber=1.1.0&packageNumber=181031006&deploymentId=17131 14:30:07 [BuildMaster] Sending POST to http: //ca-builder3:8181/api/releases/packages/deployments 14:30:07 [BuildMaster] With application/x-www-form-urlencoded content: 14:30:07 [BuildMaster] key=xxx&applicationId=44&releaseNumber=1.1.0&packageNumber=181031006&deploymentId=17131  

          Andrew Sumner added a comment - - edited

          Not sure what's going on here. 

          Can you:

          1. Confirm which version of the plugin you are using. Ideally you'll need to upgrade to 2.1.1 if you're not already using this. Note that this version has renamed setBuildVariables from buildMasterCreatePackage to packageVariables.
          2. Echo out your gitprops variable and ensure it is populated correctly
          3. Show how gitprops is being populated

          I've created a sample deployment using jenkins buildmaster plugin 2.1.1 to attempt to duplicate your issue. As you can see in the log output below that the two variables (commit and fred) appear in the logs, but I cannot see the same for your output.  All I can think is that you're using an early release of the plugin that contains a bug or that somehow the variables not not getting passed to the plugin correctly.

           Code:

          node {
              scmVars = git(credentialsId: 'andrew-git', url: 'https://github.com/andrew-sumner/Accounts.git')
              echo "$scmVars"
              
              buildMasterWithApplicationRelease(applicationId: '2', packageNumberSource: 'BUILDMASTER') {
                  BUILDMASTER_PACKAGE_NUMBER = buildMasterCreatePackage(applicationId: BUILDMASTER_APPLICATION_ID, releaseNumber: BUILDMASTER_RELEASE_NUMBER, packageNumber: BUILDMASTER_PACKAGE_NUMBER, deployToFirstStage: true, waitTillBuildCompleted: [printLogOnFailure: true], packageVariables: [preserveVariables: false, variables: "fred=one\ncommit=${scmVars.GIT_COMMIT}"])
              }
          }
          

          Log:

           

          [Pipeline] echo (hide)
          [GIT_BRANCH:origin/master, GIT_COMMIT:b9f3e9741ea20e767ddbc7d6087ba7afc09221e7, GIT_LOCAL_BRANCH:master, GIT_PREVIOUS_COMMIT:b9f3e9741ea20e767ddbc7d6087ba7afc09221e7, GIT_PREVIOUS_SUCCESSFUL_COMMIT:b9f3e9741ea20e767ddbc7d6087ba7afc09221e7, GIT_URL:https://github.com/andrew-sumner/Accounts.git]
          [Pipeline] buildMasterWithApplicationRelease (hide)
          [BuildMaster] Sending POST to http://inedo:8622/api/releases
          [BuildMaster]   With application/x-www-form-urlencoded content:
          [BuildMaster]     key=xxx&applicationId=2&status=active
          [BuildMaster] Sending POST to http://inedo:8622/api/releases
          [BuildMaster]   With application/x-www-form-urlencoded content:
          [BuildMaster]     key=xxx&applicationId=2&releaseNumber=0.0.0
          [BuildMaster] Inject environment variable BUILDMASTER_APPLICATION_ID=2
          [BuildMaster] Inject environment variable BUILDMASTER_RELEASE_NUMBER=0.0.0
          [BuildMaster] Inject environment variable BUILDMASTER_PACKAGE_NUMBER=9, sourced from BuildMaster
          [Pipeline] { (hide)
          [Pipeline] buildMasterCreatePackage (hide)
          [BuildMaster] Sending GET to http://inedo:8622/api/json/Applications_GetApplication?API_Key=xxx&Application_Id=2
          [BuildMaster] Create BuildMaster package with PackageNumber=9
          [BuildMaster] Sending PUT to http://inedo:8622/api/releases/packages/create
          [BuildMaster]   With application/x-www-form-urlencoded content:
          [BuildMaster]     key=xxx&applicationId=2&releaseNumber=0.0.0&packageNumber=9&$commit=b9f3e9741ea20e767ddbc7d6087ba7afc09221e7&$fred=one
          [BuildMaster] Wait for any active deployments to complete

           

           

          Andrew Sumner added a comment - - edited Not sure what's going on here.  Can you: Confirm which version of the plugin you are using. Ideally you'll need to upgrade to 2.1.1 if you're not already using this. Note that this version has renamed  setBuildVariables from buildMasterCreatePackage to packageVariables. Echo out your gitprops variable and ensure it is populated correctly Show how  gitprops is being populated I've created a sample deployment using jenkins buildmaster plugin 2.1.1 to attempt to duplicate your issue.  As you can see in the log output below that the two variables (commit and fred) appear in the logs, but I cannot see the same for your output.  All I can think is that you're using an early release of the plugin that contains a bug or that somehow the variables not not getting passed to the plugin correctly.  Code: node { scmVars = git(credentialsId: 'andrew-git' , url: 'https: //github.com/andrew-sumner/Accounts.git' ) echo "$scmVars" buildMasterWithApplicationRelease(applicationId: '2' , packageNumberSource: 'BUILDMASTER' ) { BUILDMASTER_PACKAGE_NUMBER = buildMasterCreatePackage(applicationId: BUILDMASTER_APPLICATION_ID, releaseNumber: BUILDMASTER_RELEASE_NUMBER, packageNumber: BUILDMASTER_PACKAGE_NUMBER, deployToFirstStage: true , waitTillBuildCompleted: [printLogOnFailure: true ], packageVariables: [preserveVariables: false , variables: "fred=one\ncommit=${scmVars.GIT_COMMIT}" ]) } } Log:   [Pipeline] echo (hide) [GIT_BRANCH:origin/master, GIT_COMMIT:b9f3e9741ea20e767ddbc7d6087ba7afc09221e7, GIT_LOCAL_BRANCH:master, GIT_PREVIOUS_COMMIT:b9f3e9741ea20e767ddbc7d6087ba7afc09221e7, GIT_PREVIOUS_SUCCESSFUL_COMMIT:b9f3e9741ea20e767ddbc7d6087ba7afc09221e7, GIT_URL:https: //github.com/andrew-sumner/Accounts.git] [Pipeline] buildMasterWithApplicationRelease (hide) [BuildMaster] Sending POST to http: //inedo:8622/api/releases [BuildMaster]   With application/x-www-form-urlencoded content: [BuildMaster]     key=xxx&applicationId=2&status=active [BuildMaster] Sending POST to http: //inedo:8622/api/releases [BuildMaster]   With application/x-www-form-urlencoded content: [BuildMaster]     key=xxx&applicationId=2&releaseNumber=0.0.0 [BuildMaster] Inject environment variable BUILDMASTER_APPLICATION_ID=2 [BuildMaster] Inject environment variable BUILDMASTER_RELEASE_NUMBER=0.0.0 [BuildMaster] Inject environment variable BUILDMASTER_PACKAGE_NUMBER=9, sourced from BuildMaster [Pipeline] { (hide) [Pipeline] buildMasterCreatePackage (hide) [BuildMaster] Sending GET to http: //inedo:8622/api/json/Applications_GetApplication?API_Key=xxx&Application_Id=2 [BuildMaster] Create BuildMaster package with PackageNumber=9 [BuildMaster] Sending PUT to http: //inedo:8622/api/releases/packages/create [BuildMaster]   With application/x-www-form-urlencoded content: [BuildMaster]     key=xxx&applicationId=2&releaseNumber=0.0.0&packageNumber=9&$commit=b9f3e9741ea20e767ddbc7d6087ba7afc09221e7&$fred=one [BuildMaster] Wait for any active deployments to complete    

          Jon Starbird added a comment -

          I see the issue. I didn't catch the change from SetBuildVariables to  PackageVariables.

          I've updated it and now it is working. 

          Thanks! Sorry for the extra work but much appreciated. 

          Jon Starbird added a comment - I see the issue. I didn't catch the change from SetBuildVariables to  PackageVariables. I've updated it and now it is working.  Thanks! Sorry for the extra work but much appreciated. 

            andrewsumner Andrew Sumner
            jstarbird Jon Starbird
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: