如何在未知Job ID的情况下下载GitLab流水线最新运行Job的构件(不受运行结果限制)
Absolutely, you can work around this limitation by chaining a few GitLab API endpoints to first locate the latest pipeline and its corresponding job, then pull the artifacts you need. Here's a practical, step-by-step approach:
Step 1: Fetch the Latest Pipeline (Any Status)
First, grab the most recent pipeline for your project (or specific branch) — this works no matter if the pipeline succeeded, failed, or was canceled. Use this API endpoint:
GET /projects/:id/pipelines?order_by=updated_at&sort=desc&per_page=1
:id: Replace with your project's numeric ID (find it in project settings or the project URL)- Add
ref=<branch_name>as a query parameter if you want to target artifacts from a specific branch (e.g.,ref=main)
Step 2: Retrieve the Target Job ID from the Pipeline
Once you have the pipeline ID, fetch all jobs linked to that pipeline and filter for your specific test job (the one configured to generate the when: always artifacts). Use this endpoint:
GET /projects/:id/pipelines/:pipeline_id/jobs
Filter the JSON response to find the job matching your target name — this will give you the job ID you need to access the artifacts.
Step 3: Download the Dynamically Named XML File
With the job ID in hand, you can directly download your test results file using:
GET /projects/:id/jobs/:job_id/artifacts/reports/junit/test-results-${JOB_ID}.xml
Replace ${JOB_ID} with the actual job ID you pulled in step 2.
Example Bash Script
Here's a complete automation script using curl and jq (a lightweight JSON parser) to run this flow end-to-end:
# Configure your variables GITLAB_INSTANCE="https://your-gitlab-url.com" PROJECT_ID="12345" # Replace with your project ID TARGET_JOB_NAME="run-tests" # Replace with your test job's name ACCESS_TOKEN="your-personal-token" # Needs `read_api` scope # Step 1: Get the latest pipeline ID PIPELINE_ID=$(curl --silent --header "PRIVATE-TOKEN: $ACCESS_TOKEN" \ "$GITLAB_INSTANCE/api/v4/projects/$PROJECT_ID/pipelines?order_by=updated_at&sort=desc&per_page=1" | \ jq -r '.[0].id') # Step 2: Get the target job ID from the pipeline JOB_ID=$(curl --silent --header "PRIVATE-TOKEN: $ACCESS_TOKEN" \ "$GITLAB_INSTANCE/api/v4/projects/$PROJECT_ID/pipelines/$PIPELINE_ID/jobs" | \ jq -r --arg JOB_NAME "$TARGET_JOB_NAME" '.[] | select(.name == $JOB_NAME) | .id') # Step 3: Download the test results XML curl --header "PRIVATE-TOKEN: $ACCESS_TOKEN" \ "$GITLAB_INSTANCE/api/v4/projects/$PROJECT_ID/jobs/$JOB_ID/artifacts/reports/junit/test-results-$JOB_ID.xml" \ -o latest-test-results.xml
Key Notes
- Permissions: Make sure your personal access token has the
read_apiscope (create one in your GitLab user settings under "Access Tokens"). - Error Handling: For production use, add checks to verify
PIPELINE_IDandJOB_IDaren't empty before proceeding (e.g.,if [ -z "$PIPELINE_ID" ]; then echo "Failed to fetch pipeline ID"; exit 1; fi). - Alternative Parsers: If
jqisn't available, you can use Python's built-injsonmodule or another tool to parse the API responses.
内容的提问来源于stack exchange,提问作者Nikita Meier

