Search code examples
mavenjenkinsjenkins-pipelinepom.xml

How to get/fetch the environment variables set in Jenkins pipeline into our maven pom.xml


I have couple of Environment variables set using Jenkins pipeline.

enter image description here

I want to fetch the values of these environment variables in my maven pom.xml file, where I want to pass them as buildArgs to docker-maven-plugin, which will be again called in a dockerfile.

enter image description here

How to call those environment variables in my pom.xml?

I tried ${env.JENKINS_USER_NAME} as well as %JENKINS_USER_NAME% but nothing seem to work.

Use case: I will be running my Jenkins pipeline job to build my project, which will eventually create an docker image in Stage-1 and then run the docker container in Stage-2 (which will run the tests as well internally).

Problem: My Jenkins job can be triggered based on user selection of specific testng.xml file as show below:

enter image description here

When triggered once, it is working fine but if we triggered for second time without cleaning the workspace, then it is throwing error like

enter image description here

I am assuming that it might be a problem with permission, as I am mounting volume to map container test-output directory with Jenkins host-VM directory

enter image description here

How can I get the environment variables in my POM?


Solution

  • Define the values as maven properties (with defaults), then you can override them on the maven command line with properties from the Jenkins environment variable populated from the drop-down.

    pom.xml:

    <properties>
            <gid>default-gid</gid> 
            <uid>default-uid/<uid> 
            <uname>default-uname</uname> 
    </properties>
    
    <configuration>
        <buildArgs>
            <GID>${gid}</GID> 
            <UID>${uid}/<UID> 
            <UNAME>${uname}</UNAME> 
        </buildArgs> 
    </configuration>
    

    SH EXEC CMD:

    mvn -Dgid=my-gid -Duid=my-uid -Duname=my-uname clean system-test:latest ${TestSuiteFiles}