0

I have below JSON file at %Workspace%\solution\config\appsettings.json

{
"appName": "Test",
"appId": "1",
"env" : "Test",
"url" : "https://url.com",
"client_id": "",
"client_secret": "",
"QAEmail" : "itteam@email.com",
"Preuri" : "https://preuri.com",
"Ravuri" : "https://Ravuri.com",
"Q&A"    : "https://QandA.com"
}

I won't be able to commit client id and client secret to git due to the security issues, but I need them to run my API test scripts through Jenkins.

So we need to alter the above json file to below one using environment variables. I have created two global credentials with a secret text CLIENT_ID and CLIENT_SECRET.

Can someone help me to write a windows batch command to replace the above json file with CLIENT_ID and CLIENT_SECRET?

For example if CLIENT_ID = 123456 and CLIENT_SECRET = 654321, the json file should be altered to following.

{
"appName": "Test",
"appId": "1",
"env" : "Test",
"url" : "https://url.com",
"client_id": "123456",
"client_secret": "654321",
"QAEmail" : "itteam@email.com",
"Preuri" : "https://preuri.com",
"Ravuri" : "https://Ravuri.com",
"Q&A"    : "https://QandA.com"
}

Any help would be appreciated.

  • Possible duplicate of [How to modify a key's value in a JSON file from command line](https://stackoverflow.com/questions/43292243/how-to-modify-a-keys-value-in-a-json-file-from-command-line) – Shlomi Bazel Aug 20 '18 at 18:02
  • shouldn't it be ```"Password" : "rock123"``` ? – timlg07 Aug 20 '18 at 20:13

3 Answers3

0

I wrote you a small script:

set "file=test.json"
set "tmpF=%TEMP%\appsettingsJSON_%time:~9,2%.json"
set "tab=    "
for /F "tokens=1* delims=: " %%V in (%file%) do if %%V=="client_id" ( echo %tab%%%V:"%CLIENT_ID%">>%tmpF% ) else ( if %%V=="client_secret" ( echo %tab%%%V:"%CLIENT_SECRET%">>%tmpF% ) else ( if [%%W]==[] ( echo %%V>>%tmpF% ) else ( echo %tab%%%V:%%W>>%tmpF% ) ) )
move "%tmpF%" "%file%"

worked for me, you have to replace "test.json" with the path to your json-file

timlg07
  • 547
  • 6
  • 20
  • I little bit changed my json file, will the same script work for the above json file too? –  Aug 21 '18 at 13:42
  • yes it still works. (But it still adds the tabs you had in your first example. The JSON is working the same way with the tabs (and is looking better), but if you want to remove them just set tab to an empty string ```set "tab="```) – timlg07 Aug 21 '18 at 14:12
  • can I use execute shell script in jenkins or do I need to install any plugin to run this script? i got below error, when i used execute shell script **C:\Program Files (x86)\Jenkins\workspace\AfsAutomation_Test>exit 0 [AfsAutomation_Test] $ sh -xe C:\Users\por\AppData\Local\Temp\jenkins6457885774024126472.sh The system cannot find the file specified Caused: java.io.IOException: Cannot run program "sh" (in directory "C:\Program Files (x86)\Jenkins\workspace\AfsAutomation_Test"): CreateProcess error=2, The system cannot find the file spec** –  Aug 21 '18 at 16:08
  • öhm... this is a batch-script. I found this question because of the tag "batch-file" and it had the title "How to make Changes to a json file using windows batch command in jenkins"... – timlg07 Aug 23 '18 at 10:15
0

Actually, I tried this way and worked like magic.

Step1: Replace your JSON file like below

{
"appName": "Test",
"appId": "1",
"env" : "Test",
"url" : "https://url.com",
"client_id": "CLIENT_ID_VALUE",
"client_secret": "CLIENT_SECRET_VALUE",
"QAEmail" : "itteam@email.com",
"Preuri" : "https://preuri.com",
"Ravuri" : "https://Ravuri.com",
"Q&A"    : "https://QandA.com"
}

Step 2: Install the following plugins to Jenkins

   **1)** Credentials plugin 

   **2)** Credential Binding Plugin

   **3)** Windows Power Shell Plugin

Step 3: Create the secret text and define your values Client_Id = "12345", Client_Secret = "54321" using Credentials Plugin(which you will find it on the left side of Jenkins, where you will create a new job). Now Create Environment variables "CLIENT_ID_VALUE" and "CLIENT_SECRET_VALUE" using Bindings Tab.

Step 4: Now add a step to execute Windows Power Shell and write the below script into it

(gc 'C:\Documents\application.json') -replace 'CLIENT_ID_VALUE', $env:CLIENT_ID_VALUE | Out-File 'C:\Documents\application.json'

(gc 'C:\Documents\application.json') -replace 'CLIENT_SECRET_VALUE', $env:CLIENT_SECRET_VALUE | Out-File
 'C:\Documents\application.json'
-3
  1. Get a suitable command-line editor (we use SED for this, available in CygWin, UnixUtils, GnuWin32, etc)

  2. Put recognizable placeholders in the places you want to edit:

{ "client_id": "@client@" "client_secret": "@secret@" "Environment" : "Test" "Username" : "rocky" "Password" " "rock123" }

  1. Pipe your files through your editor to replace your placeholders.
SharpDog
  • 10
  • 2