Jump to content

Environment Variables null in Jenkins pipelines even after successful connection


lrg_steven

Recommended Posts

The environment variables listed on the Jenkins plugin page return null for me in pipelines (https://plugins.jenkins.io/plasticscm-plugin/#documentation). They are null in both the stage where I do my cm call and other stages. I'm new to Jenkins Pipelines so I'm not sure if there is something simple that I'm missing.

 

        stage('Update Repository') {
            steps {
                cm(
					branch: '/main',
					repository: "${PLASTICSCM_TARGET_REPOSITORY}",
					server: 'mycompany@cloud',
					cleanup: 'STANDARD',
					directory: 'win64'
				)
				echo "${env.PLASTICSCM_CHANGESET_GUID}"
				echo "${env.PLASTICSCM_AUTHOR}"
            }
        }
		stage('Check variables') {
			steps {
				script {
					echo "${env.PLASTICSCM_CHANGESET_GUID}"
					echo "${env.PLASTICSCM_AUTHOR}"
				}
			}
		}

 

Link to comment
Share on other sites

Hi,

Are you following the steps from the plugin documentation?

https://plugins.jenkins.io/plasticscm-plugin/

Pipelines

Please have in mind that running the cm command in a pipeline script won't automatically set the environment variables above! This command, as do all VCS commands, returns a dictionary that contains the environment values set as expected.

To take advantage of that, you should do something like this:

pipeline {
  agent any

  environment {
    PLASTICSCM_WORKSPACE_NAME = "${env.JOB_BASE_NAME}_${env.BUILD_NUMBER}"
    PLASTICSCM_TARGET_SERVER = "192.168.1.73:8087"
    PLASTICSCM_TARGET_REPOSITORY = "default"
  }

  stages {
    stage('SCM Checkout') {
      steps {
        script {
          def plasticVars = cm(
            branch: "main",
            changelog: true,
            repository: env.PLASTICSCM_TARGET_REPOSITORY,
            server: env.PLASTICSCM_TARGET_SERVER,
            useUpdate: false
          )

          plasticVars.each {
            key, value -> println("${key} = ${value}")
          }
        }
      }
    }
  }
}

In the code above, the plasticVars dictionary would only be available in the script block inside the 'SCM Checkout' stage. If you'd like access it across different scripts, steps or stages, you can define the variable in the global scope:

def plasticVars

pipeline {
  agent any

  environment {
    PLASTICSCM_WORKSPACE_NAME = "${env.JOB_BASE_NAME}_${env.BUILD_NUMBER}"
    PLASTICSCM_TARGET_SERVER = "192.168.1.73:8087"
    PLASTICSCM_TARGET_REPOSITORY = "default"
  }

  stages {
    stage('SCM Checkout') {
      steps {
        script {
          plasticVars = cm(
            branch: "main",
            changelog: true,
            repository: env.PLASTICSCM_TARGET_REPOSITORY,
            server: env.PLASTICSCM_TARGET_SERVER,
            useUpdate: false
          )
        }

        script {
          plasticVars.each {
            key, value -> println("${key} = ${value}")
          }
        }
      }
    }
  }
}

 

Regards,

Carlos.

Link to comment
Share on other sites

I believe I was using it incorrectly. I read "If the checkout operation succeeds, these environment variables will be populated with the appropriate values for the build" and assumed it meant that existing environment variables would be populated. I was not fetching them from the dictionary.

Thanks for the help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...