简体   繁体   中英

environmentVariables() not working for Jenkins Job DSL and Pipeline Jobs

We use Jenkins Job DSL for our CI setup. Since we are using a special command only available in the traditional Jenkinsfile syntax, we need to use a pipeline job .

Inside of the pipeline job we check out our project from Git. We are using the pipeline job for multiple projects, so we want to inject the git url into the pipeline script.

This is a short version of our script generating the pipeline job:

def createPipelineJob(def jobName, def gitUrl) {
    pipelineJob(jobName) {
        environmentVariables(GIT_URL: gitUrl)
        definition {
            cps {
                script('''
                    node {
                        sh 'env | sort' 
                    }
                ''')
                sandbox(true)
            }
        }       
    }   
}

This creates the following XML config:

<flow-definition>
    <actions/>
    <description/>
    <keepDependencies>false</keepDependencies>
    <properties>
        <EnvInjectJobProperty>
            <info>
                <propertiesContent>GIT_URL=my-git.url</propertiesContent>
                <loadFilesFromMaster>false</loadFilesFromMaster>
            </info>
            <on>true</on>
            <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
            <keepBuildVariables>true</keepBuildVariables>
            <overrideBuildParameters>false</overrideBuildParameters>
            <contributors/>
        </EnvInjectJobProperty>
    </properties>
    <triggers/>
    <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition">
        <script>
            node { sh 'env | sort' }
        </script>
        <sandbox>true</sandbox>
    </definition>
</flow-definition>

But if i run this, the GIT_URL environment variable is not listed (other environment variables are). But if i instead create the pipeline job manually with this setup , the GIT_URL environment variable is printed just fine. Creating the job manually pretty much creates the same xml configuration:

<flow-definition plugin="workflow-job@2.15">
    <actions>
        <io.jenkins.blueocean.service.embedded.BlueOceanUrlAction plugin="blueocean-rest-impl@1.3.1">
            <blueOceanUrlObject class="io.jenkins.blueocean.service.embedded.BlueOceanUrlObjectImpl">
                <mappedUrl>blue/organizations/jenkins/test-jobname</mappedUrl>
                <modelObject class="flow-definition" reference="../../../.."/>
            </blueOceanUrlObject>
        </io.jenkins.blueocean.service.embedded.BlueOceanUrlAction>
    </actions>
    <description/>
    <keepDependencies>false</keepDependencies>
    <properties>
        <com.sonyericsson.rebuild.RebuildSettings plugin="rebuild@1.27">
            <autoRebuild>false</autoRebuild>
            <rebuildDisabled>false</rebuildDisabled>
        </com.sonyericsson.rebuild.RebuildSettings>
        <EnvInjectJobProperty plugin="envinject@2.1.5">
            <info>
                <propertiesContent>GIT_URL=my-git.url</propertiesContent>
                <secureGroovyScript plugin="script-security@1.35">
                <script/>
                <sandbox>false</sandbox>
            </secureGroovyScript>
            <loadFilesFromMaster>false</loadFilesFromMaster>
        </info>
        <on>true</on>
        <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
        <keepBuildVariables>true</keepBuildVariables>
        <overrideBuildParameters>false</overrideBuildParameters>
    </EnvInjectJobProperty>
        <org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
            <triggers/>
        </org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
    </properties>
    <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.41">
        <script>
            node { sh 'env | sort' }
        </script>
        <sandbox>true</sandbox>
    </definition>
    <triggers/>
    <disabled>false</disabled>
</flow-definition>

We are pretty lost because we are new to jenkins and this problem is holding us for days now.

Edit:

  • The job is generated on the jenkins master node but executed on a slave node
  • Jenkins 2.37.3
  • Environment Injector Plugin 2.1.5
  • Pipeline 2.5

This is more of a comment than an answer, but I modified and tested your DSL code and it works fine.

I created a DSL job using the script:

def createPipelineJob(def jobName, def gitUrl) {
    pipelineJob(jobName) {
        environmentVariables(GIT_URL: gitUrl)
        definition {
            cps {
                script('''
                    node {
                        sh "echo $GIT_URL" 
                    }
                ''')
                sandbox(true)
            }
        }       
    }   
}
createPipelineJob('new-job-2','my-git.url')

The resulting pipeline job has the same XML as the one you posted (minus the shell script), and building the pipeline job prints the value of GIT_URL.

[new-job-1] Running shell script
+ echo my-git.url
my-git.url

My recommendation:

  • If the short version you posted (or maybe try mine) doesn't work, I would try to see if upgrading Jenkins or the plugins makes any difference.
  • If the short version you posted or mine does work, maybe post the full version, perhaps there's an error there.

As it turns out the Environment Injector Plugin was not installed successfully, therefore the script did not run properly. So all i had to do was to restart Jenkins and everything worked just fine. Special thanks to Javier Garcés ensuring me that my script was indeed correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM