Override secrets in settings.xml at runtime
Use the following steps to override secrets in a Maven settings.xml file by modifying the Build stage settings when the pipeline runs.
These steps assume you have an understanding of the Harness Secret Manager or that you know how to add your own secrets manager. You should also be familiar with adding text secrets, adding file secrets, and adding SSH secrets.
Create a secret at the account scope
Create a text secret at the account scope that contains the content of your settings.xml
file.
You need permissions to create, edit, and view secrets at the account scope to be able to do this. For more information, go to the Permission Reference.
Go to Account Settings, select Account Resources, and then select Secrets.
Select New Secret, and then select Text.
Enter a Secret Name, such as
settingsXML
. Take note of the ID. You need it to reference the secret in your pipeline.In Secret Value, paste the XML settings content from your
settings.xml
file.Select Save.
Transcribe the text secret into settings.xml
Create a new settings.xml
file in the Harness working directory (/harness
) and include a command in your pipeline to assign the value of your settings XML text secret to that file. To do this, modify the Run step or Run Tests step where your Maven tests run.
- Run step
- Run Tests step
If your Maven tests run in a Run step, add the following to the Command:
echo '<+secrets.getValue("account.[settingsXMLSecretID]")>' > settings.xml
If your Maven tests run in a Run Tests step, add the following to the Pre-Command:
echo '<+secrets.getValue("account.settingsXML")>' > settings.xml
Modify the Maven test command
Once the settings.xml
file exists in the Harness working directory, Maven can read your text secret from this file if you run mvn test
with the -s
flag. For example:
mvn test -s settings.xml
Optional: Use the .m2 directory
If your settings.xml
file is in the ~/.m2/
directory, Maven can read the secrets from the default location and you don't need to run mvn test
with -s
flag.
For example, if you can use the following command to transcribe the text secret to ~/.m2/
:
echo '<+secrets.getValue("account.settingsXML")>' > ~/.m2/settings.xml
And then you only need mvn test
to run the test.