Search code examples
jenkinsgroovyjenkins-pipelinejenkins-job-dsljenkins-groovy

jenkins dsl job script: How to access environment variable, (which is injected via propertiesFile, ) in downstreamParameterized step


my dsl job script in brief

job('test') {
  steps {
    shell('echo VERSION=$VERSION > version.txt\n' +
          'echo VERSION_SUFFIX=$VERSION_SUFFIX >> version.txt\n' + 
          'echo GROUP_ID=$GROUP_ID >> version.txt')

    // EnvInject Plugin 
    environmentVariables {
      propertiesFile('version.txt')
    }
  }

  publishers {
    postBuildScripts {
        steps {
            shell('echo ${VERSION}')
        }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(false)
    }
    downstreamParameterized {
      trigger('next-job') {
        parameters {
          predefinedProp('relVersion', '${VERSION}')
        }
      }
    }
  }  
}

I need $VERSION number to pass to the downstream job a parameter.

I tried, ${env.VERSION} and also tried many options, but i couldn't catch the VERSION . any help is appreciated, thanks in advance.


Solution

  • You can use the option Prepare an environment for the run which is executed before SCM.

    Option Prepare an environment for the run is not belongs pre-build/ build /post build, but job properties.

    There is no job DSL API supports to configure this option. But we can use configure block.

    job('next-job') {
    
      configure { project -> 
        project / 'properties' << 'EnvInjectJobProperty' {
    
            info {
              loadFilesFromMaster false
              propertiesContent 'Branch=${relVersion}'
            }
            keepBuildVariables true
            keepJenkinsSystemVariables true
            overrideBuildParameters false
            on true
        }
      } // end of configure block
    
      scm { 
        git { 
          remote { 
            url("ssh://git@bitbucket.rl.git") 
          } 
            branches('${branch}') 
        } 
      } // end of scm
    
      steps {}
      publishers {}
    }
    

    Above job DSL can generate following xml as the content of seed job's config.xml

    <project>
        <actions></actions>
        <description></description>
        <keepDependencies>false</keepDependencies>
        <properties>
            <EnvInjectJobProperty>
                <info>
                    <loadFilesFromMaster>false</loadFilesFromMaster>
                    <propertiesContent>Branch=${relVersion}</propertiesContent>
                </info>
                <keepBuildVariables>true</keepBuildVariables>
                <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
                <overrideBuildParameters>false</overrideBuildParameters>
                <on>true</on>
            </EnvInjectJobProperty>
        </properties>
        <canRoam>true</canRoam>
        <disabled>false</disabled>
        <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
        <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
        <triggers></triggers>
        <concurrentBuild>false</concurrentBuild>
        <builders></builders>
        <publishers></publishers>
        <buildWrappers></buildWrappers>
        <scm class='hudson.plugins.git.GitSCM'>
            <userRemoteConfigs>
                <hudson.plugins.git.UserRemoteConfig>
                    <url>ssh://git@bitbucket.rl.git</url>
                </hudson.plugins.git.UserRemoteConfig>
            </userRemoteConfigs>
            <branches>
                <hudson.plugins.git.BranchSpec>
                    <name>${branch}</name>
                </hudson.plugins.git.BranchSpec>
            </branches>
            <configVersion>2</configVersion>
            <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
            <gitTool>Default</gitTool>
        </scm>
    </project>
    

    You can try jod DSL on http://job-dsl.herokuapp.com/ to check generated xml from it as expect or not.