I am using the CI/CD capabilities of Azure DevOps in order to build our micro-services.
One of the tasks I have inside the pipeline is a Bash Task that assign & execute variable declaration for further use in the same job.
When trying to gather the variable after the declaration I'm getting the output {.
The file I'm trying to read its data assigned to a variable is a JSON file, contains a JSON formatted data.
The file called pipeline_1.json, just to make sure that its inside the folder I need before trying to read it.
That is the command that takes the output to the variable.
And finally the output.
My insight was its printing only the first line of the JSON file because its formatted but when tried to format it as simple text it gives the same results
Based on your description, it seems that you need to get the value in the Json file.
You can try to use the following bash script:
For example:
Json file:
{
"AA": {
"BB": "TEST",
"CC": "XXX"
}
}
Bash script:
token1=($(jq -r '.AA.BB' pipeline_1.json))
echo "##vso[task.setvariable variable=token1;]$token1"
Then you can get the correct value in the json file.
Related
So inside of my terminal, I created a text file inside a directory (cat > fnames.txt). My initial goal was to write some data into said file. After creating fnames.txt, the following information showed up after trying to append data to the file using (cat >> fnames.txt):
cmdlet Get-Content at command pipeline position 1
Supply values for the following parameters:
Path[0]:
Image of terminal
Does anyone know the reason of this and what it means?
The command cat is in Powershell an alias to Get-Content. The cmdlet reads information from somewhere. The error message means that Get-Content does not have an idea from where you want it to get data, so it asks you.
I have to send buildId value from powershell script to the jenkinsfile pipeline. I am trying something like below. Is this right approach?
powershell script
if($out -match "buildId-") {
$splitline = $out.Split("-")
echo "splitline: " $splitline
$buildId= $splitline[1]
echo "buildId: " $buildId
$buildIds= $env:$buildId.Value
}
then want to use that build to pass as a parameter to trigger the build job.
Jenkinsfile
build job: 'build_Test', parameters: [validatingString(name: 'buildId', value: '$buildIds'), string(name: 'TASK', value: 'build')]
You can give it a shot, but I'm not sure simply adding it to an environment variable will allow you to access the value from the Jenkinsfile. You can find more about this problem in this question. However, you maybe able to achieve your goal via the EnvInject plugin, or simply store the value in a file in your workspace and access it via Jenkins later.
There are similar questions to this one on Stackoverflow, but none of them addressing my issue in using Terraform when an Azure Storage account is used to retain outputs.
Here is my scenario, which may sound familiar:
My terraform script provisions an Azure HTTP-triggered function with a function key. Also, this terraform script provisions a Web App that calls the mentioned HTTP-triggered function. The HTTP-triggered function's key is stored in the Web App's appsettings.json file to include it in the HTTP headers' calls to the HTTP-triggered function.
The following code snippet illustrates how the HTTP-triggered function is provisioned in terraform:
resource "azurerm_function_app" "myhttpfunc" {
name = var.funcname
location = "${azurerm_resource_group.rc.location}"
resource_group_name = "${azurerm_resource_group.rc.name}"
app_service_plan_id = "${azurerm_app_service_plan.funcsserviceplan.id}"
storage_account_name = "${azurerm_storage_account.funcstorage.name}"
storage_account_access_key = "${azurerm_storage_account.funcstorage.primary_access_key}"
}
The output variables to access the function's keys is per below:
output "funchostkeys" {
value = data.azurerm_function_app_host_keys.myhttpfunc
sensitive = true
}
I assume that this output and others will appear in terraform.tfstate hosted on the dedicated Azure Storage account at some point down the road.
My question is that how one can get that particular output in an Azure Release pipeline to manipulate the appsettings.json file by replacing the configuration entry with what terraform has produced in the output variable?
For example this post suggests producing terraform's output to a file per the following line but that does not seem to be an option in my case because my terraform script leverages Azure storage to retain outputs.
terraform output -json > outputs.json
Based on your requirement, you could try to output the variable with the following command:
terraform output -raw funchostkeys
Then you could use the logging command to set the value as pipeline variable.
For example: Powershell Script
- powershell: |
echo ##vso[task.setvariable variable=varname]$(terraform output -raw funchostkeys)
displayName: 'PowerShell Script'
Then you could use the Replace token task to use the pipeline variable to replace the value in appsettings.json.
You could define #{variablename}# in appsettings.json. When you run the pipeline, the output value will be set in the target place.
For a number of reasons, it would be really useful if I could create a file from a Jenkins pipeline and put it in my workspace. If I can do this, I could avoid pulling in some repositories where I'm currently pulling them in for just one or two files, keep those files in a maintainable place, and I could also use this to create temporary powershell scripts, working around a limitation of the solution described in https://stackoverflow.com/a/42576572
This might be possible through a Pipeline utility, although https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/ doesn't list any such utility; or it might be possible using a batch script - as long as that can be passed in as a string
You can do something like that:
node (''){
stage('test'){
bat """
echo "something" > file.txt
"""
String out = readFile(file.txt).trim()
print out // prints variable out groovy style
out.useFunction() // allows running functions loaded from the file
bat "type %out%" // batch closure can access the variable
}
}
I would like to capture the output of some variables to be used elsewhere in the job using Jenkins Powershell plugin.
Is this possible?
My goal is to build the latest tag somehow and the powershell script was meant to achieve that, outputing to a text file would not help and environment variables can't be used because the process is seemingly forked unfortunately
Besides EnvInject the another common approach for sharing data between build steps is to store results in files located at job workspace.
The idea is to skip using environment variables altogether and just write/read files.
It seems that the only solution is to combine with EnvInject plugin. You can create a text file with key value pairs from powershell then export them into the build using the EnvInject plugin.
You should make the workspace persistant for this job , then you can save the data you need to file. Other jobs can then access this persistant workspace or use it as their own as long as they are on the same node.
Another option would be to use jenkins built in artifact retention, at the end of the jobs configure page there will be an option to retain files specified by a match (e.g *.xml or last_build_number). These are then given a specific address that can be used by other jobs regardless of which node they are on , the address can be on the master or the node IIRC.
For the simple case of wanting to read a single object from Powershell you can convert it to a JSON string in Powershell and then convert it back in Groovy. Here's an example:
def pathsJSON = powershell(returnStdout: true, script: "ConvertTo-Json ((Get-ChildItem -Path *.txt) | select -Property Name)");
def paths = [];
if(pathsJSON != '') {
paths = readJSON text: pathsJSON
}