I'm trying to create a Azure DevOps pipeline for one of Java application using maven and in the unit test I have 2 secrets which needs to access the value from environment variables.
String key = System.getenv().get("MY_KEY");
String secret = System.getenv().get("MY_SECRET");
I have added 2 values under pipeline variables from the browser and mark them as "Keep this value secret" so that values will be kept as secret.
In the pipeline I have added required variables and substitute the value with configured variables.
variables:
MY_KEY: $(MY_KEY_VAL)
MY_SECRET: $(MY_SECRET_VAL)
However, when I run the build pipeline, it fails with unit test due to null values on both 'key' and 'secret' values.
How can I fix this? Am I doing anything wrong on the configurations?
UPDATE
As Ben mentioned below, it is not possible to set environment variable on hosted agents. In order to run all unit tests during the build pipeline and still keep the key & secret secure, I have modified the unit tests to read the key and secret from a file on the project root. Instead adding the file containing key & secret to git repo, it was added as a Secure File on the pipeline library and was given permission to access by pipeline.
On the Pipeline I was able to download the secure file using 'DownloadSecureFile' task.
- task: DownloadSecureFile@1
displayName: Download API secrets
name: configfile
inputs:
secureFile: 'service_api_keys'
Moved the file to project root using bash.
- bash: mv $(configfile.secureFilePath) ./
Run the build the process which will read the secrets on unit test execution.