Reputation: 1118
I have created a build pipeline for Azure data factory in DevOps which will fetch the files from an azure repo.
Before deploying the changes i would like to replace few string values in some of the files located in the build artifact.
Below mentioned are the steps i followed.
Below is the screenshot for repository structure.
After publishing the artifact in the build pipeline, below is the structure of published artifact in the pipeline.
I have added a sample shell script to the Agent job which is already in the artifact, as shown below
while running the release I am getting the below error regarding the path as shown below
How to update the files in the artifact by replacing the string values, using power shell. Does anyone have an idea how to point to the files in the artifact and run a replace script. If you could share a sample replace script by selecting the file name, it will be helpful.
Upvotes: 2
Views: 20914
Reputation: 30363
The above error you see in the powershell task in your release pipeline is because you pointed the script path
to a .sh1
file, while powershell task is expecting a .ps1
file. Even though the path value you specified in the script path
field is correct, powershell task will still fail with error if the file is not a .ps1 file.
If the script is bash script, you should use Bash task instead of powershell task.
You can check out below simple example in powershell script to replace a json value in artifacts file. See this thread.
$json = Convertfrom-json (get-content "_Source_alias\artifactsName\arm_template.json")
$json.parameter.value = $(PipelineVariables)
ConvertTo-Json $json -Depth 4 | Out-File _Source_alias\artifactsName\arm_template.json -Force
If you deploy arm template using ARM template deployment task. This task allows you to replace the template parameters by configuring the Template parameters
field and Override template parameters
field. See below:
Upvotes: 2
Reputation: 76920
How to update the files in the artifact by replacing the string values, using power shell. Does anyone have an idea how to point to the files in the artifact and run a replace script.
There is a great extension Replace Tokens to achieve your request.
With this task, you could directly select the file/files you want to replace the strings:
We just need to replace the variables in .json file with format #{VarValue1}#
in the repo:
like:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccounts_leoteststorageaccount_name": {
"defaultValue": "#{TestValue}#",
"type": "String"
}
},
Then define the key's values on the Variables tab:
The test result is:
This method is very simple and does not need to consider the file path, you can try it.
Upvotes: 2
Reputation: 40869
Inside the shell script I am not able to go to the repo file location to replace the string value. How to redirect to the root folder of the repo from the shell script file?
Please use workingDirectory to navigate to where you want to have your script executed.
- task: PowerShell@2
inputs:
filePath: '$(System.DefaultWorkingDirectory)\scripts\script.ps1'
workingDirectory: '$(System.DefaultWorkingDirectory)\templates\'
What you need o remember is go give fully qualified location for you script and set working directory for a directory where your arm files are.
If you are looking for other methods you can always consider creating paramaters in your arm templates and instead replacing values just pass them as those paramaetrs.
Is the above mentioned steps are correct for replacing a string value or do we need to publish the build first and then replace file from the artifact?
It is all up to you. You can replace strings in arm files before creating artifact or on release pipeline before running them. The questions you may ask yourself:
However, please consider adding paramaters. Maybe this is the best way.
Upvotes: 0