在VSTS构建任务中执行cURL命令的技术实现问询
Got it, let's break down exactly how to set this up in your Azure DevOps (formerly VSTS) build pipeline. I'll cover two straightforward approaches depending on whether you want to stick with raw cURL commands or leverage your existing Postman setup:
Option 1: Use the Command Line Task (Simplest for cURL)
This is the most direct way to run your validated cURL commands right in the pipeline.
Add a Command Line task to your pipeline
Navigate to your build definition, click "Add task", search for "Command Line", and drop it into your desired stage.Step 1: Login with a random access token & capture the auth token
Paste your Postman-derived cURL command into the "Script" box, then tweak it to:- Generate a random token (I'll use
opensslhere, but adjust to your system's tooling) - Capture the returned auth token as a pipeline variable for later use
# Generate random token and login to get auth credentials RANDOM_TOKEN=$(openssl rand -hex 16) AUTH_TOKEN=$(curl -X POST "https://your-db-login-api.com/auth" \ -H "Content-Type: application/json" \ -d '{"username":"$(db-username)","password":"$(db-password)","random-token":"'"$RANDOM_TOKEN"'"}' \ | jq -r '.access_token') # Save auth token as a pipeline variable (accessible in subsequent tasks) echo "##vso[task.setvariable variable=AuthToken;]$AUTH_TOKEN"Note: Store sensitive values like
db-usernameanddb-passwordas Secret Variables in your pipeline to avoid hardcoding.- Generate a random token (I'll use
Step 2: Fetch solution ID list & select one
Add the next cURL command to the same script (or a separate Command Line task) to pull solution IDs, then pick one (e.g., the first result) and save it as another variable:# Fetch all solution IDs SOLUTION_IDS=$(curl -X GET "https://your-api.com/solutions" \ -H "Authorization: Bearer $(AuthToken)" \ | jq -r '.solutions[].id') # Select a specific ID (example: first one in the list) SELECTED_SOLUTION_ID=$(echo "$SOLUTION_IDS" | head -n 1) echo "##vso[task.setvariable variable=SelectedSolutionId;]$SELECTED_SOLUTION_ID"Step 3: Retrieve file list for the selected solution
Wrap up with the final cURL command, using the saved variables:# Pull file list and save to a JSON file for later use/archiving curl -X GET "https://your-api.com/solutions/$(SelectedSolutionId)/files" \ -H "Authorization: Bearer $(AuthToken)" \ -o solution_files_output.json
Option 2: Use Newman (Postman's Command-Line Tool)
If you already have a validated Postman collection, this approach lets you reuse that work directly without rewriting commands.
Add tasks to install Newman
Newman is Postman's CLI tool—first, add an "npm" task to install it (skip this if using a self-hosted agent with Newman pre-installed):npm install -g newmanExport your Postman assets
In Postman, export your collection (as a.jsonfile) and any environment variables you use. Upload these files to your Azure DevOps repo or Library.Add a Command Line task to run Newman
Configure the script to execute your Postman collection, passing in dynamic values (like the random token) as environment variables:# Generate random token RANDOM_TOKEN=$(openssl rand -hex 16) # Run Postman collection, overriding variables with pipeline values newman run "your-postman-collection.json" \ -e "your-postman-environment.json" \ --env-var "random-token=$RANDOM_TOKEN" \ --env-var "auth-token=$(AuthToken)" \ --env-var "selected-solution-id=$(SelectedSolutionId)"You can set up your Postman collection to save the auth token and selected solution ID as collection variables, so Newman passes them between requests automatically.
Key Tips for Success
- Dependency Checks: If using
jq(for JSON parsing) oropenssl, ensure your agent has these tools installed. For Microsoft-hosted agents, add a quick install step (e.g.,apt-get install jqfor Ubuntu agents,choco install jqfor Windows). - Error Handling: Add
set -eat the top of bash scripts to stop the pipeline if any cURL command fails, or useif [ $? -ne 0 ]; then exit 1; fiafter each command to catch errors. - Variable Persistence: If you split commands across separate tasks, use
##vso[task.setvariable variable=VarName;isOutput=true]to make variables accessible across stages.
内容的提问来源于stack exchange,提问作者hitman126

