1

I'm trying to inject an environment variable at build step Invoke Maven whose value was set at pre-build step through Execute Shell

    #!/bin/bash
    ipAddressHub=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' selenium-hub)
    echo $ipAddressHub
    echo 'ipAddress=$ipAddressHub' > ipAddress.properties

Now I want to fetch the value of ipAddress stored in ipAddress.properties. I'm using Inject environment variables after Execute Shell and provide ipAddress.properties in Properties File Path field (not sure if that's the right way) and then i use build step Invoke Maven Artifactory and provide the command below.

   clean install -DipAddress=${ipAddressHub} -Denv=${env} -Durl=${appURL} -DserverIP=${ipAddress}

But i don't get the value in serverIP, instead i get ${ipAddressHub} in console. I know i'm making some mistake, can anybody point out what's the correct way?

Nilamber Singh
  • 804
  • 1
  • 14
  • 33
  • 1
    What does the build's Console Log say about the inject step? Did you provide the proper `Properties File Path`: `$WORKSPACE/ipAddress.properties`, as described in the answer to [Jenkins inject environment variable](https://stackoverflow.com/a/31241715/1744774)? – Gerold Broser Apr 04 '19 at 10:03
  • I tried this as well, console says variable injected successfully, but i don't seem to get the value of `ipAddressHub` assigned to `serverIP` in mvn command. It still prints the same thing i.e `${ipAddressHub}` in logs – Nilamber Singh Apr 04 '19 at 10:26

2 Answers2

0

Did some browsing and found an answer to it.

You can embed variables only in double-quoted strings. So the problem was

echo 'ipAddress=$ipAddressHub' > ipAddress.properties

changed it to

echo 'ipAddress='"$ipAddressHub"' > ipAddress.properties

And it worked like a charm

Nilamber Singh
  • 804
  • 1
  • 14
  • 33
0

I hadn't used the plugin (at least not for a while), and I was going to suggest that you are just referencing it incorrectly?

I believe if you are adding it as an environment variable (and you can check it is adding by clicking on Environment Variables on the left side of the build screen).

You should be able to reference it like below?

${env.ipAddressHub}

This is untested though. Just going from memory.

hhami
  • 176
  • 10