jenkinsci / plasticscm-plugin

A plugin for Jenkins to be able to use Plastic SCM
MIT License
16 stars 31 forks source link

PLASTIC_ environment variavbles are not set if cm is used from pipeline #26

Closed freename1 closed 4 years ago

freename1 commented 4 years ago

The environment variables that contain change set and author information arent set for workspaces that have been created in a pipeline script. Indexing like PLASTICSCM_1_CHANGESET may not work for scripts. If a parallel step is used the workspaces arent guaranteed to be created in the same order each time. A solution to this might be to create variable with names like PLASTICSCM_DIRECTORY_CHANGESET with DIRECTORY being the subdirectory the workspace has been created in.

mig42 commented 4 years ago

Hi @freename1,

How are you calling the cm command in the script? Please be aware that the populated environment variables are returned as the result value of the command. The cm command does not overwrite any global environment variables.

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('Build') {
   steps {
    echo 'Building..'
   }
  }

  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}");
     }
    }
   }
  }
 }
}

Could you please try saving the result of the cm command into an object and using it to retrieve the appropriate environment variable values? Please let us know if that works for you.

Thank you!

freename1 commented 4 years ago

Wow, this was quick and helpful! I couldnt find this trick mentioned in the plugins wiki page, may be I just missed it.

It seems plasticVars is local to the script block and is not available later in a second script block. Therefore I had to do MY_VAR = plasticVars .PLASTICSCM_CHANGESET_ID to preserve it.

mig42 commented 4 years ago

Happy to help!

Yes, we should probably add this to our documentation. It comes from how the Pipeline SCM Plugin defines its interface, we just follow the specification. There was a mention of this at some point in their docu, but I can't find it anymore 😔

Thank you so much for your feedback! We'll take note of the scope issue you mentioned as well.

I'm closing this issue since you already got it running, please feel free to open additional ones (or contact our support team) if you run into any other problems.

Cheers!

freename1 commented 4 years ago

For the next one needing this information: If the variable definition is done at the top of the file the scope is global. This way its possible to find a reasonable scope.

def plasticVars
pipeline {
...
plasticVars = cm branch: ...