I’m trying to set the variable for recordId which I’m using different stages but unable get the newRecordId , on stage tfvalidate
, printing id as null Record ID:
PUT Response: {"detail":"Method Not Allowed"}.
Stage updatestatus is working as expected Please let me know if I missed something, assist me to resolve this issue.
trigger: none
#########################
# Declare Build Agents:-
#########################
pool:
vmImage: ubuntu-latest
######################
#DECLARE PARAMETERS:-
######################
parameters:
- name: TicketNumber
displayName: Please Provide the Number:-
type: object
###################
# Declare Stages:-
###################
stages:
- stage: UpdateStatus
jobs:
- job: UpdateTriggeredStatus
steps:
- script: |
# Send "triggered" status to the API endpoint
response=$(curl -s -X GET "http://amazonaws.com:800/ticket_details/?TicketNumber=${{ parameters.TicketNumber }}")
# Extract the record ID and other details from the response
record=$(echo "$response" | jq -r '.data[] | select(.TicketNumber == "'${{ parameters.TicketNumber }}'")')
recordId=$(echo "$record" | jq -r '._id')
echo "Record ID: $recordId"
# Set the recordId as a pipeline variable
echo "##vso[task.setvariable variable=recordId;isOutput=true]$recordId"
# Validate that the record was found
if [ -z "$recordId" ]; then
echo "No record found for TicketNumber: ${{ parameters.TicketNumber }}"
exit 1
fi
# Send "triggered" status to the API endpoint using PUT request
echo "Updating record with ID: $recordId"
putResponse=$(curl -s -X PUT -H "Content-Type: application/json" -d '{
"TicketNumber": "${{ parameters.TicketNumber }}",
"TicketSysID": "${{ parameters.TicketSysID }}",
"Status": "Triggered",
"Logs": "NA"
}' "http://.compute.amazonaws.com:800/ticket_details/$recordId")
echo "PUT Response: $putResponse"
# Extract the "message" field from the JSON response
message=$(echo "$putResponse" | jq -r '.message')
if [[ "$message" == "Data name updated successfully" ]]; then
echo "Status update to triggered was successful!"
else
echo "Status update to triggered failed with message: $message"
exit 1
fi
# Print the full response for debugging
echo "Response: $putResponse"
# Set the recordId as a pipeline variable
echo "##vso[task.setvariable variable=recordId;isOutput=true]$recordId"
displayName: 'Fetch, Update and Validate Record'
- stage: tfvalidate
dependsOn: UpdateStatus
jobs:
- job: validate
variables:
- group: AzureDevopsCred
- name: newRecordId
value: $[ stageDependencies.UpdateStatus.UpdateTriggeredStatus.outputs['UpdateTriggeredStatusJob.recordId'] ]
continueOnError: false
steps:
- task: TerraformInstaller@0
inputs:
terraformVersion: 'latest'
- task: TerraformTaskV3@3
displayName: init
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: 'LaunchStack/terraform/azure/testmarklogic'
backendServiceArm: 'OIDC_TEST'
backendAzureRmResourceGroupName: $(ResourceGroup)
backendAzureRmStorageAccountName: $(StorageAccount)
backendAzureRmContainerName: $(Container)
backendAzureRmKey: $(TfstateFile)
- script: |
echo "Record ID: $(newRecordId)"
putResponse=$(curl -s -X PUT -H "Content-Type: application/json" -d '{
"TicketNumber": "${{ parameters.TicketNumber }}",
"TicketSysID": "${{ parameters.TicketSysID }}",
"Status": "Executing",
"Logs": "NA"
}' "http://amazonaws.com:800/ticket_details/$(newRecordId)")
echo "PUT Response: $putResponse"
displayName: 'Update Record Status to Executing'
2
Answers
Dependencies should use step names. Check examples here: Set an output variable for use in future jobs, Dependency syntax overview.
Add the name property to the first script and use it in the reference.
To use the output variable from a different stage, the format for referencing variables is the following:
The first thing to do is to set the name of the task where the output variable is being set:
And then fix the reference to the output variable, according to the syntax mentioned above: